fojtasd

Untitled

Nov 5th, 2018
120
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 <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. cout << "Zadej cislo ve dvojkove soustave:" << endl;
  15. getline(cin, text);
  16.  
  17.  
  18. for(int i = 0; i < text.size(); i++)
  19. {
  20. char ch = text[i];
  21.  
  22. if((ch != '0') && (ch != '1'))
  23. {
  24. cout << "Nespravny vstup." << endl;
  25. return(0);
  26. }
  27.  
  28. }
  29.  
  30.  
  31. istringstream(text) >> input;
  32.  
  33. unsigned long long output = 0;
  34. unsigned long long weight = (1ull << (text.size() - 1));
  35.  
  36. for(int i = 0; i < text.size(); i++) {
  37. output += (text[i] - '0') * weight;
  38. weight = weight >> 1;
  39. }
  40. cout << text << endl;
  41. cout << "Desitkove cislo je: " << output << endl;
  42.  
  43. return(0);
  44.  
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment