实数加法

在这里插入图片描述用string 来存储小数点前面的数和后面的数再分别进行加法

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string a,b,x1,x2,y1,y2;
	cin>>a>>b;
	x1=a.substr(0,a.find('.'));y1=a.substr(a.find('.')+1,a.length()-a.find('.')-1);
	x2=b.substr(0,b.find('.'));y2=b.substr(b.find('.')+1,b.length()-b.find('.')-1);
	while(y1.length()!=y2.length())
	{
		if(y1.length()>y2.length())y2=y2+'0';
		else y1=y1+'0';
	}
		while(x1.length()!=x2.length())
	{
		if(x1.length()>x2.length())x2='0'+x2;
		else x1='0'+x1;
	}
	int carry=0;string ans2="";
	for(int i=y1.length()-1;i>=0;i--)
	{
		int t=y1[i]-'0'+y2[i]-'0'+carry;
		ans2+=(t%10)+'0';
		carry=t/10;
	}
	string ans1="";
		for(int i=x1.length()-1;i>=0;i--)
	{
		int t=x1[i]-'0'+x2[i]-'0'+carry;
		ans1+=(t%10)+'0';
		carry=t/10;
	}
	if(carry)ans1='1'+ans1;
	for(int i=ans1.length()-1;i>=0;i--)cout<<ans1[i];
	cout<<".";
	for(int i=ans2.length()-1;i>=0;i--)cout<<ans2[i];
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值