Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <math.h>
  5. #include <conio.h>
  6. //#include <bigint.h>
  7.  
  8. using namespace std;
  9.  
  10. long long BinMirror(int x) {
  11. int q;
  12. int r;
  13. int i;
  14. string temp1;
  15. string temp2;
  16. long long result;
  17. char temp3[256];
  18. int N;
  19. double _int;
  20. long long dec = 0;
  21. int n;
  22. float y=0;
  23.  
  24. q = 1;
  25. r = 1;
  26. i = 0;
  27. N = 32;
  28. temp1 = "";
  29. temp2 = "";
  30. // temp3;
  31. if(x > 0) {
  32. temp1 = '0';
  33. while(!(q <= 0)) {
  34. q = int(x) / 2;
  35. r = int(x) % 2;
  36. if(r==0 || r==1) {
  37. if(r==0) {temp2 = temp2 + '0';}
  38. if(r==1) {temp2 = temp2 + '1';}
  39. // temp2 = temp2 + '0' + temp1;
  40. // cout << temp2;
  41. // cout << endl << r;
  42. }
  43. else cerr << "Ошибка.";
  44. x = q;
  45. i++;
  46. }
  47. N = 32 - i;
  48. i = 0;
  49. for(i = 0; i < N; i++) {
  50. temp1 = temp1 + '0';
  51. }
  52. temp2 = temp2 + temp1;
  53. // cout << temp2;
  54. // cout << endl << r;
  55. }
  56. if(x < 0) {
  57. temp1 = '0';
  58. x = -x;
  59. while(!(q <= 0)) {
  60. q = int(x) / 2;
  61. r = int(x) % 2;
  62. if(r==0 || r==1) {
  63. if(r==0) {temp2 = temp2 + '0';}
  64. if(r==1) {temp2 = temp2 + '1';}
  65. // temp2 = temp2 + '0' + temp1;
  66. // cout << temp2;
  67. // cout << endl << r;
  68. }
  69. else cerr << "Ошибка.";
  70. x = q;
  71. i++;
  72. }
  73. N = 32 - i;
  74. i = 0;
  75. for(i = 0; i < N-1; i++) {
  76. temp1 = temp1 + '0';
  77. }
  78. temp1 = temp1 + '1';
  79. temp2 = temp2 + temp1;
  80. // cout << temp2;
  81. // cout << endl << r;
  82. }
  83.  
  84. strcpy(temp3, temp2.c_str());
  85.  
  86. cout << "Зеркально отраженная двоичная запись числа: " << temp3 << endl;
  87.  
  88. i = 0;
  89. n=strlen(temp3);
  90.  
  91. for (i=n-1; i>=0; i--)
  92. {_int=temp3[i];
  93. if (_int==48) dec+=0;
  94. else if (_int==49) dec+=(1*(pow(2, y)));
  95. else break;
  96. y++;
  97. }
  98.  
  99. result = dec;
  100. return result;
  101. }
  102.  
  103. int main() {
  104. long long num;
  105.  
  106. setlocale(LC_ALL, "Russian");
  107. cout << "Введите целое число: ";
  108. cin >> num;
  109. if(cin.fail()) {cout << "Неверный ввод данных; ожидался long long. Программа будет завершена." << endl;}
  110. else {cout<< "Новое число в десятичной системе исчисления: " << BinMirror(num) << endl;}
  111.  
  112. cout << endl << "Нажмите любую клавишу чтобы выйти.";
  113. _getch();
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement