AlenAntonelli

Suma de numeros grandes

May 30th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. string suma (string &a, string &b)
  8. {
  9.     int may = max( a.size(), b.size() );
  10.    
  11.     reverse( a.begin(), a.end() );
  12.     reverse( b.begin(), b.end() );
  13.    
  14.     for(int i=a.size(); i<may; i++) ///que me haga max-x.size() de operaciones
  15.         a.push_back('0');
  16.     for(int i=b.size(); i<may; i++) ///que me haga max-x.size() de operaciones
  17.         b.push_back('0');
  18.        
  19.     bool ac = false;
  20.     string c;
  21.    
  22.     for(int i=0; i<a.size(); i++)
  23.     {
  24.         int A = a[i]-'0';
  25.         int B = b[i]-'0';
  26.        
  27.         int C = A+B+ac;
  28.         ac = C/10;
  29.         c.push_back( (C)%10 +'0' );
  30.     }
  31.     if(ac)
  32.         c.push_back('1');
  33.    
  34.     reverse(c.begin(), c.end());
  35.    
  36.     return c;
  37. }
  38.  
  39. int main()
  40. {
  41.     string a, b;
  42.    
  43.     cout<<"Ingrese el primer sumando:";
  44.     cin>>a;
  45.     cout<<"Ingrese el segundo sumando:";
  46.     cin>>b;
  47.     cout<<"La suma es:"<<suma(a,b);
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment