功能实现:超长整数加法,按照书上写的,但是不能运行,求大神指教
#include<iostream>#include<Cstring>
using namespace std;
char *ladd(char *s1,char *s2)
{
int n1,n2,n;
char *res,c=0;
n1=strlen(s1);
n2=strlen(s2);
if(n1>n2)
n=n1;
else
n=n2;
for(int i=n+1;i>=0;i--)
{
if(i>n-n1)
res[i]=s1[i-n-1+n1];
else
res[i]='0';
}
for(int i=n;i>=0;i--)
{
char tchar;
if(i>n-n2)
tchar=res[i]-'0'+s2[i-n+n2-1]-'0'+c;
else
tchar=res[i]-'0'+c;
if(tchar>9)
c=1;
else
c=0;
if(c>0)
res[i]=tchar-10+'0';
else
res[i]=tchar+'0';
}
return res;
}
int main()
{
char num1[100],num2[100],*num;
cin>>num1>>num2;
num=ladd(num1,num2);
cout<<num1<<"+"<<num2<<"="<<num<<endl;
delete []num;
return 0;
}