Q BgQuestion:

Novice
Karma Points: 19
Respect (66%):
posted by  hershey09 on 5/17/2008 1:46:44 PM  |  status: Live  

C++ program writing

Course Textbook Chapter Problem
N/A Discrete Mathematics and its Applications with Math Zone 6th ed. by Kenneth H. Rosen 3.6 N/A
Question Details:

Write a C++ computer program to add binary numbers.

Is missing something???

AAnswers:

Answer Question
Scholar
Karma Points: 332
posted by catherine chrysler on 5/22/2008 6:17:22 AM  |  status: Live
Asker's Rating: Lifesaver   
hershey09's comment:
"I really do appreciate the help"
Response Details:
The following is the program to add two binary numbers, it can only add two binary numbers of 16 bit.



#include <iostream.h>

#include <string.h>

 

int const binSize = 16;

 

class Binary
{

    public:
    
    Binary (const char*);
    
    Binary (unsigned int);
    
    friend Binary operator + (const Binary, const Binary);
    
    operator int (); // type conversion
    
    void Print (void);
    
    private:
    
    char bits[binSize]; // binary quantity

};


Binary::Binary (const char *num)

{

    int iSrc = strlen(num) - 1;
    
    int iDest = binSize - 1;
    
    
    
    while (iSrc >= 0 && iDest >= 0) // copy bits
    
    bits[iDest--] = (num[iSrc--] == '0' ? '0' : '1');
    
    while (iDest >= 0) // pad left with zeros
    
    bits[iDest--] = '0';

}

 

Binary::Binary (unsigned int num)

{

    for (register int i = binSize - 1; i >= 0; --i)
    {
    
        bits[i] = (num % 2 == 0 ? '0' : '1');
        
        num >>= 1;
    
    }

}

 

Binary operator + (const Binary n1, const Binary n2)

{

    unsigned carry = 0;
    
    unsigned value;
    
    Binary res = "0";
    
    
    
    for (register int i = binSize - 1; i >= 0; --i)
    {
    
        value = (n1.bits[i] == '0' ? 0 : 1) +
        
        (n2.bits[i] == '0' ? 0 : 1) + carry;
        
        res.bits[i] = (value % 2 == 0 ? '0' : '1');
        
        carry = value >> 1;

    }

    return res;

}

 

Binary::operator int ()

{

    unsigned value = 0;
    
    
    
    for (register int i = 0; i < binSize; ++i)
    
    value = (value << 1) + (bits[i] == '0' ? 0 : 1);
    
    return value;

}

 

void Binary::Print (void)

{

    char str[binSize + 1];
    
    strncpy(str, bits, binSize);
    
    str[binSize] = '\0';
    
    cout << str << '\n';
    
}



int main ()
{

    char binary1[16];
    char binary2[16];
    cout << "\nEnter first binary number: ";
    cin >> binary1;
    cout << "\nEnter second binary number: ";
    cin >> binary2;
        
    Binary n1 = binary1;
    
    Binary n2 = binary2;

        
    n1.Print();

    n2.Print();
    
    (n1 + n2).Print();

}



The above code compiles successfully in QUINCY-2005 compiler.

Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different than tutoring »