We got an assignment to write a function multiplying big numbers, which prints the result on screen.
Here is my code so far:
void mnozenie(string a, string b)
{
if(a=="0"||b=="0")
cout<<0;
else
{
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);
vector<int> C;
for(int i=0; i<(int)a.size()+(int)b.size(); i++)
C.push_back(0);
for(int i=(int)b.size()-1; i>=0; i--)
{
for(int j=(int)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<(int)C.size(); i++)
cout<<C[i];
cout<<endl;
}
}
I haven't found a case when it's incorrect, but when I try to submit it I get ANSWER mistake. So probably there is such a case.