Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <math.h>
- #include <conio.h>
- //#include <bigint.h>
- using namespace std;
- long long BinMirror(int x) {
- int q;
- int r;
- int i;
- string temp1;
- string temp2;
- long long result;
- char temp3[256];
- int N;
- double _int;
- long long dec = 0;
- int n;
- float y=0;
- q = 1;
- r = 1;
- i = 0;
- N = 32;
- temp1 = "";
- temp2 = "";
- // temp3;
- if(x > 0) {
- temp1 = '0';
- while(!(q <= 0)) {
- q = int(x) / 2;
- r = int(x) % 2;
- if(r==0 || r==1) {
- if(r==0) {temp2 = temp2 + '0';}
- if(r==1) {temp2 = temp2 + '1';}
- // temp2 = temp2 + '0' + temp1;
- // cout << temp2;
- // cout << endl << r;
- }
- else cerr << "Ошибка.";
- x = q;
- i++;
- }
- N = 32 - i;
- i = 0;
- for(i = 0; i < N; i++) {
- temp1 = temp1 + '0';
- }
- temp2 = temp2 + temp1;
- // cout << temp2;
- // cout << endl << r;
- }
- if(x < 0) {
- temp1 = '0';
- x = -x;
- while(!(q <= 0)) {
- q = int(x) / 2;
- r = int(x) % 2;
- if(r==0 || r==1) {
- if(r==0) {temp2 = temp2 + '0';}
- if(r==1) {temp2 = temp2 + '1';}
- // temp2 = temp2 + '0' + temp1;
- // cout << temp2;
- // cout << endl << r;
- }
- else cerr << "Ошибка.";
- x = q;
- i++;
- }
- N = 32 - i;
- i = 0;
- for(i = 0; i < N-1; i++) {
- temp1 = temp1 + '0';
- }
- temp1 = temp1 + '1';
- temp2 = temp2 + temp1;
- // cout << temp2;
- // cout << endl << r;
- }
- strcpy(temp3, temp2.c_str());
- cout << "Зеркально отраженная двоичная запись числа: " << temp3 << endl;
- i = 0;
- n=strlen(temp3);
- for (i=n-1; i>=0; i--)
- {_int=temp3[i];
- if (_int==48) dec+=0;
- else if (_int==49) dec+=(1*(pow(2, y)));
- else break;
- y++;
- }
- result = dec;
- return result;
- }
- int main() {
- long long num;
- setlocale(LC_ALL, "Russian");
- cout << "Введите целое число: ";
- cin >> num;
- if(cin.fail()) {cout << "Неверный ввод данных; ожидался long long. Программа будет завершена." << endl;}
- else {cout<< "Новое число в десятичной системе исчисления: " << BinMirror(num) << endl;}
- cout << endl << "Нажмите любую клавишу чтобы выйти.";
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement