Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string s;
  9. cout << "Podaj cene w systemie binarnym: ";
  10. cin >> s;
  11.  
  12. string poPrzecinku = s.substr(s.length() - 2);
  13. string przedPrzecinkiem = s.substr(0, s.length() - 3);
  14.  
  15. int czescCalkowita = 0;
  16. float czescUlamkowa = 0;
  17.  
  18. int wykladnik = 0;
  19.  
  20. for(int i = przedPrzecinkiem.length() - 1; i >= 0; i--)
  21. {
  22. if(przedPrzecinkiem[i] == '1')
  23. czescCalkowita += pow(2, wykladnik);
  24. wykladnik++;
  25. }
  26.  
  27. wykladnik = -1;
  28.  
  29. for(int i = poPrzecinku.length() - 1; i >= 0; i--)
  30. {
  31. if(poPrzecinku[i] == '1')
  32. czescUlamkowa += pow(2, wykladnik);
  33. wykladnik--;
  34. }
  35.  
  36. float w = czescCalkowita + czescUlamkowa;
  37. cout << fixed << setprecision(2);
  38. cout << "Cena w systemie dziesietnym to: " << w;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement