【求助】 关于一道 计算 很长很长的 整数 加减乘的问题
当整数位数特别多的时候 会超出 int 的运算范围而溢出,这时候需要我们从新来写程序才能运算。我的思路是 用 vector 来进行计算。加和减 都已经弄好了,现在就差 乘了,没有好想法,看看大家能不能帮我一下。题目要求是是这样的:Please enter two integers:
a = 123456789123645
b = 12321346546546
The results are:
a + b = 135778135670191
a – b = 111135442577099
a * b = 1521153882316281094541680170
下面是我写的代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void subtract(string s1,string s2,int a,int b);
int compare(string s1,string s2);
int fstzero(vector <int> v3);
void main ()
{
vector <int> v1,v2,v3;
string s1,s2;
cout<<"Please input two integers:"<<endl;
cout<<"a = ";
cin>>s1;
for(int i=0;i<s1.length();i++)
{v1.push_back(s1[i]-48);}
cout<<"b = ";
cin>>s2;
for (int i=0;i<s2.length();i++)
{v2.push_back(s2[i]-48);}
int a = s1.length ()<s2.length ()?s1.length ():s2.length ();//得到 最短的 数的位数
int b = s1.length ()>s2.length ()?s1.length ():s2.length ();//得到 最长的 数的位数
int sum=0;
//开始 算 sum
for (int i=1;i<a;i++)
{
if (v1[s1.length()-i]+v2[s2.length ()-i]<10)
{v3.push_back (v1[s1.length ()-i]+v2[s2.length ()-i]);}
else {v3.push_back (v1[s1.length ()-i]+v2[s2.length ()-i]-10);v2[s2.length()-i-1]=v2[s2.length()-i-1]+1;}
}
if (v1[s1.length()-a]+v2[s2.length()-a]<10)
{v3.push_back (v1[s1.length()-a]+v2[s2.length()-a]);}
else {
v3.push_back (v1[s1.length()-a]+v2[s2.length()-a]-10);
((s1.length ()>s2.length ())?v1[s1.length()-a-1]:v2[s2.length()-a-1])=((s1.length ()>s2.length ())?v1[s1.length()-a-1]:v2[s2.length()-a-1]) + 1;
}
cout<<"The results are:"<<endl;
cout<<"a + b =";
//开始显示 sum;
for (int i=0;i<b-a;i++)
{cout<<(s1.length ()>s2.length ()?v1[i]:v2[i]);}
for (int i=a-1;i>=0;i--)
{cout<<v3[i];}
cout<<endl;
//开始 计算 差值
//首先判断大小
cout<<"a - b =";
if (compare(s1,s2)==0)
{string temp =s1;
s1=s2;
s2=temp;
cout<<'-';
subtract(s1,s2,a,b);}
else {subtract(s1,s2,a,b);}
//开始 有关 * 法的 部分
//目前还没什么好想法
}
int compare(string s1,string s2)
{
if (s1.length()>s2.length ())
return 1;
else if (s1.length()==s2.length ())
{
for (int i=0;i<s1.length();i++)
{ if (s2[i]<s1[i]) return 1;}
}
else return 0;
}
void subtract(string s1,string s2,int a,int b)
{
vector <int> v1,v2,v3;
if (s1.length()>=s2.length ())
{
for(int i=0;i<s1.length();i++)
{v1.push_back(s1[i]-48);}
for (int i=0;i<s2.length();i++)
{v2.push_back(s2[i]-48);}
for (int i=1;i<=a;i++)
{
if (v1[s1.length()-i]+v2[s2.length ()-i]>=0)
{v3.push_back (v1[s1.length ()-i]-v2[s2.length ()-i]);}
else {v3.push_back (v1[s1.length ()-i]-v2[s2.length ()-i]+10);v1[s1.length()-i-1]=v1[s1.length()-i-1]-1;}
}
//if (v1[s1.length()-a]-v2[s2.length()-a]>=0)
//{v3.push_back (v1[s1.length()-a]-v2[s2.length()-a]);}
//else {
// v3.push_back (v1[s1.length()-a]-v2[s2.length()-a]+10);
// v1[s1.length()-a-1]=v1[s1.length()-a-1]-1;
// }
if (b-a>0)
{
for (int i=0;i<b-a;i++)
{cout<<(s1.length ()>s2.length ()?v1[i]:v2[i]);}
for (int i=a-1;i>=0;i--)
{cout<<v3[i];}
cout<<endl;
}
else
{
for (int i=fstzero(v3);i>=0;i--)
{cout<<v3[i];}
cout<<endl;
}
}
}
int fstzero(vector <int> v3)
{
for (int i=v3.size()-1;i>=0;i--)
{
if (v3[i]!=0)
return i;
}
}