I need help in my home work urgent.

Write a program in C++ which asks user to enter two binary numbers. Store these two numbers in two different stack variables (objects) bit by bit and calculate its sum and store it in another stack variable and display its sum.


Solution Guidelines:

1. Create a class named Bin_add (means stack class) that should contain following inline functions:
•   Constructor( ): To initialize “Bin_add” class data members.
•   Isempty( ):        To check whether a stack is empty or not. 
•   Push( )              To push binary number bit by bit onto the stack
•   Pop( )               To pop bit (either 0 or 1) from the top of the stack
•   Add( )              To add two binary numbers bit by bit and display the sum.

2. In main function:
           (a). Create three objects b1, b2, b_sum of type Bin_add class, two for input and one for storing the sum. 
           (b). Read two binary numbers from user bit by bit and push (insert) them in b1 and b2.
           (c). Call the Add() function with b_sum object to add and display the sum of both binary numbers.
#include <iostream.h> 
#include<stdlib.h> 
using namespace std; 
class Bin_Add 
{ 
public: 
      Bin_Add() { current = -1;} 
       int top(){return A[current];} 
       int pop(){ return A[current--];} 
       void push(int x) {A[++current] = x;} 
        int isEmpty(){return ( current == -1 );} 
       void add();    

private: 
        int current; 
        int A[20]; 
        int x; 
        int y; 
}; 
int main() 
{ 
    int n=0, a=0,b=0; 
   Bin_Add b1,b2,b3,b_Sum;

  cout << "\nPlease Enter the number of bits in each binary number = "; 
  cin >> n; 
  cout << "\nPlease Enter 1st binary number bit by bit = \n"; 
  for (int i=0; i<n ;i++) 
  { 
      cin >> a; 
      b1.push(a); 
   } 
cout << "\nPlease Enter 1st binary number bit by bit = \n";
for (int i=0; i<n ;i++) 
  { 
      cin >> b; 
      b2.push(b); 
   } 
/*****************************************************************************************************/   
/* ADDITION OF TWO BINARY NOS.*/ 
   int sum,carry=0,x1,x2; 
while(!b1.isEmpty()||!b2.isEmpty()) 
{ 
x1=x2=0; 
if(!b1.isEmpty()) 
  { 
  x1=b1.top(); 
  b1.pop(); 
  } 
if(!b2.isEmpty())
{ 
  x2=b2.top(); 
  b2.pop(); 
  } 
 sum = (x1+x2+carry)%2; 
 carry=(x1+x2+carry)/2; 
 b3.push(sum); 
} 
if(carry==1) 
 b3.push(1); 
cout << "\nSum of binary number is = ";
while(!b3.isEmpty()) 
{ 
cout <<  b3.top(); 
b3.pop(); 
} 
cout << "\n "; 
/*****************************************************************************************************/   
system("pause");
}




This my code .Please check it. Is this code is according to my requirement.
Create a class named Bin_add (means stack class) that should contain following inline functions:
•   Add( )              To add two binary numbers bit by bit and display the sum.

Should be a function from your class, that should be implemented in the class, and not aside, in some clauses in the main function...

Please tell me how to implement it in main function?

Should be a function from your class, that should be implemented in the class, and not aside, in some clauses in the main function...

I'm saying to implement it in the class, and NOT in the main function.

okay! Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.