#include<stdio.h> #include<string.h> #include<math.h> #define IndexMax 1002 char* puqi(char num[],int Lmax,int Lmin) { int i; char c[IndexMax]; for(i=0;i< Lmax;i++) { c[i]='0'; } c[i]='\0'; for(i=0;i<=Lmin;i++) { c[Lmax-i]=num[Lmin-i]; } return c; } int main() { //输入数据 char a[IndexMax],b[IndexMax]; //补齐后的输入数据 char ap[IndexMax],bp[IndexMax]; //逆序后的输入数据 char a1[IndexMax],b1[IndexMax]; //计算结果 char result[IndexMax]; char * ca,*cb; int p = 0,q = 0; while(scanf("%s %s",&a[p++],&b[q++])!=EOF)//%s发现空白就终止,自动加'\0' { int i,j,la,lb,n,Lmax; la = strlen(a); lb = strlen(b); Lmax = la+1; if(Lmax<lb) Lmax=lb; //第一步:补齐数据 if(la>lb) { cb=puqi(b,la,lb); for(i=0;i<Lmax;i++) { bp[i] = *(cb+i); ap[i] = a[i]; } ap[Lmax]='\0'; bp[Lmax]='\0'; n = strlen(bp); } if(la<lb) { ca=puqi(a,lb,la); for(i=0;i<Lmax;i++) { ap[i] = *(ca+i); bp[i] = b[i]; } ap[Lmax]='\0'; bp[Lmax]='\0'; n = strlen(ap); } if(la == lb) for(i=0;i<Lmax;i++) { ap[i] = a[i]; bp[i] = b[i]; ap[Lmax]='\0'; bp[Lmax]='\0'; n = la; } //第二步:逆序 j=0; for(i=n-1;i>=0;i--) { a1[j] = ap[i]; b1[j] = bp[i]; j=j+1; } a1[j] = '\0'; b1[j] = '\0'; //第三步:加法操作 for(i=0;i<n;i++) { if((a1[i]+b1[i]-'0')>'9') { result[i] = a1[i]+b1[i]-('9'+'1'-'0'); b1[i+1] += ('1'-'0'); } else result[i] = a1[i]+b1[i]-'0'; } //判断最高位是否进位 if((a1[n-1]+b1[n-1]-'0')>'9') { result[n] = '1'; result[n+1] = '\0'; } else result[n] = '\0'; n = strlen(result); //第四步:逆序输出结果 for(i=n-1;i>=0;i--) { printf("%c",result[i]); } printf("\n"); p=0;q=0; } return 0; }
0.0分
0 人评分