Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stack>
- using namespace std;
- stack <int>read(int bits){
- stack <int>s;
- for(int i = 0; i < bits; i++){
- int n1;
- cout << "Enter digit " << i+1 << ": " << endl;
- cin >> n1;
- s.push(n1);
- }
- return s;
- }
- stack <int> add(stack <int>a1, stack <int>a2, int num){
- stack <int>add;
- int carry = 0;
- int n1 = 0, n2= 0, sum = 0;
- for(int i = 0; i < num; i++){
- n1 = a1.top();
- n2 = a2.top();
- a1.pop();
- a2.pop();
- sum = (n1 + n2 + carry)%2;
- add.push(sum);
- carry = (n1 + n2 + carry)/2;
- }
- if(carry == 1){
- add.push(carry);
- }
- return add;
- }
- void display(stack <int> dis){
- while(!dis.empty()){
- int t = dis.top();
- cout << t << " ";
- dis.pop();
- }
- }
- int main(){
- stack <int>s1, s2, s3;
- int n = 0;
- cout << "Enter no of bits" << endl;
- cin >> n;
- cout << "Enter stack 1 numbers: " << endl;
- s1 = read(n);
- cout << "Enter stack 2 numbers: " << endl;
- s2 = read(n);
- s3 = add(s1, s2, n);
- display(s3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement