-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_addition_using_STL.cpp
More file actions
67 lines (58 loc) · 911 Bytes
/
Copy pathbinary_addition_using_STL.cpp
File metadata and controls
67 lines (58 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <algorithm>
#include <stack>
#include <queue>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int displaystack( stack <int>);
int add(stack <int>b1, stack <int>b2);
int main()
{
char a[40],b[40];
int i;
stack <int>b1;
stack <int>b2;
stack <int>b3;
cout << "Enter two binary numbers"<<endl<<endl;
cin>>a>>b;
for(i=0;a[i]!='\0';i++)
{
b1.push(a[i]-'0');
}
for(i=0;b[i]!='\0';i++)
{
b2.push(b[i]-'0');
}
stack <int>s;
int q=0, w=0,carry=0,sum;
while(!b1.empty()||!b2.empty())
{
q=0, w=0;
if(!b1.empty())
{
q=b1.top();
b1.pop();
}
if(!b2.empty())
{
w=b2.top();
b2.pop();
}
sum=(q+w+carry)%2;
carry=(q+w+carry)/2;
s.push(sum);
if(b1.empty()&&b2.empty()&&carry==1)
s.push(1);
}
displaystack(s);
}
int displaystack(stack <int>s)
{
while(!s.empty())
{
cout<<""<<s.top();
s.pop();
}
return 0;
}