2092 诡异的邮件

来源:互联网 发布:unity3d ui 动画 编辑:程序博客网 时间:2024/05/16 18:47
描述


最近,lcl总会遇到一些怪事,比如邮箱里每天都会多出一些诡异的邮件,其中,有一组邮件的内容是这样的:能帮我计算一下A+B么?邮件的后面会附上A和B两个正整数。

一开始,lcl觉得很无聊,因为A和B都很小,计算起来很简单,但逐渐的,给出的A和B变得很大,她开始计算不了了,于是懒惰的她又找到了你~快用你的程序帮她解决问题吧~

注意,A和B的值都可能非常大,但保证它们的长度不超过100.

输入

第一行包括一个整数T,表示共有T组数据.
第2~2*T+1行:
对于每组数据,占两行,包含两个超长正整数A,B。(长度<100)

输出

对于每组数据,输出一行,格式:A + B = 计算结果。

样例输入
2
2
112233445566778899 
998877665544332211
样例输出
1 + 2 = 3
112233445566778899 + 998877665544332211 = 1111111111111111110
高精度加法
#include <stdio.h>#include <string.h>int main(){void add(char a[],char b[],char result[]);void plus(char a[],char b[],char result[]);char a[1000]={0},b[1000]={0},temp1[1000]={0},temp2[1000]={0};char result[1000]={0},zancun[1000]={0};int m,i,n,t;int number;scanf("%d",&number);for(t=1;t<=number;t++){scanf("%s%s",a,b);printf("%s + %s = ",a,b);if(a[0]=='-'&&b[0]=='-'){m=0;n=0;for(i=1;a[i]!='\0';i++)temp1[m++]=a[i];temp1[m]='\0';for(i=1;b[i]!='\0';i++)temp2[n++]=b[i];temp2[n]='\0';add(temp1,temp2,result);printf("-%s\n",result);goto abc;}if(a[0]!='-'&&b[0]!='-'){add(a,b,result);printf("%s\n",result);goto abc;}if(a[0]=='-'&&b[0]!='-'){   m=0;n=0;for(i=1;a[i]!='\0';i++)temp2[m++]=a[i];temp2[m]='\0';for(i=0;b[i]!='\0';i++)temp1[n++]=b[i];temp1[n]='\0';if(m<n){plus(temp1,temp2,result);printf("%s\n",result);goto abc;}if(m>n){   strcpy(zancun,temp2);strcpy(temp2,temp1);strcpy(temp1,zancun);plus(temp1,temp2,result);printf("-%s\n",result);goto abc;}if(m==n){  if(strcmp(temp1,temp2)==0){printf("0\n");goto abc;}if(strcmp(temp1,temp2)<0){strcpy(zancun,temp2);strcpy(temp2,temp1);strcpy(temp1,zancun);plus(temp1,temp2,result);printf("-%s\n",result);goto abc;}if(strcmp(temp1,temp2)>0){ plus(temp1,temp2,result);printf("%s\n",result);goto abc;}}}if(a[0]!='-'&&b[0]=='-'){ m=0;n=0;for(i=0;a[i]!='\0';i++)temp1[m++]=a[i];temp1[m]='\0';for(i=1;b[i]!='\0';i++)temp2[n++]=b[i];temp2[n]='\0';if(m>n){plus(temp1,temp2,result);printf("%s\n",result);goto abc;}if(m<n){  strcpy(zancun,temp2);strcpy(temp2,temp1);strcpy(temp1,zancun);plus(temp1,temp2,result);printf("-%s\n",result);goto abc;}if(m==n){  if(strcmp(temp1,temp2)==0){ printf("0\n");goto abc;}if(strcmp(temp1,temp2)<0){strcpy(zancun,temp2);strcpy(temp2,temp1);strcpy(temp1,zancun);plus(temp1,temp2,result);printf("-%s\n",result);goto abc;}if(strcmp(temp1,temp2)>0){ plus(temp1,temp2,result);printf("%s\n",result);goto abc;}}}abc:   memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(result,0,sizeof(result));memset(temp1,0,sizeof(temp1));memset(temp2,0,sizeof(temp2));memset(zancun,0,sizeof(zancun));}return 0;}void add(char a[],char b[],char result[]){int la,lb,lresult;int s,c,t;int i;char temp;la=strlen(a)-1;lb=strlen(b)-1;c=0;t=0;while(la >= 0 || lb >= 0){if(la < 0) s=b[lb--]-48;else if(lb < 0) s=a[la--]-48;else s=a[la--]-48+b[lb--]-48;result[t++]=(s+c)%10+48;c=(s+c)/10;}if(c != 0) {result[t]=c+48;lresult=t;}else lresult=t-1;for(i=0;i <= lresult/2;i++){temp=result[i];result[i]=result[lresult-i];result[lresult-i]=temp;}}void plus(char temp1[],char temp2[],char result[]){int la,lb,lresult;int s,c,t=0;int i,j;char temp[1000]={0};char temp3[1000]={0},temp4[1000];int length;memset(temp4,'0',sizeof(temp4));la=strlen(temp1);/*����大��������*/lb=strlen(temp2);c=0;s=0;for(i=la-1;i>=0;i--)temp3[c++]=temp1[i];for(i=lb-1;i>=0;i--)temp4[s++]=temp2[i];temp[la]='0';for(i=0;i<la;i++){if(temp3[i]<temp4[i]){temp[i]=temp3[i]+10-temp4[i]+'0';temp3[i+1]--;}if(temp3[i]==temp4[i])temp[i]='0';if(temp3[i]>temp4[i])temp[i]=temp3[i]-temp4[i]+'0';}for(j=i;j>0;j--){  if(temp[j]=='0')continue;elsebreak;}for(i=j;i>=0;i--)result[t++]=temp[i];result[t]='\0';}