Advertisement
Marisichka

Untitled

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