Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ====================================================================================
- // Bublik.hpp
- #include <vector>
- const int Pepper = 5 + 5;
- void AddSugar();
- void AddSaltToFront(std::vector<int>& salts);
- // ====================================================================================
- // Bublik.cpp
- // Подключение своего же объявления.
- #include "Bublik.hpp"
- #include "Main.hpp"
- // Duplicate symbol
- // double Salt = 4;
- // Не дубликат, так как константа.
- // const int Pepper = 7;
- // Объявление, не нарушает ODR.
- void AddSugar();
- // Определение
- void AddSugar() {
- ++Sugar;
- }
- // ====================================================================================
- // Main.hpp
- extern int Sugar;
- inline void AddDoubleSugar() {
- Sugar += 2;
- }
- // ====================================================================================
- // Main.cpp
- #include "Main.hpp"
- #include <iostream>
- #include "BigInteger.hpp"
- #include "Bublik.hpp"
- int Sugar = 1;
- int Salt = 3;
- // Redefinition из Bublik.hpp
- // const int Pepper = 5;
- int main1() {
- Sugar = 2;
- AddSugar();
- AddDoubleSugar();
- std::cout << "Sweetness is " << Sugar << std::endl;
- std::cout << "Salt is " << Salt << std::endl;
- std::cout << "Pepper is " << Pepper << std::endl;
- return 0;
- }
- int main() {
- std::string s; std::cin >> s;
- BigInteger bigint(s);
- std::cout << bigint << std::endl;
- BigInteger second(std::move(bigint));
- std::cout << "First = " << bigint << ", second = " << second << std::endl;
- std::cout << "Minus second = " << -second << std::endl;
- }
- // ====================================================================================
- // BigInteger.hpp
- #include <ostream>
- #include <string>
- #include <vector>
- class BigInteger {
- public:
- // Конструктор по умолчанию. Ноль
- BigInteger() {}
- // Конструктор копирования
- BigInteger(const BigInteger& other) = default;
- // Конструктор переноса
- BigInteger(BigInteger&& other) : digits(std::move(other.digits)), negative(other.negative) {}
- // Оператор присваивания
- BigInteger& operator=(const BigInteger& other) = default;
- // Оператор переноса
- BigInteger& operator=(BigInteger&& other) = default;
- // Деструктор
- ~BigInteger() {}
- explicit BigInteger(const std::string& source);
- std::string ToString() const;
- BigInteger operator-() const;
- BigInteger& operator++(); // Префиксный
- BigInteger operator++(int); // Постфиксный
- BigInteger operator+(const BigInteger& right);
- private:
- // Младшие цифры в начале.
- std::vector<unsigned int> digits;
- bool negative = false;
- void AddShort(int s);
- void MultiplyByShort(unsigned int s);
- };
- BigInteger operator+(int left, const BigInteger& right);
- std::ostream& operator<<(std::ostream& out, const BigInteger& bigint);
- // ====================================================================================
- // BigInteger.cpp
- #include "BigInteger.hpp"
- #include <algorithm>
- unsigned int Base = 1'000'000'000u;
- BigInteger::BigInteger(const std::string& source) {
- if (source.empty()) throw std::bad_cast();
- if (source[0] == '-') negative = true;
- for (int i = source.length(); i > negative; i -= 9) {
- digits.push_back(0);
- for (int j = std::max(int(negative), i - 9); j < i; ++j) {
- if (source[j] < '0' || source[j] > '9') throw std::bad_cast();
- digits.back() = digits.back() * 10 + source[j] - '0';
- }
- }
- while (!digits.empty() && digits.back() == 0) digits.pop_back();
- }
- std::string BigInteger::ToString() const {
- if (digits.empty()) return "0";
- std::string result = negative ? "-" : "";
- result += std::to_string(digits.empty() ? 0 : digits.back());
- for (int i = digits.size() - 2; i >= 0; --i) {
- std::string tmp = std::to_string(digits[i]);
- result += std::string(9 - tmp.size(), '0') + tmp;
- }
- return result;
- }
- BigInteger BigInteger::operator-() const {
- BigInteger result;
- result.digits = digits;
- return result;
- }
- BigInteger& BigInteger::operator++() {
- AddShort(1);
- return *this;
- }
- BigInteger BigInteger::operator++(int) {
- BigInteger result(*this);
- AddShort(1);
- return result;
- }
- void BigInteger::AddShort(int s) {
- if (!negative && !digits.empty() && digits[0] < Base - s) digits[0] += s;
- else {
- // TODO
- }
- }
- std::ostream& operator<<(std::ostream& out, const BigInteger& bigint) {
- return out << bigint.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement