Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <iostream>
- #include <string>
- using namespace std;
- string add (string x , string y)
- {
- int xs=x.size() ,ys=y.size() ;
- if ( xs < ys)
- {
- for (int b=0 ; b <(ys-xs) ;b++ )
- x.insert(x.begin(), '0');
- }
- else if ( xs > ys)
- {
- for (int b=0 ; b <(xs-ys) ;b++ )
- y.insert(y.begin(), '0');
- }
- //cout<<x<<" "<<y<<endl;
- string h; int carry=0 ,s=0;
- //h.resize(x.size() , 0);
- for(int i=(x.size()-1) ;i>=0 ;i--)
- {
- int f=x[i]-48 , j=y[i]-48 ;
- s=f+j+carry;
- if (s>9)
- {
- h.insert(h.begin() ,((s-10)+48));
- carry=1;
- if (i==0)
- h.insert(h.begin() ,((s/10)+48));
- }
- else
- {
- h.insert(h.begin() ,(s+48));
- carry=0;
- }
- }
- return h;
- }
- string mul (string x , string y)//هنا بتشتغل، جربي كده
- {
- int xm , ym , xs = x.size() , ys = y.size() ,m ;
- string k="";
- if (xs >= ys)
- {
- for (int i=ys-1 ; i>=0 ; i--)
- {
- string t="";int carry=0;
- for (int f=xs-1 ; f>=0 ; f--)
- {
- xm=x[f]-48; ym =y[i]-48;
- m=(xm*ym)+carry;
- if (m>9)
- {
- t.insert(t.begin() ,((m%10)+48));
- carry=m/10;
- if (f==0)
- t.insert(t.begin() ,((m/10)+48));
- }
- else
- {
- t.insert(t.begin() ,(m+48));
- carry=0;
- }
- }
- for (int d=ys-1-i ; d>0 ; d-- )
- t.insert(t.end() , '0');
- k=add(t,k);
- }
- }
- if (ys > xs)
- {
- for (int i=xs-1 ; i>=0 ; i--)
- {
- string t="";int carry=0;
- for (int f=ys-1 ; f>=0 ; f--)
- {
- xm=x[i]-48; ym =y[f]-48;
- m=(xm*ym)+carry;
- if (m>9)
- {
- t.insert(t.begin() ,((m%10)+48));
- carry=m/10;
- if (i==0)
- t.insert(t.begin() ,((m/10)+48));
- }
- else
- {
- t.insert(t.begin() ,(m+48));
- carry=0;
- }
- }
- for (int d=xs-1-i ; d>0 ; d-- )
- t.insert(t.end() , '0');
- k=add(t,k);
- }
- }
- return k;
- }
- int main ()
- {
- freopen("myDearInput.in", "r", stdin);
- string x ,y;
- while ( cin >> x >> y )
- {
- if (x=="0" || y=="0")
- {
- cout<<"0"<<endl;
- continue;
- }
- y= mul(x,y);
- cout<<y<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment