加法:
1 char * add(char *s, char *ss) { 2 memset(str3, 0, sizeof(str3)); 3 int len1 = strlen(s)-1; 4 int len2 = strlen(ss)-1; 5 int len3 = 0, x, y, flag = 0; 6 while(len1 >= 0 || len2 >= 0) { 7 if(len1 >= 0) x = s[len1] - '0'; 8 else x = 0; 9 if(len2 >= 0) y = ss[len2] - '0'; 10 else y = 0; 11 str3[len3++] = (x+y+flag)%10 + '0'; 12 flag = (x+y+flag)/10; 13 len2--;len1--; 14 } 15 if(flag) str3[len3++] = '1'; 16 for(int i = 0; i < len3/2; i ++) { 17 swap(str3[i],str3[len3-i-1]); 18 } 19 return str3; 20 }
减法:
1 char *sub(char *s, char *ss) { 2 memset(str3, 0, sizeof(str3)); 3 int len1 = strlen(s)-1, len2 = strlen(ss)-1; 4 int flag = 1, len3 = 0, x, y, cnt = 0; 5 if(strcmp(s,ss) == 0){ 6 str3[len3++] = '0'; 7 return str3; 8 } 9 if(len1 < len2 || (len1==len2&&strcmp(s,ss)<0)) { 10 swap(len1,len2); 11 swap(s,ss); 12 flag = 0; 13 } 14 while(len1 >= 0 || len2 >= 0) { 15 if(len1 >= 0) x = s[len1--] - '0'; 16 else x = 0; 17 if(len2 >= 0) y = ss[len2--] - '0'; 18 else y = 0; 19 int ans = x-y+cnt; 20 if(ans < 0) { 21 str3[len3++] = ans+10+'0'; 22 cnt = -1; 23 } else { 24 str3[len3++] = ans+'0'; 25 cnt = 0; 26 } 27 } 28 while(str3[len3-1] == '0') { 29 str3[--len3] = '