大整形数据 存储 相加

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	int tempA,tempB,temp,m,jw;
	int lenA,lenB,lenC;
	int i,j,k;
	int n,index;// the number of cases
	char a[1001],b[1001],c[1001];
	//printf("Please input two large numbers:\n");
	scanf("%d",&n);
	for(index=1;index<=n;index++){
		scanf("%s",a);
		scanf("%s",b);
	
		lenA=strlen(a);
		lenB=strlen(b);
		i=lenA-1;
		j=lenB-1;
		k=0;
		jw=0;
		while(i>=0&&j>=0){
			tempA=a[i]-'0';
			tempB=b[j]-'0';
			m=tempA+tempB+jw;
			jw=m/10;
			temp=m%10;
			c[k]=temp+'0';
			i--;
			j--;
			k++;
		}
		if(i>=0)
			while(i>=0){
				tempA=a[i]-'0';
				m=tempA+jw;
				jw=m/10;
				temp=m%10+'0';
				c[k]=temp;
				i--;
				k++;
			}
		if(j>=0)
			while(j>=0){
				tempB=a[j]-'0';
				m=tempB+jw;
				jw=m/10;
				temp=m%10+'0';
				c[k]=temp;
				j--;
				k++;
			}
		if(jw>0){
			c[k]='1';
		}
		printf("Case %d:\n",index);
		printf("%s+%s=",a,b);
		//printf("%s\n",c);
		//printf("%d\n",strlen(c));
		for(k=strlen(c)-1;k>=0;k--){
			printf("%c",c[k]);
		}
		printf("\n\n");
	}
}

posted on 2011-10-21 15:58  一指流砂  阅读(440)  评论(0编辑  收藏  举报

导航