Advertisement
Guest User

big_number

a guest
Apr 27th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. char compare(std::string s1, std::string s2)
  6. {
  7.     if(s1.length() != s2.length()) {
  8.         return (s1.length() > s2.length()) ? '>' : '<';
  9.     } else {
  10.         while(s1.length()) {
  11.             if(s1[0] > s2[0]) {
  12.                 return '>';
  13.             } else if (s1[0] < s2[0]) {
  14.                 return '<';
  15.             }
  16.             s1.erase(0, 1);
  17.             s2.erase(0, 1);
  18.         }
  19.         return '=';
  20.     }
  21. }
  22.  
  23. int toi(char ch) {
  24.     return ch - '0';
  25. }
  26.  
  27. char toc(int i) {
  28.     return i + '0';
  29. }
  30.  
  31. std::string op_plus(std::string s1, std::string s2) {
  32.     std::string s_add = "";
  33.     int tmp = 0;
  34.     while (s1 != "" || s2 != "" || tmp != 0) {
  35.         if (s1 != "") {
  36.             tmp += toi(s1[s1.length()-1]);
  37.             s1.erase(s1.length()-1);
  38.         }
  39.         if (s2 != "") {
  40.             tmp += toi(s2[s2.length()-1]);
  41.             s2.erase(s2.length()-1);
  42.         }
  43.         s_add += toc(tmp%10);
  44.         tmp /= 10;
  45.     }
  46.     while (s_add[s_add.length()-1] == '0' && s_add.length() != 1) {
  47.         s_add.erase(s_add.length()-1);
  48.     }
  49.     std::reverse(s_add.begin(), s_add.end());
  50.     return s_add;
  51. }
  52.  
  53. std::string op_minus(std::string s1, std::string s2) {
  54.     std::string s_sub = "";
  55.     int tmp = 0;
  56.     char cmp = compare(s1, s2);
  57.     if (cmp == '=') {
  58.         return "0";
  59.     } else if (cmp == '<'){
  60.         std::swap(s1, s2);
  61.     }
  62.     while (s1 != "") {
  63.         if (s2 != "") {
  64.             tmp += toi(s1[s1.length()-1]) - toi(s2[s2.length()-1]);
  65.             s2.erase(s2.length()-1);
  66.         } else {
  67.             tmp += toi(s1[s1.length()-1]);
  68.         }
  69.         s1.erase(s1.length()-1);
  70.         if (tmp < 0) {
  71.             s_sub += toc(tmp + 10);
  72.             tmp = -1;
  73.         } else {
  74.             s_sub += toc(tmp);
  75.             tmp = 0;
  76.         }
  77.     }
  78.     while (s_sub[s_sub.length()-1] == '0' && s_sub.length() != 1) {
  79.         s_sub.erase(s_sub.length()-1);
  80.     }
  81.     if (cmp == '<') {
  82.         s_sub += "-";
  83.     }
  84.     std::reverse(s_sub.begin(), s_sub.end());
  85.     return s_sub;
  86. }
  87.  
  88. std::string op_time_by_one_digit(std::string s1, char s2) {
  89.     std::string s_mul = "0";
  90.     if (s2 == '0') {
  91.         return "0";
  92.     } else {
  93.         for (int i = 0; i < (s2-'0'); ++i) {
  94.             s_mul = op_plus(s_mul, s1);
  95.         }
  96.         return s_mul;
  97.     }
  98. }
  99.  
  100. std::string op_time(std::string s1, std::string s2) {
  101.     std::string s_mul = "0", zero = "";
  102.     if (compare(s1, s2) == '<') {
  103.         std::swap(s1, s2);
  104.     }
  105.     while(s2 != "") {
  106.         s_mul = op_plus(s_mul, op_time_by_one_digit(s1, s2[s2.length()-1]) + zero);
  107.         s2.erase(s2.length()-1);
  108.         zero += "0";
  109.     }
  110.     return s_mul;
  111. }
  112.  
  113. std::string op_divide(std::string s1, std::string s2) {
  114.     std::string s_div = "1";
  115.     if (s1 == s2) {
  116.         return "1";
  117.     }
  118.    
  119.     while (compare(op_time(s_div, s2), s1) != '>') {
  120.         s_div += "0";
  121.     }
  122.     s_div.erase(s_div.length()-1);
  123.    
  124.     std::string tmp = s_div;
  125.     s_div = "0";
  126.     for (int i = 0; i < s_div.length(); ++i) {
  127.         for (int j = 0; j < 9; ++j) {
  128.             if (compare(op_time(s_div, s2), s1) == '>') {
  129.                 s_div = op_minus(s_div, tmp);
  130.                 break;
  131.             } else if (compare(op_time(s_div, s2), s1) == '=') {
  132.                 return s_div;
  133.             }
  134.             s_div = op_plus(s_div, tmp);
  135.         }
  136.         if (tmp != "") {
  137.             tmp.erase(tmp.length()-1);
  138.         }
  139.     }
  140.     return s_div;
  141. }
  142.  
  143. int main()
  144. {
  145.     std::string s1, s2, ans;
  146.     char ope;
  147.     while (std::cin >> s1 >> ope >> s2) {
  148.         ans = "";
  149.         switch (ope) {
  150.             case '+':
  151.                 ans = op_plus(s1, s2);
  152.                 break;
  153.             case '-':
  154.                 ans = op_minus(s1, s2);
  155.                 break;
  156.             case '*':
  157.                 ans = op_time(s1, s2);
  158.                 break;
  159.             case '/':
  160.                 ans = op_divide(s1, s2);
  161.                 break;
  162.             default:
  163.                 std::cerr << "Operator was not defined." << std::endl;
  164.                 return 1;
  165.         }
  166.         std::cout << ans << std::endl;
  167.     }
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement