2013.6.17大数计算之浮点数

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值