forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_to_demical.cpp
69 lines (57 loc) · 1.69 KB
/
binary_to_demical.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<string>
using namespace std;
class BinaryConverter {
private:
string binaryNum;
public:
void setBinaryNum(string num) {
binaryNum = num;
}
int binaryToDecimal() {
int decimalNum = 0;
int base = 1;
for(int i = binaryNum.length() - 1; i >= 0; i--) {
if(binaryNum[i] == '1') {
decimalNum += base;
}
base *= 2;
}
return decimalNum;
}
void displayConversion() {
int decimalNum = binaryToDecimal();
cout << "The decimal equivalent of " << binaryNum << " is " << decimalNum << endl;
}
bool isValidBinary() {
for(int i = 0; i < binaryNum.length(); i++) {
if(binaryNum[i] != '0' && binaryNum[i] != '1') {
return false;
}
}
return true;
}
bool isEmpty() {
return binaryNum.empty();
}
bool isTooLong() {
return binaryNum.length() > 32; // assuming 32-bit integer
}
};
int main() {
BinaryConverter converter;
string binaryNum;
cout << "Enter a binary number: ";
cin >> binaryNum;
converter.setBinaryNum(binaryNum);
if(converter.isEmpty()) {
cout << "Error: Binary number cannot be empty." << endl;
} else if(converter.isTooLong()) {
cout << "Error: Binary number is too long. Maximum length is 32 bits." << endl;
} else if(!converter.isValidBinary()) {
cout << "Error: Invalid binary number. Please enter a number consisting only of 0s and 1s." << endl;
} else {
converter.displayConversion();
}
return 0;
}