-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.binary stack.cpp
164 lines (143 loc) · 2.27 KB
/
1.binary stack.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//12
#include<iostream>
#include<stack>//push,pop,top
using namespace std;
class binary
{
private:
stack<int>s1;
stack<int>s2;
stack<int>s3;
int b1[4]; //IMP
int b2[4];
public:
void read()
{
int n;
cout<<"ENTER 1ST BINARY NO."<<endl;
for(int i=0;i<4;i++)
{
cout<<"Enter "<<i+1<<" bit : ";
cin>>n;
b1[i]=n;
s1.push(n);
}
cout<<"\n\nENTER 2ND BINARY NO."<<endl;
for(int i=0;i<4;i++)
{
cout<<"Enter "<<i+1<<" bit : ";
cin>>n;
b2[i]=n;
s2.push(n);
}
}
void display()
{
cout<<"1ST BINARY NO."<<endl;
for(int i=0;i<4;i++)
{
cout<<b1[i];
}
cout<<"\n2ND BINARY NO."<<endl;
for(int i=0;i<4;i++)
{
cout<<b2[i];
}
}
void add()
{
int carry=0;
int j1,j2;
for(int i=0;i<4;i++)
{
j1=s1.top();
s1.pop();
j2=s2.top();
s2.pop();
// cout<<"j1="<<j1<<" j2="<<j2<<"\n";
switch(carry)
{
case 0:
if(j1==0&&j2==0)
{
s3.push(0);
carry=0;
}
if(j1==0&&j2==1)
{
s3.push(1);
carry=0;
}
if(j1==1&&j2==0)
{
s3.push(1);
carry=0;
}
if(j1==1&&j2==1)
{
s3.push(0);
carry=1;
}
break;
case 1:
if(j1==0&&j2==0)
{
s3.push(1);
carry=0;
}
if(j1==0&&j2==1)
{
s3.push(0);
carry=1;
}
if(j1==1&&j2==0)
{
s3.push(0);
carry=1;
}
if(j1==1&&j2==1)
{
s3.push(1);
carry=1;
}
break;
}
}
cout<<"ADDITTION: ";
for(int i=0;i<4;i++)
{
cout<<s3.top();
s3.pop();
}
cout<<"\n";
}
};
main()
{
binary b;
int op=-1;
while(op!=0)
{
cout<<"\n1.accept binary no ";
cout<<"\n2.display binary no";
cout<<"\n3.Add binary no";
cout<<"\n4.exit";
cout<<"\n\nenter choice : ";
cin>>op;
switch(op)
{
case 1:
b.read();
break;
case 2:
b.display();
break;
case 3:
b.add();
break;
case 4:
op=0;
break;
}
}
}