One way to solve the problem of adding extra large numbers is to turn the shaping into a string, not much to say directly on the code, which has been debugged through.
#include <iostream> #include <string> #include <cmath>using namespace std;string Add (String str1, String str2) {int i;string str;int len_str1 = Str1.length (); int len_str2 = Str2.length (); int n = ABS (LEN_STR1-LEN_STR2);//Long degree alignment if (LEN_STR1 < LEN_STR2) for (i=0; i<n; i++) str1 = "0" + str1;elsefor (i=0; i<n; i++) str2 = "0" + str2;len_str1 = Str1.length ();//At this time the length of two strings is the same as int cf=0;//carry flag bit int temp;//holds two numbers added and for (i=len_str1-1; i>=0; i--) {temp = str1[i]-' 0 ' + str2[i]-' 0 ' + cf;//converts the string to integer CF = temp/10;temp = Temp%10;str = char (temp+ ' 0 ') + str;} if (cf!=0) str = char (cf+ ' 0 ') + str;return str;} int main () {string str1, str2;cin >> str1;cin >> str2;cout << Add (str1, str2) << Endl;return 0;}
Two extra large numbers added