-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_23(c++).cpp
169 lines (142 loc) · 3.88 KB
/
Practical_23(c++).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
165
166
167
168
169
'''
Write C++ program for storing binary number using doubly linked lists. Write functions
a) To compute 1‘s and 2‘s complement
b) Add two binary numbers
'''
#include <iostream>
using namespace std;
// Structure for the doubly linked list node
struct Node {
int data;
Node* prev;
Node* next;
};
// Function to create a new node
Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the doubly linked list
Node* insertEnd(Node* head, int data) {
if (head == NULL) {
head = createNode(data);
return head;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
Node* newNode = createNode(data);
temp->next = newNode;
newNode->prev = temp;
return head;
}
// Function to get the 1's complement of a binary number
void onesComplement(Node* head) {
Node* temp = head;
while (temp != NULL) {
temp->data = (temp->data == 0) ? 1 : 0;
temp = temp->next;
}
}
// Function to get the 2's complement of a binary number
void twosComplement(Node* head) {
Node* temp = head;
int carry = 1;
// Find the rightmost 1 in the list
while (temp->next != NULL) {
temp = temp->next;
}
// Perform 1's complement
while (temp != NULL) {
temp->data = (temp->data == 0) ? 1 : 0;
temp = temp->prev;
}
// Add 1 to the 1's complement
temp = head;
while (temp != NULL) {
int sum = temp->data + carry;
temp->data = sum % 2;
carry = sum / 2;
temp = temp->next;
}
}
// Function to add two binary numbers
Node* addBinaryNumbers(Node* head1, Node* head2) {
Node* sumHead = NULL;
int carry = 0;
Node* temp1 = head1;
Node* temp2 = head2;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
while (temp2->next != NULL) {
temp2 = temp2->next;
}
// Start adding the binary numbers from the rightmost bits
while (temp1 != NULL || temp2 != NULL) {
int sum = carry;
if (temp1 != NULL) {
sum += temp1->data;
temp1 = temp1->prev;
}
if (temp2 != NULL) {
sum += temp2->data;
temp2 = temp2->prev;
}
carry = sum / 2;
sum %= 2;
sumHead = insertEnd(sumHead, sum);
}
// Add the remaining carry if any
if (carry != 0) {
sumHead = insertEnd(sumHead, carry);
}
return sumHead;
}
// Function to display the binary number
void displayBinaryNumber(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data;
temp = temp->next;
}
cout << endl;
}
int main() {
Node* binaryNumber1 = NULL;
Node* binaryNumber2 = NULL;
// Input first binary number
cout << "Enter binary number 1: ";
string num1;
cin >> num1;
for (int i = 0; i < num1.length(); i++) {
int digit = num1[i] - '0';
binaryNumber1 = insertEnd(binaryNumber1, digit);
}
// Input second binary number
cout << "Enter binary number 2: ";
string num2;
cin >> num2;
for (int i = 0; i < num2.length(); i++) {
int digit = num2[i] - '0';
binaryNumber2 = insertEnd(binaryNumber2, digit);
}
cout << "Binary Number 1: ";
displayBinaryNumber(binaryNumber1);
cout << "Binary Number 2: ";
displayBinaryNumber(binaryNumber2);
cout << "1's Complement of Binary Number 1: ";
onesComplement(binaryNumber1);
displayBinaryNumber(binaryNumber1);
cout << "2's Complement of Binary Number 2: ";
twosComplement(binaryNumber2);
displayBinaryNumber(binaryNumber2);
cout << "Sum of Binary Number 1 and Binary Number 2: ";
Node* sum = addBinaryNumbers(binaryNumber1, binaryNumber2);
displayBinaryNumber(sum);
return 0;
}