Guest User

Untitled

a guest
Jun 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <math.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. string add (string x , string y)
  6. {
  7.     int xs=x.size() ,ys=y.size() ;
  8.  
  9.     if ( xs < ys)
  10.     {
  11.         for (int b=0 ; b <(ys-xs) ;b++ )
  12.             x.insert(x.begin(), '0');
  13.     }
  14.  
  15.  
  16.     else if ( xs > ys)
  17.     {
  18.         for (int b=0 ; b <(xs-ys) ;b++ )
  19.             y.insert(y.begin(), '0');
  20.     }
  21.     //cout<<x<<" "<<y<<endl;
  22.     string  h; int carry=0 ,s=0;
  23.     //h.resize(x.size() , 0);
  24.  
  25.     for(int i=(x.size()-1) ;i>=0 ;i--)
  26.     {
  27.  
  28.         int f=x[i]-48 , j=y[i]-48 ;
  29.         s=f+j+carry;
  30.         if (s>9)
  31.         {
  32.             h.insert(h.begin() ,((s-10)+48));
  33.             carry=1;
  34.             if (i==0)
  35.                 h.insert(h.begin() ,((s/10)+48));
  36.  
  37.         }
  38.         else
  39.         {
  40.             h.insert(h.begin() ,(s+48));
  41.             carry=0;
  42.         }
  43.  
  44.     }
  45.     return h;
  46. }
  47. string mul (string x , string y)//هنا بتشتغل، جربي كده
  48. {
  49.     int xm , ym , xs = x.size() , ys = y.size() ,m ;
  50.     string k="";
  51.     if  (xs >= ys)
  52.     {
  53.         for (int i=ys-1 ; i>=0 ; i--)
  54.         {
  55.             string t="";int carry=0;
  56.             for (int f=xs-1 ; f>=0 ; f--)
  57.             {
  58.                 xm=x[f]-48; ym =y[i]-48;
  59.                 m=(xm*ym)+carry;
  60.                 if (m>9)
  61.                 {
  62.                     t.insert(t.begin() ,((m%10)+48));
  63.                     carry=m/10;
  64.                     if (f==0)
  65.                         t.insert(t.begin() ,((m/10)+48));
  66.  
  67.                 }
  68.                 else
  69.                 {
  70.                     t.insert(t.begin() ,(m+48));
  71.                     carry=0;
  72.                 }
  73.  
  74.             }
  75.             for (int d=ys-1-i ; d>0 ; d-- )
  76.                 t.insert(t.end() , '0');
  77.             k=add(t,k);
  78.         }
  79.     }
  80.     if (ys > xs)
  81.     {
  82.        
  83.         for (int i=xs-1 ; i>=0 ; i--)
  84.         {
  85.             string t="";int carry=0;
  86.             for (int f=ys-1 ; f>=0 ; f--)
  87.             {
  88.                 xm=x[i]-48; ym =y[f]-48;
  89.                 m=(xm*ym)+carry;
  90.                 if (m>9)
  91.                 {
  92.                     t.insert(t.begin() ,((m%10)+48));
  93.                     carry=m/10;
  94.                     if (i==0)
  95.                         t.insert(t.begin() ,((m/10)+48));
  96.  
  97.                 }
  98.                 else
  99.                 {
  100.                     t.insert(t.begin() ,(m+48));
  101.                     carry=0;
  102.                 }
  103.  
  104.             }
  105.             for (int d=xs-1-i ; d>0 ; d-- )
  106.                 t.insert(t.end() , '0');
  107.             k=add(t,k);
  108.         }
  109.     }
  110.     return k;
  111. }
  112. int main ()
  113. {
  114.     freopen("myDearInput.in", "r", stdin);
  115.     string x ,y;
  116.     while ( cin >> x >> y )
  117.     {
  118.         if (x=="0" || y=="0")
  119.         {
  120.             cout<<"0"<<endl;
  121.             continue;
  122.         }
  123.         y= mul(x,y);
  124.         cout<<y<<endl;
  125.     }
  126.  
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment