Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. void bin_to_dec(string s)
  8. {
  9. int res = 0;
  10. int len = s.length();
  11. int *arr = new int[len];
  12.  
  13.  
  14. for (int i = 0; i < len; i++)
  15. {
  16. if (s[i] == '1')
  17. {
  18. arr[i] = 1;
  19.  
  20. }
  21.  
  22. if (s[i] == '0')
  23. {
  24. arr[i] = 0;
  25. }
  26. }
  27.  
  28.  
  29. for (int i = 0, j = len - 1; i < len, j >= 0; i++, j--)
  30. res = res + arr[i] * (int)pow(2, j);
  31.  
  32. cout << "in dec " << res << endl;
  33.  
  34. }
  35.  
  36. int main()
  37.  
  38. {
  39. string s = "";
  40.  
  41. while (true)
  42. {
  43. cin >> s;
  44. bin_to_dec(s);
  45. }
  46.  
  47. system("pause");
  48. return 0;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement