现在的位置: 首页 > 综合 > 正文

大数计算_加法

2013年11月16日 ⁄ 综合 ⁄ 共 783字 ⁄ 字号 评论关闭
#include<iostream>
using namespace std;
#include<string>

string x,y,sum;

void add()
{
	int index_x=x.size()-1,index_y=y.size()-1;
		int num=0,tmp=0;
	    while(index_x>=0&&index_y>=0){
		    int num_x=x[index_x]-48,
		 	    num_y=y[index_y]-48;
		        num=num_x+num_y+tmp;
		    string temp;
		    if(num>=10)tmp=num/10;
			else tmp=0;
		    temp+=(num%10+48);
		    sum.insert(0,temp);
		    index_x--;index_y--;
	    }
		if(tmp&&index_x<0&&index_y<0){string temp;temp+=(tmp+48);sum.insert(0,temp);}
		else{
	    while(index_x>=0){
		   string temp;
		   int num=x[index_x]-48+tmp;
		   if(num>=10)tmp=num/10;
		   else tmp=0;
		   temp+=(num%10+48);
		   sum.insert(0,temp);
		   index_x--;
	    }
	    while(index_y>=0){
		   string temp;
		   int num=y[index_y]-48+tmp;
		   if(num>=10)tmp=num/10;
		   else tmp=0;
		   temp+=(num%10+48);
		   sum.insert(0,temp);
		   index_y--;
	    }
		if(tmp){string temp;temp+=(tmp+48);sum.insert(0,temp);}
		}
}

void main()
{
	
	while(cin>>x>>y){
	    add();
	   cout<<sum<<endl;
	}
}

【上篇】
【下篇】

抱歉!评论已关闭.