Vladislav_Bezruk

Untitled

Oct 8th, 2021
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define SIZE 25
  5.  
  6. using namespace std;
  7.  
  8. class LongNumber {
  9.  
  10. public:
  11.     int data[SIZE];
  12.  
  13.     LongNumber() {
  14.  
  15.         for (int i = 0; i < SIZE; i++)
  16.             data[i] = 0;
  17.  
  18.     }
  19.  
  20.     LongNumber(string _no) {
  21.  
  22.         for (int i = 0; i < SIZE; i++) {
  23.             data[i] = 0;
  24.         }
  25.  
  26.         int index = SIZE - 1;
  27.  
  28.         for (int i = _no.length() - 1; i >= 0; i--, index--) {
  29.             data[index] = _no[i] - '0';
  30.         }
  31.     }
  32.  
  33.     LongNumber operator *(LongNumber& x) {
  34.  
  35.         LongNumber ret;
  36.         int carry = 0;
  37.         int mat[2 * SIZE + 1][2 * SIZE] = { 0 };
  38.  
  39.         for (int i = SIZE - 1; i >= 0; i--) {
  40.  
  41.             for (int j = SIZE - 1; j >= 0; j--) {
  42.                 carry += data[i] * x.data[j];
  43.  
  44.                 int index = SIZE - 1 - ((SIZE - 1 - i) + (SIZE - 1 - j));
  45.  
  46.                 if (index >= 0) {
  47.                     if (carry < 10) {
  48.                         //cout << "index = " << SIZE - 1 - ((SIZE - 1 - i) + (SIZE - 1 - j)) << endl;
  49.                         mat[i][SIZE - 1 - ((SIZE - 1 - i) + (SIZE - 1 - j))] = carry;
  50.                         carry = 0;
  51.                     }
  52.                     else {
  53.                         mat[i][SIZE - 1 - ((SIZE - 1 - i) + (SIZE - 1 - j))] = carry % 10;
  54.                         carry = carry / 10;
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.         for (int i = 1; i < SIZE + 1; i++) {
  60.             for (int j = SIZE - 1; j >= 0; j--) {
  61.                 carry += mat[i][j] + mat[i - 1][j];
  62.  
  63.                 if (carry < 10) {
  64.  
  65.                     mat[i][j] = carry;
  66.  
  67.                     carry = 0;
  68.  
  69.                 }
  70.  
  71.                 else {
  72.  
  73.                     mat[i][j] = carry % 10;
  74.  
  75.                     carry = carry / 10;
  76.  
  77.                 }
  78.             }
  79.         }
  80.  
  81.         //cout << "yes!" << endl;
  82.  
  83.         for (int i = 0; i < SIZE; i++) {
  84.             //cout << "yes! " << i << endl;
  85.             //cout << mat[SIZE][i] << endl;
  86.             //cout << ret.len[i] << endl;
  87.             ret.data[i] = mat[SIZE][i];
  88.  
  89.             //cout << "yes!af " << i << endl;
  90.         }
  91.  
  92.         //cout << "yes!af " << endl;
  93.  
  94.         return ret;
  95.     }
  96.  
  97.     void print() {
  98.  
  99.         int start = 0;
  100.         for (int i = 0; i < SIZE; i++)
  101.             if (data[i] != 0) {
  102.                 start = i;
  103.                 break;
  104.             }
  105.  
  106.         for (int i = start; i < SIZE; i++)
  107.  
  108.             cout << data[i];
  109.         cout << endl;
  110.         return;
  111.  
  112.     }
  113. };
  114.  
  115. int main() {
  116.  
  117.     string str1, str2;
  118.     cin >> str1 >> str2;
  119.  
  120.     LongNumber n1(str1);
  121.     LongNumber n2(str2);
  122.  
  123.     LongNumber n3 = n1 * n2;
  124.  
  125.     //cout << " " << endl;
  126.  
  127.    
  128.         int k = 0;
  129.         for (int i = 0; i < SIZE; i++)
  130.         {
  131.             if (n3.data[i] == 0)
  132.             {
  133.                 k++;
  134.             }
  135.             else
  136.                 break;
  137.         }
  138.  
  139.         if (k == SIZE){
  140.             cout << "0" << endl;
  141.         }
  142.  
  143.        else {
  144.  
  145.             for (int i = k; i < SIZE; i++){
  146.                 cout << n3.data[i];
  147.             }
  148.         }
  149.    
  150.     return 0;
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment