#include <stdio.h>
#include <string.h>
void Swap(char &a,char &b)
{
char c =a;
a =b;
b = c;
}
void ReverseString(char *s)
{
int i=0;
int j = strlen(s)-1;
while (i<j)
{
Swap(s[i],s[j]);
i++;
j--;
}
}
void AddString(char *A,char *B,char *C)
{
char AA[100];
char BB[100];
char CC[100];
strcpy(AA,A);
strcpy(BB,B);
ReverseString(AA);
ReverseString(BB);
int posAA=-1;
int posBB=-1;
char *p = strchr(AA,'.');
if (p!=NULL)
{
posAA = p-AA;
}
char *q = strchr(BB,'.');
if (q!=NULL)
{
posBB = q-BB;
}
int lenCC=0;
int len_float_min = posAA<posBB ? posAA : posBB;
if (p==NULL || q==NULL)
{
len_float_min=0;
}
int i=0,j=0;
int carry = 0;
while (posAA-i>len_float_min)
{
int tmp = carry+AA[i]-'0';
carry = tmp/10;
CC[lenCC++] = tmp%10+'0';
i++;
}
while (posBB-j>len_float_min)
{
int tmp = carry+BB[j]-'0';
carry = tmp/10;
CC[lenCC++] = tmp%10+'0';
j++;
}
while (i<posAA && j<posBB)
{
int tmp = carry + AA[i]-'0'+BB[j]-'0';
carry = tmp/10;
CC[lenCC++] = tmp%10+'0';
i++;
j++;
}
CC[lenCC++] = '.';
i++;
j++;
if (p==NULL)
{
i=0;
}
if (q==NULL)
{
j=0;
}
while (i<strlen(AA) && j<strlen(BB))
{
int tmp = carry + AA[i]-'0'+BB[j]-'0';
carry = tmp/10;
CC[lenCC++] = tmp%10+'0';
i++;
j++;
}
while (i<strlen(AA))
{
int tmp = carry+AA[i]-'0';
carry = tmp/10;
CC[lenCC++] = tmp%10+'0';
i++;
}
while (j<strlen(BB))
{
int tmp = carry+BB[j]-'0';
carry = tmp/10;
CC[lenCC++] = tmp%10+'0';
j++;
}
if (carry)
{
CC[lenCC++]=carry+'0';
}
CC[lenCC]=0;
ReverseString(CC);
int cnt=0;
for (i=0;i<strlen(CC);++i)
{
if (CC[i]!='0') break;
cnt++;
}
for (i=cnt;i<strlen(CC)+1;++i)
{
CC[i-cnt] = CC[i];
}
cnt=0;
for (i=strlen(CC)-1;i>=0;--i)
{
if (CC[i]!='0') break;
cnt++;
}
CC[strlen(CC)-cnt]=0;
char *r = strchr(CC,'.');
int pos = r-CC;
if (pos==strlen(CC)-1)
{
CC[strlen(CC)-1]=0;
}
strcpy(C,CC);
}
int main()
{
char s[300];
char s1[100];
char s2[100];
char s3[100];
// strcpy(s1,"1111.222");
// strcpy(s2,"2222.333477777");
scanf("%s",s);
// strcpy(s,"1.2+3");
int pos=0;
char *p = strchr(s,'+');
pos = p-s;
int i=0;
for (i=0;i<pos;++i)
{
s1[i]=s[i];
}
s1[pos]=0;
for (i=pos+1;i<strlen(s);++i)
{
s2[i-pos-1] = s[i];
}
s2[strlen(s)-pos-1]=0;
AddString(s1,s2,s3);
printf("%s\n",s3);
return 0;
}
2013.6.17大数计算之浮点数
最新推荐文章于 2022-01-26 01:35:31 发布