在这里插入代码片//完整代码
#include<iostream>
#include<algorithm>
#include<vector>
#include<sstream>
using namespace std;
string zsxj(string a,string b)
{
vector<int> v;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
// cout<<"a="<<a<<" b="<<b<<endl;
int la=a.length();
int lb=b.length();
int l=lb;
if(la>lb)
l=la;
for(int i=0;i<=l+2;i++)
v.push_back(0);
for(int i=0;i<a.length();i++)
v[i]+=a[i]-'0';
for(int i=0;i<b.length();i++)
v[i]+=b[i]-'0';
for(int i=0;i<l+1;i++)
{
if(v[i]>=10)
{
v[i+1]++;
v[i]-=10;
}
}
string zfc;
int i=l+2;
// cout<<"zheli i="<<i<<endl;
/*for(int j=0;j<=i;j++)
cout<<v[j]<<" ";
cout<<endl;*/
while(v[i]==0)
i--;
// cout<<"i="<<i<<endl;
for(int j=i;j>=0;j--)
zfc+=v[j]+'0';
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
if(zfc.length()==0)
zfc="0";
cout<<"加法运算数为:\n";
cout<<a<<" "<<b<<endl;
cout<<"加法结果="<<zfc<<endl;
return zfc;
}
string fsxj(string a,string b)
{
string zfc;
a=a.substr(1,a.length());
vector<int> v;
// cout<<"a="<<a<<endl;
// cout<<"b="<<b<<endl;
// cout<<"没有倒置以前\n";
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int l=a.length();
if(a.length()<b.length())
{
l=b.length();
for(int i=0;i<l;i++)
v.push_back(0);
for(int i=0;i<a.length();i++)
v[i]+=-1*(a[i]-'0');
for(int i=0;i<b.length();i++)
v[i]+=b[i]-'0';
// for(int i=b.length()-1;i>=0;i--)
// cout<<v[i]<<" ";
for(int i=0;i<b.length();i++)
if(v[i]<0)
{
v[i]+=10;
v[i+1]--;
}
int i=l-1;
while(v[i]==0)
i--;
for(int j=i;j>=0;j--)
zfc+=v[j]+'0';
// cout<<"zfc="<<zfc<<endl;
}
else
{
for(int i=0;i<a.length()+1;i++)
v.push_back(0);
for(int i=0;i<b.length();i++)
v[i]+=-1*(b[i]-'0');
for(int i=0;i<a.length();i++)
v[i]+=a[i]-'0';
/*for(int i=a.length();i>=0;i--)
cout<<v[i]<<" ";
cout<<endl; */
/*for(int i=0;i<a.length();i++)
if(v[i]<0)
{
v[i]+=10;
v[i+1]--;
}*/
int i=a.length();
while(v[i]==0)
i--;
if(i<0)
{
cout<<"相等\n";
zfc="0";
}
else
{
if(v[i]>0)
{
zfc+='-';
for(int j=0;j<=i;j++)
if(v[j]<0)
{
v[j]+=10;
v[j+1]--;
}
while(v[i]==0)
i--;
/*for(int j=i;j>=0;j--)
cout<<v[j]<<" ";*/
for(int j=i;j>=0;j--)
zfc+=v[j]+'0';
// cout<<"zfc="<<zfc<<endl;
}
else
{
for(int j=0;j<=i;j++)
v[j]*=-1;
for(int j=0;j<=i;j++)
if(v[j]<0)
{
v[j]+=10;
v[j+1]--;
}
while(v[i]==0)
i--;
/*for(int j=0;j<=i;j++)
cout<<v[j]<<" ";
cout<<endl;*/
for(int j=i;j>=0;j--)
zfc+=v[j]+'0';
// cout<<"zfc="<<zfc<<endl;
}
}
}
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
// cout<<"-"<<a<<" + "<<b<<" =\n";
// cout<<zfc<<endl;
// cout<<zfc.length()<<endl;
cout<<"减法运算数为\n";
cout<<"-"<<a<<" "<<b<<endl;
cout<<"减法结果= "<<zfc<<endl;
return zfc;
}
string js(string a,string b)
{
if(a.length()>b.length())
{
string jh=a;
a=b;
b=jh;
}
string zfc;
if(a.length()==1)
{
if(b.length()==1)
{
int number=(a[0]-'0')*(b[0]-'0');
stringstream ss;
ss<<number;
ss>>zfc;
return zfc;
}
else
{
if(a=="0")
return "0";
int lb=b.length();
// cout<<"lb="<<lb<<endl;
string b1,b2;
b1=b.substr(0,lb/2);
b2=b.substr(lb/2,lb);
int lb2=b2.length();
b1=js(a,b1);
b2=js(a,b2);
for(int i=0;i<lb2;i++)
b1+='0';
zfc=zsxj(b1,b2);
// cout<<"b1="<<b1<<endl;
// cout<<"b2="<<b2<<endl;
return zfc;
}
}
else
{
int la=a.length();
int lb=b.length();
string A,B,C,D,ab,dc,acbd;
if(la<=(lb+1)/2)
{
A="0";
B=a;
}
else
{
A=a.substr(0,la-(lb+1)/2);
B=a.substr(la-(lb+1)/2,la);
}
C=b.substr(0,lb/2);
string cc="-";
cc+=C;
string bb="-";
int jc=0;
while(B[jc]=='0')
jc++;
B=B.substr(jc,B.length());
if(B=="")
B="0";
bb+=B;
D=b.substr(lb/2,lb);
jc=0;
while(D[jc]=='0')
jc++;
D=D.substr(jc,D.length());
if(D=="")
D="0";
int i=0;
cout<<"截取开始\n";
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"A="<<A<<endl;
cout<<"B="<<B<<endl;
cout<<"C="<<C<<endl;
cout<<"D="<<D<<endl;
cout<<"截取结束\n";
cout<<"fsxj bb="<<bb<<" A="<<A<<endl;
ab=fsxj(bb,A);
cout<<"fsxj cc="<<cc<<" D="<<D<<endl;
dc=fsxj(cc,D);
cout<<"A="<<A<<" C="<<C<<endl;
A=js(A,C);
cout<<"B="<<B<<" D="<<D<<endl;
B=js(B,D);
/*cout<<"计算 A*C B*D a-b d-c\n";
cout<<"ab="<<ab<<endl;
cout<<"dc="<<dc<<endl;
cout<<"A="<<A<<endl;
cout<<"B="<<B<<endl;*/
acbd=zsxj(A,B);
cout<<"acbd="<<acbd<<endl;
cout<<"一下为分段\n";
cout<<"ab="<<ab<<endl;
cout<<"dc="<<dc<<endl;
if(ab[0]=='-')
{
string aba=ab.substr(1,ab.length());
if(dc[0]=='-')
{
string dcd=dc.substr(1,dc.length());
ab=js(aba,dcd);
ab=zsxj(ab,acbd);
cout<<"解雇1ab="<<ab<<endl;
}
else
{
if(dc=="0")
ab="0";
else
ab="-"+js(aba,dc);
ab=fsxj(ab,acbd);
cout<<"解雇2ab="<<ab<<endl;
}
}
else
{
if(dc[0]=='-')
{
string dcd=dc.substr(1,dc.length());
if(ab!="0")
ab="-"+js(ab,dcd);
else
ab="0";
ab=fsxj(ab,acbd);
cout<<"解雇3ab="<<ab<<endl;
}
else
{
if(ab!="0")
ab=js(ab,dc);
else
ab="0";
ab=zsxj(ab,acbd);
cout<<"解雇4ab="<<ab<<endl;
}
}
cout<<"ab*cd="<<ab<<endl;
if(lb%2!=0)
lb++;
for(int i=0;i<lb;i++)
A+='0';
A=zsxj(A,B);
for(int i=0;i<lb/2;i++)
ab+='0';
if(ab[0]=='-')
zfc=fsxj(ab,A);
else
zfc=zsxj(ab,A);
return zfc;
}
}
int main()
{
string a,b;
cin>>a>>b;
// a="987654321987654321123456789123456789";
// b="987654321987654321123456789123456789";
if(a.length()>b.length())
{
string jh=a;
a=b;
b=jh;
}
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
string zfc=js(a,b);
cout<<"jg="<<zfc<<endl;
cout<<zfc.length()<<endl;
}
结果在最后一行,由于自己输入麻烦,又要在eclipse上比对,渐渐开始找不出bug了