Here's the code for the function multiplying big numbers, which prints the result on screen:
void multiplying(string a, string b)
{
if(a=="0"||b=="0")
cout<<0;
else
{
vector<int> C;
for(int i=0; i<a.size()+b.size(); i++)
C.push_back(0);
bool minus=false;
if((a[0]=='-'&&b[0]!='-')||(b[0]=='-'&&a[0]!='-'))
minus=true;
if(a[0]='-')
a.erase(a.begin());
if(b[0]='-')
b.erase(b.begin());
if(a.size()<b.size())
swap(a,b);
for(int i=b.size()-1; i>=0; i--)
{
for(int j=a.size()-1; j>=0; j--)
{
C[i+j+1]+=((a[j]-'0')*(b[i]-'0'));
int k=i+j+1;
while(C[k]>9&&k>0)
{
C[k-1]+=C[k]/10;
C[k]=C[k]%10;
k--;
}
}
}
while(C[0]==0)
C.erase(C.begin());
if(minus)
cout<<"-";
for(int i=0; i<C.size(); i++)
cout<<C[i];
cout<<endl;
}
}
However, it is never correct and I need to improve this. I can't see where is my mistake though - spent quite a long time on this code.
........... ............. ............... ............
Aucun commentaire:
Enregistrer un commentaire