#include <iostream>
#include <string>
using namespace std;
// 输入参数:
// char * pcAddend:加数
// char * pcAugend:被加数
// char * pcAddResult:加法结果
void AddLongInteger(char * pcAddend, char * pcAugend, char * pcAddResult)
{
int length1 = strlen(pcAddend);
int length2 = strlen(pcAugend);
int carry_bit =0; //记录有无进位
int a1 = 0;
int a2 = 0;
int i = length1-1;
int j = length2-1;
string s="";
for(;i>=0||j>=0;i--,j--)
{
if(i>=0)
{
a1 = pcAddend[i]-'0';
}
else
{
a1 = 0;
}
if(j>=0)
{
a2 = pcAugend[j]-'0';
}
else
{
a2 = 0;
}
int sum = a1+a2+carry_bit;
if(sum>=10)
{
carry_bit = 1;
sum -= 10;
}
else
{
carry_bit = 0;
}
char temp = sum +'0';
// cout<<temp<<endl;
s = temp+s;
}
if(carry_bit==1)
s = '1'+s;
// cout<<s<<endl;
pcAddResult = new char[s.length()+1];
strcpy(pcAddResult,s.c_str()); //将字符串的值拷贝给字符数组
pcAddResult[s.length()] ='\0';
cout<<pcAddResult<<endl;
}
int main(){
string s1,s2;
cin>>s1;
cin>>s2;
char *a,*b,*c;
a = new char[s1.length()+1];
strcpy(a,s1.c_str());
a[s1.length()] ='\0';
b = new char[s2.length()+1];
strcpy(b,s2.c_str());
b[s2.length()] ='\0';
AddLongInteger(a,b,c);
// cout<<c<<endl;
// cout<<a<<endl;
}
题是做对了,但是感觉代码比较乱,请多提意见!