本文主要是介绍两个大数(包括负数)相加,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
分析:
当这两个大数为正数时,我们可以将字符转化为数字相加,再加进位talg,有进位为1,否则为0;
两个为负数则与正数相似,为一正一负时,我们可以将它们转化为两个正数,用大的减去小的,然后在根据较大的数为正或负,为正时,则结果为正,否则为负。
代码如下:
<span style="font-size:18px;">#include<iostream>
#include<string>
#include<algorithm>using namespace std;</span>
<span style="font-size:18px;">//两个正数相加</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">,isPositive是结果的正负</span><span style="font-size:18px;">
string addPositiveNum(string num1,string num2,bool isPositive)
{int i=num1.size()-1;int j=num2.size()-1;int talg=0;int sum;char pt;string str="";while(i>=0&&j>=0){sum=num1[i]-'0'+(num2[j]-'0')+talg;if(sum>9){talg=1;sum=sum%10;}else{talg=0;}str.append(1,sum+'0');i--;j--;}while(i>=0){if(talg>0){sum=num1[i]-'0'+talg;if(sum>9){talg=1;sum=sum%10;}else{talg=0;}str.append(1,sum+'0');i--;}else{str.append(1,num1[i]);i--;}}while(j>=0){if(talg>0){sum=num2[j]-'0'+talg;if(sum>9){talg=1;sum=sum%10;}else{talg=0;}str.append(1,sum+'0');j--;}else{str.append(1,num2[j]);j--;}}if(talg>0){str.append(1,'1');}if(!isPositive){str.append(1,'-');}reverse(str.begin(),str.end());return str;
}</span>
<span style="font-size:18px;">//两个正数相减</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">,isPositive是结果的正负</span><span style="font-size:18px;">
string minusPositiveNum(string num1,string num2,bool isPositive)
{int i=num1.size()-1;int j=num2.size()-1;string str;if(i<j||(i==j&&num1.compare(num2)<0)){str=num2;num2=num1;num1=str;}int talg=0;int sum;str="";while(i>=0&&j>=0){sum=num1[i]-'0'-(num2[j]-'0')+talg;if(sum<0){talg=-1;sum=sum+10;}else{talg=0;}str.append(1,sum+'0');i--;j--;}while(i>=0){if(talg<0){sum=num1[i]-'0'+talg;if(sum<0){talg=-1;sum=10+sum;}else{talg=0;}str.append(1,sum+'0');i--;}else{str.append(1,num1[i]);i--;}}while(j>=0){if(talg<0){sum=num2[j]-'0'+talg;if(sum<0){talg=-1;sum=10+sum;}else{talg=0;}str.append(1,sum+'0');j--;}else{str.append(1,num2[j]);j--;}}if(!isPositive){str.append(1,'-');}reverse(str.begin(),str.end());return str;
}
string add(string num1,string num2)
{if(num1.size()<=0||num2.size()<=0)return "";bool isPositive=true;string result;if(num1[0]=='-'&&num2[0]=='-'){</span>
<span style="font-size:18px;"> //为两个负数相加,去掉‘-’号result=addPositiveNum(num1.substr(1,num1.size()-1),num2.substr(1,num2.size()-1),false);}else if(num1[0]!='-'&&num2[0]!='-'){</span>
<span style="font-size:18px;"> //为两个正数相加,result=addPositiveNum(num1,num2,true);}else{</span>
<span style="font-size:18px;"> //为一正一负string str1;string str2;</span>
<span style="font-size:18px;"> //去掉其中的‘-’号if(num1[0]=='-'){str1=num1.substr(1,num1.size()-1);str2=num2;}else{str1=num1;str2=num2.substr(1,num2.size()-1);}</span>
<span style="font-size:18px;"> //判断结果的正负int t=str1.size()-str2.size();if(t==0){int t1=str1.compare(str2);if(t1<0)t=-1;else if(t1>0)t=1;}</span>
<span style="font-size:18px;"> //两个数相等if(t==0){return "0";}</span>
<span style="font-size:18px;"> //两个正数相减if(num1[0]=='-'){if(t<0){result=minusPositiveNum(str1,str2,true);}else{result=minusPositiveNum(str1,str2,false);}}else{if(t<0){result=minusPositiveNum(str1,str2,false);}else{result=minusPositiveNum(str1,str2,true);}}}return result;
}int main()
{cout<<add("45430543","-10")<<endl;return 0;
}</span>
这篇关于两个大数(包括负数)相加的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!