加法:
1 string add(string s1, string s2) { 2 int len1 = s1.length(), len2 = s2.length(); 3 int maxlen = max(len1, len2) + 1; 4 string res(maxlen, ‘0‘); 5 int flag = 0; 6 int i = len1 - 1, j = len2 - 1, k = maxlen - 1; 7 for (; i >= 0 && j >= 0; i--, j--) { 8 int temp = (s1[i] - ‘0‘) + (s2[j] - ‘0‘) + flag; 9 flag = temp / 10; 10 res[k--]= temp % 10 + ‘0‘; 11 } 12 while (i >= 0) { 13 int temp = s1[i--] - ‘0‘ + flag; 14 flag = temp / 10; 15 res[k--] = temp % 10 + ‘0‘; 16 } 17 while (j >= 0) { 18 int temp = s2[j--] - ‘0‘ + flag; 19 flag = temp / 10; 20 res[k--] = temp % 10 + ‘0‘; 21 } 22 res[k] = flag + ‘0‘; 23 if (flag) { 24 return res; 25 } else { 26 return res.substr(1); 27 } 28 }
减法:
乘法:
除法:
取模:
原文:https://www.cnblogs.com/qinduanyinghua/p/11440852.html