大数加法
//大数加法#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
go: int i=0,j=0,cn1,cn2,num=0;
int *a=new int[100];
int *b=new int[100];
int answer[100]={0};
char *numb1=new char[100];
char *numb2=new char[100];
int sum(int x,int y);
cin>>numb1;
cn1=strlen(numb1);
cin>>numb2;
cn2=strlen(numb2);
for(i=0;i<cn1;i++)
{
a[i]=numb1[i]-'0';
}
for(i=0,j=cn1-1;i<j;i++,j--)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i=0;i<cn1;i++)cout<<a[i];
cout<<endl;
for(i=0;i<cn2;i++)
{
b[i]=numb2[i]-'0';
}
for(i=0,j=cn2-1;i<j;i++,j--)
{
int t=b[i];
b[i]=b[j];
b[j]=t;
}
for(i=0;i<cn2;i++)cout<<b[i];
cout<<endl;
cout<<setw(cn2+1) <<numb1<<endl;
cout<<"+"<<setw(cn1-1) <<numb2<<endl;
cout<<"-----------------------------"<<"\n"<<"=";
if(cn1>=cn2)
{
for(i=0;i<cn2;i++)
{
if((answer[i]+sum(a[i],b[i]))>=10)
{
answer[i+1]=1;
answer[i]=(answer[i]+sum(a[i],b[i]))%10;
}
else
{
answer[i]=answer[i]+sum(a[i],b[i]);
}
}
for(i=cn2;i<cn1;i++)
{
if((answer[i]+a[i])>=10)
{
answer[i+1]=1;
answer[i]=(answer[i]+a[i])%10;
}
else
{
answer[i]+=a[i];
}
}
if(cn1==cn2)cn1++;
for(i=cn1-1;i>=0;i--)
{
cout<<answer[i];
}
cout<<endl;
}
if(cn1<cn2)
{
for(i=0;i<cn1;i++)
{
if((answer[i]+sum(a[i],b[i]))>=10)
{
answer[i+1]=1;
answer[i]=(answer[i]+sum(a[i],b[i]))%10;
}
else
{
answer[i]=answer[i]+sum(a[i],b[i]);
}
}
for(i=cn1;i<cn2;i++)
{
if((answer[i]+b[i])>=10)
{
answer[i+1]=1;
answer[i]=(answer[i]+b[j])%10;
}
else
{
answer[i]+=b[i];
}
}
for(i=cn2-1;i>=0;i--)
{
cout<<answer[i];
}
cout<<endl;
}
system("pause");
goto go;
return 0;
}
int sum(int x,int y)
{
return x+y;
}