加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

学习笔记——大数加法

发布时间:2020-12-14 03:58:45 所属栏目:大数据 来源:网络整理
导读:2013.10.2 晚上十二点 问题: 大数加法(包括小数的加法) 问题描述: 实现大数(包括小数)相加 代码: ? #include iostream #include algorithm #include string #include vector ? using namespace std; ? vectorstringsplitEx( const string src,strings

2013.10.2 晚上十二点

问题:大数加法(包括小数的加法)

问题描述:实现大数(包括小数)相加

代码:

?

#include <iostream>

#include <algorithm>

#include <string>

#include <vector>

?

using namespace std;

?

vector<string>splitEx(const string& src,stringseparate_character)??

{??

??? vector<string> strs;??

?

??? intseparate_characterLen = separate_character.size();

??? intlastPosition = 0,index = -1;??

??? while (-1 !=(index = src.find(separate_character,lastPosition)))??

??? {??

??????? strs.push_back(src.substr(lastPosition,index- lastPosition));??

??????? lastPosition = index +separate_characterLen;??

??? }??

??? string lastString =src.substr(lastPosition);

??? if(!lastString.empty())? ?

??????? strs.push_back(lastString);

??? returnstrs;??

}??

?

stringaddNumDecimals(string decimal1,string decimal2)

{

??? intiLen1=decimal1.length();

??? intiLen2=decimal2.length();

?

??? intiLen=iLen1>iLen2?iLen1:iLen2;

?

??? if (iLen> iLen1) decimal1.append(iLen - iLen1,'0');??

??? if (iLen> iLen2) decimal2.append(iLen - iLen2,'0');?

?

??? reverse(decimal1.begin(),decimal1.end());?

??? reverse(decimal2.begin(),decimal2.end());

?

??? int tmp=0;

??? int i=0;

??? string strRes;

?

??? while(i<iLen)

??? {

??????? if(decimal1[i]<'0' || decimal1[i]>'9' || decimal2[i]<'0'||decimal2[i]>'9')

??????? {

??????????? cout<<"Illegalcharacter"<<endl;

??????????? system("pause");

??????????? exit(0);

??????? }

?

??????? if(decimal1[i]-0x30+decimal2[i]+tmp>'9')

??????? {

??????????? strRes.append(1,decimal1[i]+decimal2[i]-'9'-1+tmp);

??????????? tmp=1;

??????? }

??????? else

??????? {

??????????? strRes.append(1,decimal1[i]+decimal2[i]-'0'+tmp);

??????????? tmp=0;

??????? }

??????? ++i;

??? }

???

??? if (tmp==1)

??? {

??????? strRes.append(1,'1');

??? }else

??? {

??????? strRes.append(1,'0');

??? }

?

??? reverse(strRes.begin(),strRes.end());

??? returnstrRes;

?

}

?

stringaddNumInteger(string integer1,string integer2,charflag)

{

??? intiLen1=integer1.length();

??? intiLen2=integer2.length();

?

??? intiLen=iLen1>iLen2?iLen1:iLen2;

?

??? reverse(integer1.begin(),integer1.end());?

??? reverse(integer2.begin(),integer2.end());

?

??? if (iLen> iLen1) integer1.append(iLen - iLen1,'0');??

??? if (iLen> iLen2) integer2.append(iLen - iLen2,'0');?

?

??? inttmp=flag-'0';

??? int i=0;

??? string strRes;

?

??? while(i<iLen)

??? {

??????? if(integer1[i]<'0' || integer1[i]>'9' || integer2[i]<'0'||integer2[i]>'9')

??????? {

??????????? cout<<"Illegalcharacter"<<endl;

??????????? system("pause");

??????????? exit(0);

??????? }

?

??????? if(integer1[i]-0x30+integer2[i]+tmp>'9')

??????? {

??????????? strRes.append(1,integer1[i]+integer2[i]-'9'-1+tmp);

??????????? tmp=1;

??????? }

??????? else

??????? {

??????????? strRes.append(1,integer1[i]+integer2[i]-'0'+tmp);

??????????? tmp=0;

??????? }

??????? ++i;

??? }

?

??? if (tmp==1)

??? {

??????? strRes.append(1,'1');

??? }

?

??? reverse(strRes.begin(),strRes.end());

??? returnstrRes;

?

}

?

int run()

{

??? string num1="1123213214#243242343242.9872";

??? string num2="382137218478923732902440441.12345567";

???

??? string split_char=".";

??? vector<string>num1_split=splitEx(num1,split_char);

??? vector<string>num2_split=splitEx(num2,split_char);

??? if(num1_split.size()>2 || num2_split.size()>2)

??? {

??????? cout<<"error!"<<endl;

??????? system("pause");

??????? return-1;

??? }

?

??? if(num1_split.size()==2 && num2_split.size()==2)??

??? {

??????? string res1 =addNumDecimals(num1_split[1],num2_split[1]);

??????? charflag = res1[0];

??????? string res2 =addNumInteger(num1_split[0],num2_split[0],flag);

??????? string res=res2+".";

??????? for (int i=1;i<res1.length();++i)

??????? {

??????????? res+=res1[i];

??????? }

?

??????? cout<<res<<endl;

??????? system("pause");

??????? return0;

??? }

?

??? if(num1_split.size()==2 && num2_split.size()==1)??

??? {

??????? string res1 =addNumInteger(num1_split[0],'0');

??????? string res=res1+"."+num1_split[1];

??????? cout<<res<<endl;

??????? system("pause");

??????? return0;

??? }

?

??? if(num1_split.size()==1 && num2_split.size()==2)??

??? {

??????? string res1 =addNumInteger(num1_split[0],'0');

??????? string res=res1+"."+num2_split[1];

??????? cout<<res<<endl;

??????? system("pause");

??????? return0;

??? }

?

??? if(num1_split.size()==1 && num2_split.size()==1)??

??? {

??????? string res =addNumInteger(num1_split[0],'0');

??????? cout<<res<<endl;

??????? system("pause");

??????? return0;

??? }

?

??? return 0;

}

?

?

}

?

问题关键点:整数部分的加法和小数部分的加法方法不一样,小数部分是先将缺少部分补全再反转相加,整数部分是先反转再将缺少部分补全

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读