/
rokhin
/
Small_test_programs_cpp
Обзор
Документация
Войти
/
rokhin
/
Small_test_programs_cpp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
Binary.cpp
135 строк
3 KB
sergeyrokhin
Начало
03 фев 2025, 13:36
03 фев 2025, 13:36
e354b68
Код
Авторство
О чём код?
#include <iostream> #include <cmath> #include <vector> using namespace std; struct Binary : vector<int> { Binary() : vector<int>(16, 0) { } void Sign(int count) { if (count >= 0) { int i = 15; while (count != 0) { while (count < pow(2, i)) { i--; } (*this)[15 - i] = 1; count -= pow(2, i); } return; } else { count *= -1; int i = 15; while (count != 0) { while (count < pow(2, i)) { i--; } (*this)[15 - i] = 1; count -= pow(2, i); } (*this)[0] = 1; return; } } void Solve(int count) { if (count >= 0) { int i = 15; while (count != 0) { while (count < pow(2, i)) { i--; } (*this)[15 - i] = 1; count -= pow(2, i); } return; } else { int i = 15; count *= -1; while (count != 0) { while (count < pow(2, i)) { i--; } (*this)[15 - i] = 1; count -= pow(2, i); } for (int i = 0; i < this->size(); i++) { if ((*this)[i] == 1) { (*this)[i] = 0; } else { (*this)[i] = 1; } } for (int i = this->size() - 1; i > 0; i--) { if (!(PlusOne(true, i))) { return; } } } }; bool PlusOne(bool Plus, int i) { if ((*this)[i] == 1) { (*this)[i] = 0; return true; } else { (*this)[i] = 1; return false; } } void print() { for (int i = 0; i < size(); i++) { cout << (*this)[i]; } cout << endl; } }; int main(int argc, char const *argv[]) { Binary tmp; int number; bool sign; cout << "input" << endl; cin >> number; cout << "input form: 1 - signed, 0 - unsigned" << endl; cin >> sign; if (sign) { tmp.Sign(number); } else { tmp.Solve(number); } tmp.print(); return 0; }