Advertisement
Marisichka

Untitled

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