Advertisement
Josif_tepe

Untitled

Oct 26th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. string multiply_two_numbers_as_string(string a, string b) {
  6.     int len_a = a.size();
  7.     int len_b = b.size();
  8.     int mult[len_a + len_b + 1];
  9.     for(int i = 0; i < len_a + len_b + 1; i++) {
  10.         mult[i] = 0;
  11.     }
  12.     int till_a = 0, till_b = 0;
  13.     if(a[0] == '-') {
  14.         till_a = 1;
  15.     }
  16.     if(b[0] == '-') {
  17.         till_b = 1;
  18.     }
  19.     for(int i = len_a - 1; i >= till_a; i--) {
  20.         for(int j = len_b - 1; j >= till_b; j--) {
  21.             mult[i + j + 1] += (a[i] - '0') * (b[j] - '0');
  22.         }
  23.     }
  24.     for(int i = len_a + len_b - 1; i >= 0; i--) {
  25.         if(mult[i] > 9) {
  26.             mult[i - 1] += (mult[i] / 10);
  27.             mult[i] %= 10;
  28.         }
  29.     }
  30.     int i = 0;
  31.     while(mult[i] == 0) {
  32.         i++;
  33.     }
  34.     string product = "";
  35.     if(till_a != till_b) {
  36.         product += "-";
  37.     }
  38.     for(int j = i; j < len_a + len_b; j++) {
  39.         product += (mult[j] + '0');
  40.     }
  41.     return product;
  42. }
  43. bool is_A_smaller_than_B(string A, string B) {
  44.     if(A.size() < B.size()) {
  45.         return true;
  46.     }
  47.     if(A.size() > B.size()) {
  48.         return false;
  49.     }
  50.     for(int i = 0; i < (int) A.size(); i++) {
  51.         if(A[i] < B[i]) {
  52.             return true;
  53.         }
  54.         if(A[i] > B[i]) {
  55.             return false;
  56.         }
  57.     }
  58.     return true;
  59. }
  60. string subtract_big_integer(string A, string B) {
  61.     if(is_A_smaller_than_B(A, B)) {
  62.         swap(A, B);
  63.     }
  64.     int sz_a = (int)A.size() - 1;
  65.     int sz_b = (int)B.size() - 1;
  66.     int carry = 0;
  67.     string diff = "";
  68.     while(sz_a >= 0 and sz_b >= 0) {
  69.         int digit_a = (A[sz_a] - '0');
  70.         int digit_b = (B[sz_b] - '0');
  71.         int razlika = digit_a - digit_b - carry;
  72.         if(razlika < 0) {
  73.             razlika += 10;
  74.             carry = 1;
  75.         }
  76.         else {
  77.             carry = 0;
  78.         }
  79.         diff += (razlika + '0');
  80.         sz_a--;
  81.         sz_b--;
  82.     }
  83.     while(sz_a >= 0) {
  84.         int digit_a = (A[sz_a] - '0');
  85.         int razlika = digit_a - carry;
  86.         if(razlika < 0) {
  87.             carry = 1;
  88.             razlika += 10;
  89.         }
  90.         else {
  91.             carry = 0;
  92.         }
  93.         diff += (razlika + '0');
  94.     }
  95.     reverse(diff.begin(), diff.end());
  96.     int i = 0;
  97.     string result = "";
  98.     while(diff[i] == '0') {
  99.         i++;
  100.     }
  101.     for(int j = i; j < diff.size(); j++) {
  102.         result += diff[j];
  103.     }
  104.     return result;
  105. }
  106. string add_big_integer(string A, string B) { // funkcija koja ke ni sobere A + B
  107.     if(A.size() < B.size()) {
  108.         swap(A, B);
  109.     }
  110.     int a_i = (int) A.size() - 1;
  111.     int b_i = (int) B.size() - 1;
  112.     int carry = 0;
  113.     string sum = "";
  114.     while(a_i >= 0 and b_i >= 0) {
  115.         int digit_a = (A[a_i] - '0');
  116.         int digit_b = (B[b_i] - '0');
  117.         int zbir = digit_a + digit_b + carry;
  118.        
  119.         if(zbir <= 9) {
  120.             sum += (zbir + '0');
  121.             carry = 0;
  122.         }
  123.         else {
  124.             carry = 1;
  125.             zbir -= 10;
  126.             sum += (zbir + '0');
  127.         }
  128.         a_i--;
  129.         b_i--;
  130.     }
  131.     while(a_i >= 0) {
  132.         int digit_a = (A[a_i] - '0');
  133.         int zbir = digit_a + carry;
  134.         if(zbir <= 9) {
  135.             sum += (zbir + '0');
  136.             carry = 0;
  137.         }
  138.         else {
  139.             carry = 1;
  140.             zbir -= 10;
  141.             sum += (zbir + '0');
  142.         }
  143.         a_i--;
  144.     }
  145.     if(carry == 1) {
  146.         sum += "1";
  147.     }
  148.     reverse(sum.begin(), sum.end());
  149.    
  150.     return sum;
  151. }
  152. string divide_two_big_numbers_as_string(string a, string b) {
  153.     if(is_A_smaller_than_B(a, b)) {
  154.         swap(a, b);
  155.     }
  156.    
  157.     string S = "";
  158.     for(int i = 0; i < b.size(); i++) {
  159.         S += a[i];
  160.     }
  161.     if(is_A_smaller_than_B(S, b)) {
  162.         S += a[b.size()];
  163.     }
  164.     string diff  = S;
  165.     string pati = "1";
  166.     while(!is_A_smaller_than_B(diff, b)) {
  167.         if(!is_A_smaller_than_B(subtract_big_integer(diff, b), b)) {
  168.             diff = subtract_big_integer(diff, b);
  169.             pati = add_big_integer(pati, "1");
  170.         }
  171.         else {
  172.             break;
  173.         }
  174.     }
  175.    
  176.     string remainder = "";
  177.     string quotient = "";
  178.     quotient += pati;
  179.     for(int i = (int) S.size(); i < (int) a.size(); i++) {
  180.         remainder = subtract_big_integer(S, multiply_two_numbers_as_string(pati, b));
  181.         remainder += a[i];
  182.         diff = remainder;
  183.         pati = "1";
  184.         while(!is_A_smaller_than_B(diff, b)) {
  185.             if(!is_A_smaller_than_B(subtract_big_integer(diff, b), b)) {
  186.                 diff = subtract_big_integer(diff, b);
  187.                 pati = add_big_integer(pati, "1");
  188.             }
  189.             else {
  190.                 break;
  191.             }
  192.         }
  193.         quotient += pati;
  194.         S = remainder;
  195.     }
  196.    
  197.     return quotient;
  198. }
  199.  
  200. int main() {
  201.     string a, b;
  202.     a = "32167";
  203.     b = "123";
  204.     cout << divide_two_big_numbers_as_string(a, b) << endl;
  205.     return 0;
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement