#include<iostream> #include<string> using namespace std; //记录进位,0表示没进位,1表示进位 int tag=0; //记录结果正负,0表示负,1表示正 int plus=1; //整数部分的相加intNum1.length()>=intNum2.length() string addInt(string intNum1,string intNum2) { string intSum=""; int sizeDif=intNum1.length()-intNum2.length(); int i=intNum1.length()-1; for(;i>=sizeDif;i--) { intSum+=(char)((intNum1[i]-48+intNum2[i-sizeDif]-48+tag)%10+48); tag=(intNum1[i]-48+intNum2[i-sizeDif]-48+tag)/10; } i=sizeDif-1; for(;i>=0;i--) { intSum+=(char)((intNum1[i]-48+tag)%10+48); tag=(intNum1[i]-48+tag)/10; } if(tag) { intSum+='1'; } return intSum; } //整数部分的相减 intNum1.length()>=intNum2.length() string subInt(string intNum1,string intNum2) { string intSub=""; int sizeDif=intNum1.length()-intNum2.length(); int i=intNum1.length()-1; for(;i>=sizeDif;i--) { if(intNum1[i]-tag>=intNum2[i-sizeDif]) { intSub+=(char)(intNum1[i]-intNum2[i-sizeDif]-tag+48); tag=0; } else { intSub+=(char)(intNum1[i]+10-intNum2[i-sizeDif]-tag+48); tag=1; } } i=sizeDif-1; for(;i>=0;i--) { if(intNum1[i]-tag>=0) { intSub+=(char)(intNum1[i]-tag); tag=0; } else { intSub+=(char)(intNum1[i]+10-tag); tag=1; } } return intSub; } //小数部分的相加poiNum1.length()>=poiNum2.length() string addPoi(string poiNum1,string poiNum2) { string poiSum=""; int sizeDif=poiNum1.length()-poiNum2.length(); int i=poiNum1.length()-1; for(;i>=poiNum2.length();i--) { poiSum+=poiNum1[i]; } i=poiNum2.length()-1; for(;i>=0;i--) { poiSum+=(char)((poiNum1[i]-48+poiNum2[i]-48+tag)%10+48); tag=(poiNum1[i]-48+poiNum2[i]-48+tag)/10; } return poiSum; } //小数部分的相减poiNum1-poiNum2 string subPoi(string poiNum1,string poiNum2) { string poiSub=""; int sizeDif=poiNum1.length()-poiNum2.length(); if(sizeDif>=0) { int i=poiNum1.length()-1; for(;i>=poiNum2.length();i--) { poiSub+=poiNum1[i]; } i=poiNum2.length()-1; for(;i>=0;i--) { if(poiNum1[i]-tag>=poiNum2[i]) { poiSub+=(char)(poiNum1[i]-poiNum2[i]-tag+48); tag=0; } else { poiSub+=(char)(poiNum1[i]+10-poiNum2[i]-tag+48); tag=1; } } } else { sizeDif=-sizeDif; int i=poiNum2.length()-1; for(;i>poiNum1.length()-1;i--) { poiSub+=(char)('0'-poiNum2[i]+10-tag+48); tag=1; } i=poiNum1.length()-1; for(;i>=0;i--) { if(poiNum1[i]-tag>=poiNum2[i]) { poiSub+=(char)(poiNum1[i]-poiNum2[i]-tag+48); tag=0; } else { poiSub+=(char)(poiNum1[i]+10-poiNum2[i]-tag+48); tag=1; } } } return poiSub; } //比较绝对值 int compAbs(string intNum1,string poiNum1,string intNum2,string poiNum2) { if(intNum1.length()>intNum2.length()) { return 1; } else if(intNum1.length()==intNum2.length()) { if(intNum1>intNum2) { return 1; } else if(intNum1==intNum2) { if(poiNum1>=poiNum1) { return 1; } } } return 0; } //找到小数点的索引 int findPoi(string &num) { int i=num.length()-1; for(;i>=0;i--) { if(num[i]=='.') return i; } num+=".0"; return findPoi(num); } //提取数据的中间(索引index1到index2之间)数据 string sepNum(string num,int index1,int index2) { string sepNo=""; int i=index1; for(;i<=index2;i++) { sepNo+=num[i]; } return sepNo; } //显示结果 int showMes(string resultNum) { int i=resultNum.length()-1; for(;i>=0;i--) { cout<<resultNum[i]; } cout<<endl; return 0; } //将结果两头的多余的0去掉 /*void delZero(string &numSum) { }*/ //两数相加 int add(string num1,string num2) { int poiIndex1; int poiIndex2; string intNum1; string intNum2; string poiNum1; string poiNum2; string numSum; if(num1[0]!='-'&&num2[0]!='-') //两数都为正 { poiIndex1=findPoi(num1); poiIndex2=findPoi(num2); intNum1=sepNum(num1,0,poiIndex1-1); intNum2=sepNum(num2,0,poiIndex2-1); poiNum1=sepNum(num1,poiIndex1+1,num1.length()-1); poiNum2=sepNum(num2,poiIndex2+1,num2.length()-1); if(poiNum1.length()>poiNum2.length()) { numSum+=addPoi(poiNum1,poiNum2); } else { numSum+=addPoi(poiNum2,poiNum1); } numSum+='.'; if(intNum1.length()>intNum2.length()) { numSum+=addInt(intNum1,intNum2); } else { numSum+=addInt(intNum2,intNum1); } plus=1; } else if('-'==num1[0]&&'-'==num2[0]) //两数都为负 { poiIndex1=findPoi(num1); poiIndex2=findPoi(num2); intNum1=sepNum(num1,1,poiIndex1-1); intNum2=sepNum(num2,1,poiIndex2-1); poiNum1=sepNum(num1,poiIndex1+1,num1.length()-1); poiNum2=sepNum(num2,poiIndex2+1,num2.length()-1); if(poiNum1.length()>=poiNum2.length()) { numSum+=addPoi(poiNum1,poiNum2); } else { numSum+=addPoi(poiNum2,poiNum1); } numSum+='.'; if(intNum1.length()>=intNum2.length()) { numSum+=addInt(intNum1,intNum2); } else { numSum+=addInt(intNum2,intNum1); } plus=0; } else if('-'==num1[0]&&'-'!=num2[0]) { poiIndex1=findPoi(num1); poiIndex2=findPoi(num2); intNum1=sepNum(num1,1,poiIndex1-1); intNum2=sepNum(num2,0,poiIndex2-1); poiNum1=sepNum(num1,poiIndex1+1,num1.length()-1); poiNum2=sepNum(num2,poiIndex2+1,num2.length()-1); if(compAbs(intNum1,poiNum1,intNum2,poiNum2)) { numSum+=subPoi(poiNum1,poiNum2); numSum+='.'; numSum+=subInt(intNum1,intNum2); plus=compAbs(intNum1,poiNum1,intNum2,poiNum2)-1; } else { numSum+=subPoi(poiNum2,poiNum1); numSum+='.'; numSum+=subInt(intNum2,intNum1); plus=compAbs(intNum1,poiNum1,intNum2,poiNum2)+1; } } else if('-'!=num1[0]&&'-'==num2[0]) { poiIndex1=findPoi(num1); poiIndex2=findPoi(num2); intNum1=sepNum(num1,0,poiIndex1-1); intNum2=sepNum(num2,1,poiIndex2-1); poiNum1=sepNum(num1,poiIndex1+1,num1.length()-1); poiNum2=sepNum(num2,poiIndex2+1,num2.length()-1); if(compAbs(intNum1,poiNum1,intNum2,poiNum2)) { numSum+=subPoi(poiNum1,poiNum2); numSum+='.'; numSum+=subInt(intNum1,intNum2); plus=compAbs(intNum1,poiNum1,intNum2,poiNum2); } else { numSum+=subPoi(poiNum2,poiNum1); numSum+='.'; numSum+=subInt(intNum2,intNum1); plus=compAbs(intNum1,poiNum1,intNum2,poiNum2); } } //delZero(numSum); if(!plus) { numSum+='-'; } showMes(numSum); return 0; } int main() { string num1; string num2; cout<<"请输入要求和的两个数:"<<endl; cin>>num1>>num2; add(num1,num2); return 0; }