Advertisement
MeehoweCK

Untitled

Mar 2nd, 2021
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. string odwroc(string liczba)
  7. {
  8.     string wynik = "";
  9.     for(int i = liczba.size() - 1; i >= 0; --i)
  10.         wynik += liczba[i];
  11.     return wynik;
  12. }
  13.  
  14. int konwertuj_2_na_10(string liczba2)
  15. {
  16.     liczba2 = odwroc(liczba2);
  17.     unsigned n = liczba2.size();
  18.     int wynik = 0;
  19.     for(unsigned i = 0; i < n; ++i)
  20.         if(liczba2[i] == '1')
  21.             wynik += pow(2,i);
  22.     return wynik;
  23. }
  24.  
  25. string konwertuj_10_na_2(int liczba10)
  26. {
  27.     string wynik = "";
  28.     while(liczba10 > 0)
  29.     {
  30.         if(liczba10 % 2 == 1)
  31.             wynik += '1';
  32.         else
  33.             wynik += '0';
  34.         liczba10 /= 2;
  35.     }
  36.     return odwroc(wynik);
  37. }
  38.  
  39. string int_to_string(int liczba)
  40. {
  41.     string wynik = "";
  42.     while(liczba > 0)
  43.     {
  44.         wynik += static_cast<char>(liczba % 10 + 48);
  45.         liczba /= 10;
  46.     }
  47.     return odwroc(wynik);
  48. }
  49.  
  50. int main()
  51. {
  52.     int liczba = 104682;
  53.     cout << int_to_string(liczba);
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement