Binary Number
#include <iostream>
#include <list>
#include <math.h>
using namespace std;
class BinaryNumber
{
public:
list<short> getBinary(int);
void printBinary(list<short>);
list<short> onesCompliment(list<short>);
list<short> twosCompliment(list<short>);
list<short> addBinary(list<short>, list<short>);
};
template <typename T>
T checkInput(T input, string content)
{
while (!cin.good())
{
cout << "\nWrong Input, Please Enter It Correctly!!\n";
cin.clear();
cin.ignore();
cout << content;
cin >> input;
}
while (input < 0)
{
cout << "\nThis Input Cannot Be Negative!!\n";
cin.clear();
cin.ignore();
cout << content;
cin >> input;
}
return input;
}
list<short> BinaryNumber::getBinary(int num)
{
list<short> binaryNumber;
while (num > 0)
{
short rem = num % 2;
binaryNumber.push_back(rem);
num /= 2;
}
binaryNumber.reverse();
return binaryNumber;
}
void BinaryNumber::printBinary(list<short> binaryNumber)
{
list<short>::iterator iter;
for (iter = binaryNumber.begin(); iter != binaryNumber.end(); iter++)
{
cout << *iter;
}
}
list<short> BinaryNumber::onesCompliment(list<short> binaryNumber)
{
list<short> ones;
list<short>::iterator iter;
for (iter = binaryNumber.begin(); iter != binaryNumber.end(); iter++)
{
if (*iter == 0)
ones.push_back(1);
else
ones.push_back(0);
}
return ones;
}
list<short> BinaryNumber::twosCompliment(list<short> binaryNumber)
{
list<short> one;
one.push_back(1);
list<short> ones = onesCompliment(binaryNumber);
list<short> twos = addBinary(ones, one);
return twos;
}
int getDecimal(list<short> bin)
{
bin.reverse();
int decimal = 0, power = 0;
list<short>::iterator iter;
for (iter = bin.begin(); iter != bin.end(); iter++)
{
decimal += (*iter) * pow(2, power++);
}
return decimal;
}
list<short> BinaryNumber::addBinary(list<short> bin1, list<short> bin2)
{
int first, second, sum;
list<short> add;
first = getDecimal(bin1);
second = getDecimal(bin2);
sum = first + second;
add = getBinary(sum);
return add;
}
int main()
{
int num, num1, num2;
list<short> bin, bin1, bin2, addBin, ones, twos;
bool isBinary = false;
list<short> binaryNumber;
string content1("\nEnter The Number : ");
string content2("\nEnter The First Number : ");
string content3("\nEnter The Second Number : ");
string content("\n1. Press 1 To Get Binary\n2. Press 2 To Print Binary\n3. Press 3 To Print 1's Compliment\n4. Press 4 To Print 2's Compliment\n5. Press 5 To Add Two Binary Numbers\n6. Press 6 To Exit\n>>>> ");
BinaryNumber *bn = new (nothrow) BinaryNumber();
if (!bn)
{
cout << "\nMemory Allocation Failed\n";
exit(0);
}
while (true)
{
int userInput;
cout << content;
cin >> userInput;
userInput = checkInput(userInput, content);
switch (userInput)
{
case 1:
cout << content1;cin >> num;
bin = bn->getBinary(num);
isBinary = true;
break;
case 2:
if (!isBinary)
{
cout << "\nThere is no Binary Number to Print!!\n";
break;
}
cout << "\nBin(" << num << ") : ";
bn->printBinary(bin);
cout << "\n";
break;
case 3:
if (!isBinary)
{
cout << "\nThere is no Binary Number to Print One's Compliment!!\n";
break;
}
ones = bn->onesCompliment(bin);
cout << "\n1's(";bn->printBinary(bin);cout << ")' : ";bn->printBinary(ones);cout << "\n";
break;
case 4:
if (!isBinary)
{
cout << "\nThere is no Binary Number to Print Two's Compliment!!\n";
break;
}
twos = bn->twosCompliment(bin);
cout << "\n2's(";bn->printBinary(bin);cout << ")' : ";bn->printBinary(twos);cout << "\n";
break;
case 5:
cout << content2;cin >> num1;
num1 = checkInput(num1, content2);
bin1 = bn->getBinary(num1);
cout << content3;cin >> num2;
num2 = checkInput(num2, content3);
bin2 = bn->getBinary(num2);
addBin = bn->addBinary(bin1, bin2);
cout << "\n(";bn->printBinary(bin1);cout << " + ";bn->printBinary(bin2);cout << ")";cout << " : ";bn->printBinary(addBin);cout << "\n";
break;
case 6:
exit(0);
default:
cout << "\nPlease Enter Correct Input!!\n";
break;
}
}
delete bn;
return 0;
}
// Output
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 1
// Enter The Number : 88
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 2
// Bin(88) : 1011000
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 3
// 1's(1011000)' : 0100111
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 4
// 2's(1011000)' : 101000
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 5
// Enter The First Number : 5
// Enter The Second Number : 8
// (101 + 1000) : 1101
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 5
// Enter The First Number : 58
// Enter The Second Number : 85
// (111010 + 1010101) : 10001111
// 1. Press 1 To Get Binary
// 2. Press 2 To Print Binary
// 3. Press 3 To Print 1's Compliment
// 4. Press 4 To Print 2's Compliment
// 5. Press 5 To Add Two Binary Numbers
// 6. Press 6 To Exit
// >>>> 6
Comments
Post a Comment