fojtasd

Untitled

Nov 5th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11.  
  12. string text="";
  13. int input=0;
  14. int a=0;
  15. int i=0;
  16. cout << "Zadej cislo ve dvojkove soustave:" << endl;
  17. getline(cin, text);
  18.  
  19.  
  20. for(i; i < static_cast<int>(text.size()); i++)
  21. {
  22. char ch = text[i];
  23.  
  24. if((ch != '0') && (ch != '1'))
  25. {
  26. cout << "Nespravny vstup." << endl;
  27. return(0);
  28. }
  29.  
  30. }
  31.  
  32.  
  33.  
  34.  
  35. int output = 0;
  36. int weight = (1ull << (text.size() - 1));
  37.  
  38. for(a; a < static_cast<int>(text.size()); a++) {
  39. output += (text[a] - '0') * weight;
  40. weight = weight >> 1;
  41. }
  42.  
  43. cout << "Desitkove cislo je: " << output << endl;
  44.  
  45. return(0);
  46.  
  47.  
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment