Summary of addition and subtraction of large numbers
Sum up the addition and subtraction of large numbers (including integers or negative numbers ):
1. First remove the addition and subtraction of a number without symbols
2. Determine whether to select addition or subtraction calculation based on the number of addition or subtraction, and assign the corresponding symbol to the result.
Note that the result of unsigned subtraction may be '0' at a high level and must be processed.
String bigNumberMinusWithoutSign (const char * num1, const char * num2) {string res = ""; bool flag1 = false; // Positive and Negative flag int flag = 0; // return sign const char * p1, * p2; p1 = num1, p2 = num2; int len1 = strlen (p1); int len2 = strlen (p2 ); int index1 = len1-1; int index2 = len2-1; if (len1 <len2 | len1 = len2 & strcmp (num1, num2) <0) {flag1 = true; p1 = num2; p2 = num1; index1 = len2-1; index2 = len1-1;} // process the subtraction while (I Ndex1> = 0 & index2> = 0) {if (p1 [index1] + flag> p2 [index2]) {res = char (p1 [index1] + flag-p2 [index2] + '0') + res; flag = 0 ;} else if (p1 [index1] + flag = p2 [index2]) {res = char ('0') + res; flag = 0 ;} else {res = char (p1 [index1] + flag + 10-p2 [index2] + '0') + res; flag =-1 ;}-- index1, -- index2 ;} // process the remaining digits of a large number while (index1> = 0) {if (flag + p1 [index1] <'0 ') {res = char (flag + p1 [index1] + 10) + res; flag =-1;} else {res = c Har (flag + p1 [index1]) + res; flag = 0 ;}-- index1 ;}int zeroCount = 0; for (int k = 0; k <res. length (); ++ k) {if (res [k] = '0') ++ zeroCount; elsebreak;} res. erase (0, zeroCount); if (res. length () = 0) return res; if (flag1 = true) res = char ('-') + res; return res;} string bigNumberAddWithoutSign (const char * num1, const char * num2) {string res = ""; if (! Num1 |! Num2) return res; const char * p1, * p2; p1 = num1, p2 = num2; int len1 = strlen (p1); int len2 = strlen (p2 ); int index1 = len1-1; int index2 = len2-1; if (len1 <len2) {p1 = num2; p2 = num1; index1 = len2-1; index2 = len1-1;} int flag = 0; // carry flag int temp; while (index1> = 0 & index2> = 0) {temp = flag + p1 [index1] + p2 [index2]-2 * '0'; if (temp> = 10) flag = 1; elseflag = 0; res = char (temp % 10 + '0') + res; -- I Ndex1, -- index2;} while (index1> = 0) {temp = flag + p1 [index1]-'0'; if (temp> = 10) flag = 1; elseflag = 0; res = char (temp % 10 + '0') + res; -- index1;} return res;} string bigNumberAdd (const char * num1, const char * num2) {bool sign1 = true; bool sign2 = true; const char * p1 = num1; const char * p2 = num2; if (* p1 = '-') {sign1 = false; + + p1;} else if (* p1 = '+') + + p1; if (* p2 = '-') {sign2 = false; ++ p2 ;} else if (* p2 = '+') + + P2; string res; if (sign1 & sign2) return bigNumberAddWithoutSign (p1, p2); else if (sign1 &&! Sign2) return bigNumberMinusWithoutSign (p1, p2); else if (! Sign1 & sign2) return bigNumberMinusWithoutSign (p2, p1); else {res = bigNumberAddWithoutSign (p1, p2); res = char ('-') + res; return res ;}} string bigNumberMinus (const char * num1, const char * num2) {bool sign1 = true; bool sign2 = true; const char * p1 = num1; const char * p2 = num2; if (* p1 = '-') {sign1 = false; ++ p1;} else if (* p1 = '+') ++ p1; if (* p2 = '-') {sign2 = false; ++ p2;} else if (* p2 = '+') ++ p2; if (sign1 & Sign2) return bigNumberMinusWithoutSign (p1, p2); else if (sign1 &&! Sign2) return bigNumberAddWithoutSign (p1, p2); else if (! Sign1 & sign2) {string res = bigNumberAddWithoutSign (p1, p2); res = char ('-') + res; return res;} elsereturn bigNumberMinusWithoutSign (p2, p1 );}