Advertisement
Guest User

Untitled

a guest
Nov 27th, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int ti(char a) { // changes char to int
  4.     int output = a - 48;
  5.     return output;
  6. }
  7.  
  8. char tc(int a) { // changes int to char
  9.     char output = a + 48;
  10.     return output;
  11. }
  12.  
  13. void toMinus(std::string &a) { // 123 -> -1 -2 -3
  14.     for (auto &x : a) {
  15.         x = tc(-ti(x));
  16.     }
  17. }
  18.  
  19. void changeSize(std::string &a, std::string &b) {
  20.     size_t exp_size = std::max(a.size(), b.size()) + 2;
  21.     while (a.size() != exp_size) {
  22.         a = '0' + a;
  23.     }
  24.  
  25.     while (b.size() != exp_size) {
  26.         b = '0' + b;
  27.     }
  28. }
  29.  
  30. void removeZeros(std::string &a) {
  31.     int i = 0;    
  32.     for (; i < a.size(); i++) {
  33.         if (a[i] != '0') {
  34.             break;
  35.         }
  36.     }
  37.     a.erase(0, i);
  38.     if (a.size() == 0) {
  39.         a = "0";
  40.     }
  41. }
  42.  
  43. std::string add(std::string &a, std::string &b) {
  44.     bool neg[2] = {false, false};
  45.     bool out_negative = false;
  46.     if (a[0] == '-') {
  47.         neg[0] = true;
  48.         a.erase(0, 1);
  49.     }
  50.     if (b[0] == '-') {
  51.         neg[1] = true;
  52.         b.erase(0, 1);
  53.     }
  54.  
  55.     changeSize(a, b);
  56.  
  57.     if (neg[0] && !(neg[1] && neg[0])) {
  58.         toMinus(a);
  59.     }
  60.     if(neg[1] && !(neg[1] && neg[0])) {
  61.         toMinus(b);
  62.     }
  63.  
  64.     if (neg[1] && neg[0]) {
  65.         out_negative = true;
  66.     }
  67.    
  68.     // Dodawanie
  69.     for (int i = a.size() - 1; i > 0; i--) {
  70.         int _a = ti(a[i]);
  71.         int _b = ti(b[i]);
  72.         int out = _a + _b;
  73.         if (out >= 10) {
  74.             a[i - 1] += out / 10;
  75.         } else if (out < 0) {
  76.             if (abs(out) < 10) {
  77.                 a[i - 1]--;
  78.             } else {
  79.                 a[i - 1] += abs(out) / 10;
  80.             }
  81.             if (i != 1)
  82.                 out += 10;
  83.         }
  84.  
  85.         a[i] = tc(abs(out % 10));
  86.     }
  87.  
  88.     if (ti(a[0]) == -1) { // Overflow
  89.         out_negative = true;
  90.         a[0] = '0';
  91.         a[1]--;
  92.        
  93.         for (int i = 2; i < a.size(); i++) {
  94.             if (i == a.size() - 1) {
  95.                 a[i] = tc(10 - ti(a[i]));
  96.             } else {
  97.                 a[i] = tc(9 - ti(a[i]));
  98.             }
  99.         }
  100.     }
  101.  
  102.     if (neg[0] && neg[1]) {
  103.         out_negative = true;
  104.     }
  105.  
  106.     removeZeros(a);
  107.  
  108.     if (out_negative) {
  109.         a = '-' + a;
  110.     }
  111.  
  112.     return a;
  113. }
  114.  
  115. int main() {
  116.     std::ios_base::sync_with_stdio(0);
  117.     std::cin.tie(0);
  118.    
  119.     std::string a;
  120.     std::string b;
  121.     std::cin >> a >> b;
  122.     std::cout << add(a, b);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement