效果:
代码:
#include
#include
void reverse(char s[]) {
int len = strlen(s), h = len / 2;
char temp;
for (int i = 0; i < h; ++i) {
temp = s[i];
s[i] = s[len - i - 1];
s[len - i - 1] = temp;
}
}
#define System 10
#define MAX 24
const char mx = '9';
const char mn = '0';
char* Sum(char s1[], char s2[]) {
char ans[MAX];
int len1 = strlen(s1), len2 = strlen(s2), ad = 0, min = len1 < len2 ? len1 : len2, max = len1>len2 ? len1 : len2, i;
char* st1, *st2;
if (len1 > len2) { st1 = s2; st2 = s1; }
else { st1 = s1; st2 = s2; }
reverse(st1);
reverse(st2);
for (i = 0; i < min; i++) {
ans[i] = st1[i] + st2[i] - '0' + ad;
if (ans[i] > mx) {
ans[i] -= System;
ad = 1;
}
else ad = 0;
}
while (ad != 0 || iif (i < max)ans[i] = st2[i] + ad;
else ans[i] = mn + ad;
if (ans[i] > mx) {
ans[i] -= System;
ad = 1;
}
else ad = 0;
i++;
}
ans[i] = '\0';
reverse(ans);
return ans;
}
int main() {
char A[21], B[21], C[50], *ans;
int n, m;
scanf("%d %d", &m, &n);
scanf("%s %s", A, B);
ans = Sum(A, B);
strcpy(C, ans);
printf("%s\n", C);
}