九度教程第63题
C语言源码:
[cpp]
#include<stdio.h>
#include<string.h>
#define maxsize 10000
typedef struct big
{
int in[maxsize],fl[maxsize];
int insize,flsize;
}big;
big a,b;
int addfl()
{
int i,n,k;
n=a.flsize>b.flsize?a.flsize:b.flsize;
i=n-1;
while(i>0)
{
a.fl[i-1]+=(a.fl[i]+b.fl[i])/10;
a.fl[i]=(a.fl[i]+b.fl[i])%10;
i--;
}
k=(a.fl[i]+b.fl[i])/10;
a.fl[i]=(a.fl[i]+b.fl[i])%10;
a.flsize=n;
return k;
}
void addin(int k)
{
int i,n;
n=a.insize>b.insize?a.insize:b.insize;
for(i=0;i<n;i++)
{
if(i==0)
{
a.in[i+1]+=(a.in[i]+b.in[i]+k)/10;
a.in[i]=(a.in[i]+b.in[i]+k)%10;
}
else
{
a.in[i+1]+=(a.in[i]+b.in[i])/10;
a.in[i]=(a.in[i]+b.in[i])%10;
}
}
if(a.in[n]!=0)
n++;
a.insize=n;
}
int main()
{
char s1[maxsize],s2[maxsize];
int n,i,k,lens1,lens2,point1,point2,j;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
for(i=0;i<maxsize;i++)
{
a.in[i]=0;
a.fl[i]=0;
b.in[i]=0;
b.fl[i]=0;
}
getchar();
scanf("%s",s1);
getchar();
scanf("%s",s2);
getchar();
lens1=(int)strlen(s1);
lens2=(int)strlen(s2);
i=0;
while(s1[i]!='.')
i++;
point1=i;
i=0;
while(s2[i]!='.')
i++;
point2=i;
for(i=point1-1;i>=0;i--)
a.in[point1-1-i]=s1[i]-'0';
for(i=point1+1;i<lens1;i++)
a.fl[i-1-point1]=s1[i]-'0';
a.insize=point1;
a.flsize=lens1-point1-1;
for(i=point2-1;i>=0;i--)
b.in[point2-1-i]=s2[i]-'0';
for(i=point2+1;i<lens2;i++)
b.fl[i-1-point2]=s2[i]-'0';
b.insize=point2;
b.flsize=lens2-point2-1;
k=addfl();
addin(k);
j=a.insize;
while(j&&a.in[j-1]==0)
j--;
if(j<=0)
printf("0");
else www.zzzyk.com
for(i=j-1;i>=0;i--)
printf("%d",a.in[i]);
printf(".");
j=a.flsize-1;
while(j>=0&&a.fl[j]==0)
j--;
if(j>=0)
{
for(i=0;i<=j;i++)
printf("%d",a.fl[i]);
}
printf("\n");
}
}
}
补充:软件开发 , C++ ,