Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- class Binary {
- bool data[32];
- int number;
- public:
- Binary();
- Binary(int value);
- Binary(string s);
- void show() const;
- int to_int() const;
- void bin(int n);
- void string_to_bin(string s);
- };
- void Binary::string_to_bin(string s) {
- for (int i = 0; i < s.length(); i++) {
- if (s[i] == '1') {
- data[i] = 1;
- }
- else {
- data[i] = 0;
- }
- }
- }
- void Binary::bin(int n) {
- if (n >= 2) {
- bin(n / 2);
- }
- number++;
- }
- Binary::Binary() {
- number = 0;
- for (int i = 0; i < 32; i++) {
- data[i] = 0;
- }
- }
- Binary::Binary(int value) {
- number = 0;
- bin(value);
- for (int i = number - 1; i >= 0; i--) {
- data[i] = value % 2;
- value /= 2;
- }
- }
- Binary::Binary(string s) {
- number = s.length();
- string_to_bin(s);
- }
- void Binary::show() const {
- for (int i = 0; i < number; i++) {
- cout << data[i];
- }
- }
- int Binary::to_int() const {
- int a = 0;
- for (int i = number-1; i >= 0; i--) {
- a += data[i] * pow(2, i);
- }
- return a;
- }
- int main() {
- int n;
- cin >> n;
- Binary bin(n);
- bin.show();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement