-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryadd.cpp
More file actions
152 lines (117 loc) · 2.08 KB
/
binaryadd.cpp
File metadata and controls
152 lines (117 loc) · 2.08 KB
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
#include<iostream>
#include<stack>
using namespace std;
class bin{
public :
stack <int>stack1;
stack <int>stack2;
stack <int>stack3;
void read();
void add();
void display();
};
//===========================
void bin:: read()
{
int i,n,j;
int a[40],a1[40];
// n=20;
cout<<"\n enter the length of first no:";
cin>>n;
cout<<"\n enter the binari no1:";
for(i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==1){
stack1.push(1);}
if (a[i]==0){
stack1.push(0);}
}
cout<<"\n enter the length of second no:";
cin>>n;
cout<<"\n enter the binari no2:";
for(i=0;i<n;i++)
{ cin>>a1[i];
if(a1[i]==1)
{
stack2.push(1);
}
if(a1[i]==0)
{
stack2.push(0);
}
}
}
//=======================================
void bin::add()
{
int bit1,bit2;
int cry=0;
int rem;
while(!stack1.empty()|| !stack2.empty())
{
bit1=bit2=0;
if(!stack1.empty())
{
bit1=stack1.top();
stack1.pop();
}
if(!stack2.empty())
{
bit2=stack2.top();
stack2.pop();
}
rem=(bit1+bit2+cry)%2;
cry=(bit1+bit2+cry)/2;
stack3.push(rem);
}
if(cry==1)
stack3.push(1);
}
//===============================
void bin:: display()
{
/* cout<<"\n first no is:";
while(!stack1.empty())
{
cout<<stack1.top();
stack1.pop();
}
cout<<"\n second no is:";
while(!stack2.empty())
{
cout<<stack2.top();
stack2.pop();
}*/
cout<<"\n addition:\n";
while(!stack3.empty())
{
cout<<stack3.top();
stack3.pop();
}
}
//===============================
int main()
{
int ch;
bin b1;
b1.read();
b1.add();
b1.display();
/* do{
cout<<"\n select option:";
cout<<"\n1) Read no:\n2)add no \n3)display addition :\n4)exit\n";
cin>>ch;
switch(ch)
{
case 1 : b1.read();
break;
case 2: b1.add();
break;
case 3: b1.display();
break;
default: cout<<"\n pla anter valid chioce :";
}
}while(ch!=4);*/
return 0;
}