Advertisement
Marisichka

Untitled

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