Advertisement
smatskevich

Seminar3

Feb 27th, 2023
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. // ====================================================================================
  2. // Bublik.hpp
  3.  
  4. #include <vector>
  5.  
  6. const int Pepper = 5 + 5;
  7.  
  8. void AddSugar();
  9.  
  10. void AddSaltToFront(std::vector<int>& salts);
  11.  
  12. // ====================================================================================
  13. // Bublik.cpp
  14.  
  15. // Подключение своего же объявления.
  16. #include "Bublik.hpp"
  17.  
  18. #include "Main.hpp"
  19.  
  20. // Duplicate symbol
  21. // double Salt = 4;
  22.  
  23. // Не дубликат, так как константа.
  24. // const int Pepper = 7;
  25.  
  26. // Объявление, не нарушает ODR.
  27. void AddSugar();
  28.  
  29. // Определение
  30. void AddSugar() {
  31.   ++Sugar;
  32. }
  33.  
  34. // ====================================================================================
  35. // Main.hpp
  36.  
  37. extern int Sugar;
  38.  
  39. inline void AddDoubleSugar() {
  40.   Sugar += 2;
  41. }
  42.  
  43. // ====================================================================================
  44. // Main.cpp
  45.  
  46. #include "Main.hpp"
  47.  
  48. #include <iostream>
  49.  
  50. #include "BigInteger.hpp"
  51. #include "Bublik.hpp"
  52.  
  53. int Sugar = 1;
  54.  
  55. int Salt = 3;
  56.  
  57. // Redefinition из Bublik.hpp
  58. // const int Pepper = 5;
  59.  
  60. int main1() {
  61.   Sugar = 2;
  62.   AddSugar();
  63.   AddDoubleSugar();
  64.   std::cout << "Sweetness is " << Sugar << std::endl;
  65.   std::cout << "Salt is " << Salt << std::endl;
  66.   std::cout << "Pepper is " << Pepper << std::endl;
  67.   return 0;
  68. }
  69.  
  70. int main() {
  71.   std::string s; std::cin >> s;
  72.   BigInteger bigint(s);
  73.   std::cout << bigint << std::endl;
  74.   BigInteger second(std::move(bigint));
  75.   std::cout << "First = " << bigint << ", second = " << second << std::endl;
  76.   std::cout << "Minus second = " << -second << std::endl;
  77. }
  78.  
  79. // ====================================================================================
  80. // BigInteger.hpp
  81.  
  82. #include <ostream>
  83. #include <string>
  84. #include <vector>
  85.  
  86. class BigInteger {
  87.  public:
  88.   // Конструктор по умолчанию. Ноль
  89.   BigInteger() {}
  90.  
  91.   // Конструктор копирования
  92.   BigInteger(const BigInteger& other) = default;
  93.   // Конструктор переноса
  94.   BigInteger(BigInteger&& other) : digits(std::move(other.digits)), negative(other.negative) {}
  95.   // Оператор присваивания
  96.   BigInteger& operator=(const BigInteger& other) = default;
  97.   // Оператор переноса
  98.   BigInteger& operator=(BigInteger&& other) = default;
  99.   // Деструктор
  100.   ~BigInteger() {}
  101.  
  102.   explicit BigInteger(const std::string& source);
  103.   std::string ToString() const;
  104.  
  105.   BigInteger operator-() const;
  106.   BigInteger& operator++();  // Префиксный
  107.   BigInteger operator++(int);  // Постфиксный
  108.  
  109.   BigInteger operator+(const BigInteger& right);
  110.  
  111.  private:
  112.   // Младшие цифры в начале.
  113.   std::vector<unsigned int> digits;
  114.   bool negative = false;
  115.  
  116.   void AddShort(int s);
  117.   void MultiplyByShort(unsigned int s);
  118. };
  119.  
  120. BigInteger operator+(int left, const BigInteger& right);
  121.  
  122. std::ostream& operator<<(std::ostream& out, const BigInteger& bigint);
  123.  
  124. // ====================================================================================
  125. // BigInteger.cpp
  126.  
  127. #include "BigInteger.hpp"
  128.  
  129. #include <algorithm>
  130.  
  131. unsigned int Base = 1'000'000'000u;
  132.  
  133. BigInteger::BigInteger(const std::string& source) {
  134.  if (source.empty()) throw std::bad_cast();
  135.  
  136.  if (source[0] == '-') negative = true;
  137.  
  138.  for (int i = source.length(); i > negative; i -= 9) {
  139.    digits.push_back(0);
  140.    for (int j = std::max(int(negative), i - 9); j < i; ++j) {
  141.      if (source[j] < '0' || source[j] > '9') throw std::bad_cast();
  142.      digits.back() = digits.back() * 10 + source[j] - '0';
  143.    }
  144.  }
  145.  while (!digits.empty() && digits.back() == 0) digits.pop_back();
  146. }
  147.  
  148. std::string BigInteger::ToString() const {
  149.  if (digits.empty()) return "0";
  150.  
  151.  std::string result = negative ? "-" : "";
  152.  result += std::to_string(digits.empty() ? 0 : digits.back());
  153.  for (int i = digits.size() - 2; i >= 0; --i) {
  154.    std::string tmp = std::to_string(digits[i]);
  155.    result += std::string(9 - tmp.size(), '0') + tmp;
  156.  }
  157.  return result;
  158. }
  159.  
  160. BigInteger BigInteger::operator-() const {
  161.  BigInteger result;
  162.  result.digits = digits;
  163.  return result;
  164. }
  165.  
  166. BigInteger& BigInteger::operator++() {
  167.  AddShort(1);
  168.  return *this;
  169. }
  170.  
  171. BigInteger BigInteger::operator++(int) {
  172.  BigInteger result(*this);
  173.  AddShort(1);
  174.  return result;
  175. }
  176.  
  177. void BigInteger::AddShort(int s) {
  178.  if (!negative && !digits.empty() && digits[0] < Base - s) digits[0] += s;
  179.  else {
  180.    // TODO
  181.  }
  182. }
  183.  
  184. std::ostream& operator<<(std::ostream& out, const BigInteger& bigint) {
  185.  return out << bigint.ToString();
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement