This is 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
Write a C++ program using STL to add binary numbers(assume one bit as one number).
This is 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
Prerequiiste
Theory
This is 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
Imp_Ques
This is C code for 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
code in c
This is C++ code for 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
#include<iostream>
#include<stack>
using namespace std;
stack<int> read_number()
{
stack<int> st;
int x,numberofbits,i;
cout<<"Enter the no. of bits in the no. :"<<endl;
cin>>numberofbits;
cout<<"\nEnter the binary number : "<<endl;
for(i=0;i<numberofbits;i++)
{
cin>>x;
st.push(x);
}
return st;
}
void display_result(stack<int> &st)
{
cout<<" ";
while(!st.empty())
{
cout<<st.top()<<" ";
st.pop();
}
}
stack<int> add_numbers(stack<int> &st1, stack<int> &st2)
{
stack<int> st;
int sum,carry=0,bit1,bit2;
while(!st1.empty()||!st2.empty())
{
bit1=bit2=0;
if(!st1.empty())
{
bit1=st1.top();
st1.pop();
}
if(!st2.empty())
{
bit2=st2.top();
st2.pop();
}
sum=(bit1+bit2+carry)%2;
carry=(bit1+bit2+carry)/2;
st.push(sum);
}
if(carry==1)
st.push(1);
return st;
}
int main()
{
stack<int> st1,st2,st3;
int choice;
cout<<"*******************MENU*************************"<<endl;
cout<<"The available choices are as follows: "<<endl;
cout<<"1.To Read two number: "<<endl;
cout<<"2.To add 2 binary numbers: "<<endl;
cout<<"3.Exit"<<endl;
do
{
cout<<"\nEnter your choice: "<<endl;
cin>>choice;
switch(choice)
{
case 1:
st1=read_number(); //read function returns a stack
cout<<"-------------------------------------------"<<endl;
st2=read_number();
cout<<"-------------------------------------------"<<endl;
break;
case 2:
cout<<"\nThe result of addition is = "<<endl;
st3=add_numbers(st1,st2);
display_result(st3);
cout<<"\n-------------------------------------------"<<endl;
break;
}
}while(choice!=3);
return 0;
}
This is python code for 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
code in python
This is Java code for 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
code in java
This is 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy
If you have a pre-existing submission for this assignment making a new one will override it.
blog comments powered by Disqus
This is 'Binary Stack' assignment of
Object Oriented Programming (SPPU SE SEM-4) - CS of
Savitribai Phule Pune University - Gyaani Buddy