Created
February 24, 2011 02:57
-
-
Save pjox/841672 to your computer and use it in GitHub Desktop.
suma 2 números muy grandes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
using namespace std; | |
int main() { | |
string s1, s2; | |
cout << "primer numero:" << endl; | |
cin >> s1; | |
cout << "segundo numero:" << endl; | |
cin >> s2; | |
//cout << numberOne+numberTwo << endl; | |
//string s1 = "4545454545"; | |
//string s2 = "6455771"; | |
int diff=0; | |
int i; | |
string ceros = ""; | |
if(s1.length() > s2.length()) { | |
diff= s1.length() - s2.length(); | |
for (i=0; i<diff; i++) { | |
ceros = ceros + "0"; | |
} | |
s2 = ceros + s2; | |
diff=s1.length(); | |
} | |
else { | |
diff= s2.length() - s1.length(); | |
for (i=0; i<diff; i++) { | |
ceros = ceros + "0"; | |
} | |
s1=ceros + s1; | |
diff=s2.length(); | |
} | |
/*cout << s1; | |
cout << endl; | |
cout << s2; | |
cout << endl;*/ | |
int llevo=0; | |
stringstream sr; | |
string st; | |
string suma=""; | |
for(i = 0; i < diff; i++) { | |
char c1 = s1[diff-i-1]; | |
char c2 = s2[diff-i-1]; | |
int s = ((int)c1 - 48) + ((int)c2 - 48) + llevo; | |
if(s > 9) { | |
llevo = 1; | |
stringstream sr; | |
sr << s; | |
st=sr.str(); | |
suma = st[1] + suma; | |
} | |
else { | |
llevo = 0; | |
stringstream sr; | |
sr << s; | |
st=sr.str(); | |
suma = st[0] + suma; | |
} | |
} | |
if(llevo == 1) { | |
suma = "1" + suma; | |
} | |
cout << "RESULTADO:\n"; | |
cout << suma; | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment