-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment-5.cpp
More file actions
317 lines (299 loc) · 5.4 KB
/
Copy pathassignment-5.cpp
File metadata and controls
317 lines (299 loc) · 5.4 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//author: Tejas Joshi
//Date: 11/09/2017
#include<iostream>
using namespace std;
class chain;
class node
{
int data;
node *next;
node *prev;
friend class chain;
};
class chain
{
node *start=NULL;
public:
void add_node(int num)
{
node *q=start;
node *temp= new node;
temp->data=num;
temp->next=NULL;
temp->prev=NULL;
if(start==NULL)
{
start=temp;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
temp->prev=q;
}
}
void append(int num)
{
node *q=start;
node *temp= new node;
temp->data=num;
if(start==NULL)
{
start=temp;
temp->next=NULL;
temp->prev=NULL;
}
else
{
temp->next=start;
q->prev=temp;
temp->prev=NULL;
start=temp;
}
}
void display()
{
node *q=start;
while(q!=NULL)
{
cout<<q->data<<" ";
q=q->next;
}
cout<<endl;
}
void ones_comp(chain num1)
{
node *p=num1.start;
while(p!=NULL)
{
if(p->data==1)
{
add_node(0);
}
else
{
add_node(1);
}
p=p->next;
}
}
void twos_comp()
{
node *q=start;
while(q->next!=NULL)
{
q=q->next;
}
while(q!=NULL)
{
if(q->data==1)
q->data=0;
else
{
q->data=1;
break;
}
q=q->prev;
}
}
chain operator+(chain num)
{
int carry=0;
chain add;
node *q=start;
node *p=num.start;
while(q->next!=NULL)
{
q=q->next;
}
while(p->next!=NULL)
{
p=p->next;
}
while(q!=NULL && p!=NULL)
{
if(carry==0)
{
if((q->data==0 && p->data==1) || (q->data==1 && p->data==0))
{
add.append(1);
carry=0;
}
else if(q->data==0 && p->data==0)
add.append(0);
else if(q->data==1 && p->data==1)
{
add.append(0);
carry=1;
}
}
else
{
if((q->data==0 && p->data==1) || (q->data==1 && p->data==0))
{
add.append(0);
carry=1;
}
else if(q->data==0 && p->data==0)
{
add.append(1);
carry=0;
}
else if(q->data==1 && p->data==1)
{
add.append(1);
carry=1;
}
}
q=q->prev;
p=p->prev;
}
if(q==NULL && p==NULL && carry==1)
add.append(1);
return add;
}
};
int main()
{
int bit1,bit2,i,bit,choice;
chain num1,num2,num3,num4,num5,num6,num7;
cout<<"enter number of bits for first binary number : ";
cin>>bit1;
cout<<"Enter first binary number bit by bit : ";
for(i=0;i<bit1;i++)
{
cin>>bit;
num1.add_node(bit);
}
cout<<"You entered number : ";
num1.display();
cout<<"enter number of bits for second binary number : ";
cin>>bit2;
cout<<"Enter second binary number bit by bit : ";
for(i=0;i<bit2;i++)
{
cin>>bit;
num2.add_node(bit);
}
cout<<"You entered number : ";
num2.display();
do
{
cout<<"Enter choice\n1. One's complement of first number\n2. One's complement of second number\n";
cout<<"3. Two's complement of first number\n4. Two's complement of second number\n5. Addition\n6. Exit\n";
cin>>choice;
switch (choice)
{
case 1: num3.ones_comp(num1);
cout<<"Ones complement of first number is : ";
num3.display();
cout<<endl;
break;
case 2: num4.ones_comp(num2);
cout<<"Ones complement of second number is : ";
num4.display();
cout<<endl;
break;
case 3: num5.ones_comp(num1);
num5.twos_comp();
cout<<"Two's complement of first number is : ";
num5.display();
cout<<endl;
break;
case 4: num6.ones_comp(num2);
num6.twos_comp();
cout<<"Two's complement of second number is : ";
num6.display();
cout<<endl;
break;
case 5:
if(bit1!=bit2)
{
if(bit1>bit2)
{
for(i=0;i<bit1-bit2;i++)
{
num2.append(0);
}
}
else if(bit1<bit2)
{
for(i=0;i<bit2-bit1;i++)
{
num1.append(0);
}
}
}
num7=num1+num2;
cout<<"Addition of these numbers is : ";
num7.display();
cout<<endl;
break;
}
}while(choice!=6);
return 0;
}
/*output of program
enter number of bits for first binary number : 4
Enter first binary number bit by bit : 1 1 0 1
You entered number : 1 1 0 1
enter number of bits for second binary number : 3
Enter second binary number bit by bit : 1 0 1
You entered number : 1 0 1
Enter choice
1. One's complement of first number
2. One's complement of second number
3. Two's complement of first number
4. Two's complement of second number
5. Addition
6. Exit
1
Ones complement of first number is : 0 0 1 0
Enter choice
1. One's complement of first number
2. One's complement of second number
3. Two's complement of first number
4. Two's complement of second number
5. Addition
6. Exit
2
Ones complement of second number is : 0 1 0
Enter choice
1. One's complement of first number
2. One's complement of second number
3. Two's complement of first number
4. Two's complement of second number
5. Addition
6. Exit
3
Two's complement of first number is : 0 0 1 1
Enter choice
1. One's complement of first number
2. One's complement of second number
3. Two's complement of first number
4. Two's complement of second number
5. Addition
6. Exit
4
Two's complement of second number is : 0 1 1
Enter choice
1. One's complement of first number
2. One's complement of second number
3. Two's complement of first number
4. Two's complement of second number
5. Addition
6. Exit
5
Addition of these numbers is : 1 0 0 1 0
Enter choice
1. One's complement of first number
2. One's complement of second number
3. Two's complement of first number
4. Two's complement of second number
5. Addition
6. Exit
6
*/