Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. int toInt(string s){
  7.     stringstream ss(s);
  8.     int r;
  9.     ss >> r;
  10.     return r;
  11. }
  12.  
  13.     int multiply( int a,  int b) {
  14.       int wynik = 0;
  15.       while (b > 0) {
  16.         if (b & 1)
  17.           wynik += a;
  18.         a <<= 1;
  19.         b >>= 1;
  20.       }
  21.       return wynik;
  22.     }
  23.  
  24. int main()
  25. {
  26.     string a, b; int aInt, bInt;
  27.     cin>>a>>b;
  28.     size_t foundA = a.find(",");
  29.     size_t foundB = b.find(",");
  30.  
  31.     bool isA = false, isB = false;
  32.  
  33.     if (a.find(",") != string::npos) {
  34.         a.erase(foundA, 1); isA = true;
  35.     }
  36.     if (b.find(",") != string::npos) {
  37.         b.erase(foundB, 1); isB = true;
  38.     }
  39.  
  40.     aInt = toInt(a); bInt = toInt(b);
  41.     if(aInt == 0 || bInt == 0){
  42.         cout<<0<<endl;
  43.         return 0;
  44.     }
  45.  
  46.     int result = multiply(aInt, bInt);
  47.     int comma = 0;
  48.     if(isA){
  49.         comma+=a.size()-foundA;
  50.     }
  51.     if(isB) {
  52.         comma+=b.size()-foundB;
  53.     }
  54.     //cout<<comma<<endl;
  55.     //cout<<comma<<endl;
  56.     string s = to_string(result);
  57.     if(s.size()< comma){
  58.         cout<<"0,";
  59.     }
  60.     if(s.size()== comma){
  61.         cout<<"0";
  62.     }
  63.     for(int i = 0; i <= s.size(); i++){
  64.         if(s.size()-i == comma){
  65.             cout<<",";
  66.         }
  67.         cout<<s[i];
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement