Binary Number

 #include <iostream>

#include <list>
#include <math.h>
using namespace std;

class BinaryNumber
{
public:
    list<shortgetBinary(int);
    void printBinary(list<short>);
    list<shortonesCompliment(list<short>);
    list<shorttwosCompliment(list<short>);
    list<shortaddBinary(list<short>, list<short>);
};

template <typename T>
T checkInput(T inputstring 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<shortBinaryNumber::getBinary(int num)
{
    list<shortbinaryNumber;
    while (num > 0)
    {
        short rem = num % 2;
        binaryNumber.push_back(rem);
        num /= 2;
    }
    binaryNumber.reverse();
    return binaryNumber;
}

void BinaryNumber::printBinary(list<shortbinaryNumber)
{
    list<short>::iterator iter;
    for (iter = binaryNumber.begin(); iter != binaryNumber.end(); iter++)
    {
        cout << *iter;
    }
}

list<shortBinaryNumber::onesCompliment(list<shortbinaryNumber)
{
    list<shortones;
    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<shortBinaryNumber::twosCompliment(list<shortbinaryNumber)
{
    list<shortone;
    one.push_back(1);
    list<shortones = onesCompliment(binaryNumber);
    list<shorttwos = addBinary(onesone);
    return twos;
}

int getDecimal(list<shortbin)
{
    bin.reverse();
    int decimal = 0power = 0;
    list<short>::iterator iter;
    for (iter = bin.begin(); iter != bin.end(); iter++)
    {
        decimal += (*iter) * pow(2power++);
    }
    return decimal;
}

list<shortBinaryNumber::addBinary(list<shortbin1list<shortbin2)
{
    int firstsecondsum;
    list<shortadd;
    first = getDecimal(bin1);
    second = getDecimal(bin2);
    sum = first + second;
    add = getBinary(sum);
    return add;
}

int main()
{
    int numnum1num2;
    list<shortbinbin1bin2addBinonestwos;
    bool isBinary = false;
    list<shortbinaryNumber;

    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 (nothrowBinaryNumber();
    if (!bn)
    {
        cout << "\nMemory Allocation Failed\n";
        exit(0);
    }

    while (true)
    {
        int userInput;
        cout << content;
        cin >> userInput;
        userInput = checkInput(userInputcontent);

        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(num1content2);
            bin1 = bn->getBinary(num1);

            cout << content3;cin >> num2;
            num2 = checkInput(num2content3);
            bin2 = bn->getBinary(num2);

            addBin = bn->addBinary(bin1bin2);
            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

Popular posts from this blog

Ticket Booking System

Student Database

Generalised Linked List