✔ 最佳答案
#include <stdio.h>
#define MAX 256
void reverse(char* ptr)
{
char tmp[MAX];
char* s1;
char* s2=tmp;
s1 = ptr;
while (*s1 != '\0') {
s1++;
}
--s1;
while(s1 != ptr) {
*s2++ = *s1--;
}
*s2++ = *ptr;
*s2 = '\0';
strcpy( ptr, tmp );
}
int main()
{
char a[MAX];
char b[MAX];
int i,k,m,n,carry,total;
printf("Please input number A: ");
scanf("%s", a);
printf("Please input number B: ");
scanf("%s", b);
reverse(a);
reverse(b);
m = strlen(a);
n = strlen(b);
carry = 0;
total = 0;
k = (m > n ? n : m); /* whichever smaller */
for (i=0; i < k; i++) {
if ((a[i] + b[i] + carry - '0' - '0') >= 10 ) {
carry ++;
total ++;
}
if (carry > 1) {
carry = 1;
}
}
printf("Number of carry: %d\n", total);
}