Advertisement
Marisichka

Untitled

Sep 21st, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define N 10
  5.  
  6. using namespace std;
  7.  
  8. struct Number {
  9.     int* num;
  10.     Number() {
  11.         num = new int[N];
  12.     }
  13.  
  14. };
  15.  
  16. void output(Number a) {
  17.     int i = 0;
  18.  
  19.     while (i < N && a.num[i] == 0)
  20.         i++;
  21.  
  22.     if (i == N)
  23.         cout << "0";
  24.  
  25.     for (i; i < N; i++) {
  26.         cout << a.num[i];
  27.     }
  28.  
  29.     cout << endl;
  30. }
  31.  
  32. Number init(Number a) {
  33.  
  34.     for (int i = 0; i < N; i++)
  35.         a.num[i] = 0;
  36.  
  37.     return a;
  38. }
  39.  
  40. Number enter(Number a, Number b) {
  41.     string str;
  42.     int length;
  43.     Number num = init();
  44.  
  45.     cout << "Enter the number:" << endl;
  46.     cin >> str;
  47.  
  48.     length = str.length();
  49.  
  50.     for (int i = 0; i < str.length(); i++) {
  51.         num.num[N - str.length() + i] = str[i] - '0';
  52.     }
  53.  
  54.  
  55.     return num;
  56. }
  57. Number sum(Number a, Number b) {
  58.  
  59.     Number c = init();
  60.  
  61.     for (int i = N - 1; i > 0; i--) {
  62.         c.num[i] = a.num[i] + b.num[i];
  63.  
  64.         if (c.num[i] > 9) {
  65.             c.num[i] = c.num[i] - 10;
  66.             c.num[i - 1] = 1;
  67.         }
  68.     }
  69.  
  70.     output(c);
  71. }
  72.  
  73.  
  74. int compare(Number a, Number b) {
  75.  
  76.     for (int i = 0; i < N; i++) {
  77.         if (a.num[i] > b.num[i]) {
  78.             return 1;
  79.         }
  80.         else if (a.num[i] < b.num[i]) {
  81.             return -1;
  82.         }
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
  88. Number minus(Number a, Number b) {
  89.  
  90.     Number c = init();
  91.  
  92.     for (int i = N - 1; i > 0; i--) {
  93.         c.num[i] += a.num[i] - b.num[i];
  94.  
  95.         if (c.num[i] < 0) {
  96.             c.num[i] += 10;
  97.             c.num[i - 1] = -1;
  98.         }
  99.     }
  100.  
  101.     output(c);
  102. }
  103.  
  104. int main() {
  105.     int i, j;
  106.     Number* a = new Number[N];
  107.     Number* b = new Number[N];
  108.     Number* c = new Number[N];
  109.    Number a;
  110.  
  111.     a = enter();
  112.     cout << endl;
  113.  
  114.     b = enter();
  115.     cout << endl;
  116.  
  117.     int comp = compare(a, b);
  118.  
  119.     if (comp == 1) {
  120.         cout << "a > b" << endl;
  121.     }
  122.     else if (comp == 0) {
  123.         cout << "a == b" << endl;
  124.     }
  125.     else if (comp == -1) {
  126.         cout << "a < b" << endl;
  127.     }
  128.  
  129.     cout << endl << "The sum = ";
  130.  
  131.     c = sum(a, b);
  132.  
  133.     output(c);
  134.  
  135.     cout << endl << "The subtraction is = ";
  136.  
  137.     if (comp != -1) {
  138.         c = minus(a, b);
  139.     }
  140.     else {
  141.         cout << "-";
  142.         c = minus(b, a);
  143.     }
  144.  
  145.     delete[]c;
  146.  
  147.     cout << endl;
  148.  
  149.  
  150.     system("pause");
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement