irapilguy

Untitled

Jun 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. #define N 1000
  6. //char num1[N], num2[N];
  7. struct BigInteger {
  8.     int mas[N];
  9.     int size;
  10.     bool sign;
  11.     BigInteger(){
  12.     }
  13.     BigInteger(int a) {
  14.         size = a;
  15.         sign = true;
  16.         setZero();
  17.     }
  18.     BigInteger(int  digits[], int sizee) {
  19.         for (int i = 0; i <= sizee; i++) {
  20.             mas[i] = digits[i];
  21.         }
  22.         size = sizee;
  23.     }
  24.     BigInteger(string str) {
  25.         size = str.size() - 1;
  26.         setZero();
  27.         for (int i = 0; i <= size; i++) {
  28.             mas[i] = (str.at(size - i) - '0');
  29.         }
  30.     }
  31.     void setZero() {
  32.         for (int i = 0; i < N; i++) {
  33.             mas[i] = 0;
  34.         }
  35.     }
  36.     bool compareNum(BigInteger num) {
  37.         int maxSize = max(size, num.size);
  38.         for (int i = maxSize; i >= 0; i--) {
  39.             if (mas[i] > num.mas[i]) {
  40.                 return true;
  41.             }
  42.             if (num.mas[i] > mas[i]) return false;
  43.         }
  44.         return true;
  45.     }
  46.     BigInteger getSum(BigInteger num) {
  47.         int sizeRes;
  48.         int maxSize = max(size, num.size);
  49.         BigInteger res(0);
  50.         for (int i = 0; i <= maxSize; i++) {
  51.             int x = mas[i]  + num.mas[i];
  52.             res.mas[i] += x;
  53.             res.mas[i + 1] = res.mas[i]/ 10;
  54.             res.mas[i] %= 10;
  55.             sizeRes = i + (res.mas[i + 1] ? 1 : 0);    
  56.         }
  57.         res.size = sizeRes;
  58.         return res;
  59.     }
  60.     void print() {
  61.         if (sign == false) cout << "-";
  62.         for (int i = size; i >= 0; i--) {
  63.             cout << mas[i];
  64.         }
  65.     }
  66.     BigInteger getSubtraction(BigInteger num) {
  67.         int x;
  68.         BigInteger res(0);
  69.         int maxSize = max(size, num.size);
  70.         bool maxNum = compareNum(num);
  71.         if (maxNum == false) res.sign = false;
  72.         for (int i = 0; i <= maxSize; i++) {
  73.             if (maxNum == true) {
  74.                 x = mas[i] - num.mas[i];
  75.             }
  76.             else x = num.mas[i] - mas[i];
  77.             if (x < 0) {
  78.                 res.mas[i] += 10 + x;
  79.                 res.mas[i + 1] -= 1;
  80.             }
  81.             if (x == 0) {
  82.                 if (res.mas[i] < 0) {
  83.                     res.mas[i] = 10 + res.mas[i];
  84.                     res.mas[i + 1] -= 1;
  85.                 }
  86.             }
  87.             if (x > 0) {
  88.                 res.mas[i] += x;
  89.             }
  90.         }
  91.         int sizeRes = maxSize;
  92.         int i = maxSize;
  93.         while (i > 0) {
  94.             if (res.mas[i] == 0) {
  95.                 sizeRes--;
  96.             }
  97.             if (res.mas[i] != 0) break;
  98.             i--;
  99.         }
  100.         res.size = sizeRes;
  101.         return res ;
  102.     }
  103. };
  104.  
  105. int main() {
  106.     string str1, str2;
  107.     cin >> str1 >> str2;
  108.     BigInteger a(str1);
  109.     BigInteger b(str2);
  110.     BigInteger c = a.getSubtraction(b);
  111.     c.print();
  112.     return 0;
  113. }
Add Comment
Please, Sign In to add comment