Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- string suma (string &a, string &b)
- {
- int may = max( a.size(), b.size() );
- reverse( a.begin(), a.end() );
- reverse( b.begin(), b.end() );
- for(int i=a.size(); i<may; i++) ///que me haga max-x.size() de operaciones
- a.push_back('0');
- for(int i=b.size(); i<may; i++) ///que me haga max-x.size() de operaciones
- b.push_back('0');
- bool ac = false;
- string c;
- for(int i=0; i<a.size(); i++)
- {
- int A = a[i]-'0';
- int B = b[i]-'0';
- int C = A+B+ac;
- ac = C/10;
- c.push_back( (C)%10 +'0' );
- }
- if(ac)
- c.push_back('1');
- reverse(c.begin(), c.end());
- return c;
- }
- int main()
- {
- string a, b;
- cout<<"Ingrese el primer sumando:";
- cin>>a;
- cout<<"Ingrese el segundo sumando:";
- cin>>b;
- cout<<"La suma es:"<<suma(a,b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment