#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
int j,len1,len2,temp1,temp2,l3,l4,count;
char a[500],b[500];
int a1[500],b1[500],a2[500],b2[500],c1[500],c2[500],swap[500];
while(scanf("%s %s",a,b)!=EOF)
{
temp1=-1;
temp2=-1;
for(j=0;j<500;j++)
c1[j]=0,a1[j]=0,b1[j]=0,a2[j]=0,b2[j]=0,c2[j]=0;
len1=strlen(a);
len2=strlen(b);
for(j=0;j<len1;j++)
{
if(a[j]=='.')
temp1=j;
}
for(j=0;j<len2;j++)
if(b[j]=='.')
temp2=j;
l4=(len1-temp1-1)>(len2-temp2-1)?(len1-temp1-1):(len2-temp2-1);
if(temp1==-1)
{
for(j=0;j<len1;j++)
a1[j]=a[len1-1-j]-'0';
temp1=len1;
}
else
{
for(j=0;j<temp1;j++)
a1[j]=a[temp1-1-j]-'0';
for(j=0;j<len1-temp1-1;j++)
swap[j]=a[temp1+1+j]-'0';
for(j=len1-temp1-1;j<l4;j++)
swap[j]=0;
for(j=0;j<l4;j++)
a2[j]=swap[l4-j-1];
}
if(temp2==-1)
{
for(j=0;j<len2;j++)
b1[j]=b[len2-1-j]-'0';
temp2=len2;
}
else
{
for(j=0;j<temp2;j++)
b1[j]=b[temp2-1-j]-'0';
for(j=0;j<len2-temp2-1;j++)
swap[j]=b[temp2+1+j]-'0';
for(j=len2-temp2-1;j<l4;j++)
swap[j]=0;
for(j=0;j<l4;j++)
b2[j]=swap[l4-j-1];
}
l3=temp1>temp2?temp1:temp2;
for(j=0;j<l4;j++) //l3
{
c2[j]+=a2[j]+b2[j];
if(c2[j]>=10)
{
c2[j]-=10;
c2[j+1]++;
}
}
if(c2[l4]>0)
c1[0]++;
for(j=0;j<l3;j++) //l3
{
c1[j]+=a1[j]+b1[j];
if(c1[j]>=10)
{
c1[j]-=10;
c1[j+1]++;
}
}
if(c1[l3]>0)
l3++;
for(j=l3-1;j>=0;j--)
cout<<c1[j];
count=0;
for(j=0;j<l4;j++)
{
if(c2[j]==0)
count++;
else
break;
}
if(count==l4)
;
else
{
cout<<".";
for(j=l4-1;j>=count;j--)
cout<<c2[j];
}
cout<<endl;
}
}
直接贴代码,自己写的,个人觉得不太好。
注意问题1: 不只是小数,整数也要能算
2:2.500要写成2.5
3: 先把1.5+2.65,先把1.5变成1.50