-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigNum Division.cpp
More file actions
294 lines (249 loc) · 4.09 KB
/
Copy pathBigNum Division.cpp
File metadata and controls
294 lines (249 loc) · 4.09 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
/**
Big Number Division
Write a C++ Program to know if Division is possible of two BigNum or not
**/
#include <iostream>
#include <string>
using namespace std;
struct node
{
int num;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR head1=NULL;
NODEPTR head2=NULL;
NODEPTR head3=NULL;
struct node* node_creation()
{
struct node *p;
p=(NODEPTR)malloc(sizeof(struct node));
return p;
}
void enter_elements(struct node *temp, int n)
{
struct node *p;
p=node_creation();
p->num=n;
p->next=NULL;
if(temp->next==NULL)
{
temp->next=p;
}
else
{
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
void display(struct node *temp)
{
struct node *n;
n = temp->next;
if(n==NULL)
{}
else
{
while(n != NULL)
{
cout<<n->num;
n=n->next;
}
}
}
int count_nodes(struct node *temp)
{
int count = 0;
struct node *n, *t;
t = temp->next;
if(t->num == 0)
{
while(t->next != NULL)
{
if(t->num != 0)
{break;}
else
{delete(t);}
t = t->next;
}
temp->next=t;
}
n = temp->next;
while(n != NULL)
{
count = count+1;
n=n->next;
}
return count;
}
void reverse(struct node *temp)
{
struct node *previous = NULL;
struct node *current = temp->next;
struct node *forward;
while(current !=NULL)
{
forward = current->next;
current->next = previous;
previous = current;
current = forward;
}
temp->next = previous;
}
void delete_ll(struct node *temp)
{
struct node *t, *n;
t=temp->next;
while(t != NULL)
{
delete(t);
t = t->next;
}
temp->next=NULL;
}
string removeZero(string str)
{
int i = 0;
while (str[i] == '0')
{i++;}
str.erase(0, i);
return str;
}
int get_bigger_list(struct node *temp1, struct node *temp2)
{
struct node *n1, *n2;
int nodes_list1 = count_nodes(temp1);
int nodes_list2 = count_nodes(temp2);
if(nodes_list1 > nodes_list2)
{
return 1;
}
else if(nodes_list1 < nodes_list2)
{
cout << "CANNOT BE DIVIDED" << endl;
exit(0);
}
else
{
n1=temp1->next;
n2=temp2->next;
while(n1 != NULL && n2 !=NULL)
{
if (n1->num > n2->num)
{
return 1;
}
else if (n1->num < n2->num)
{
cout << "CANNOT BE DIVIDED" << endl;
exit(0);
}
else
{
n1=n1->next; n2=n2->next;
}
}
cout << "BOTH CAN BE DIVIDED" << endl;
exit(0);
}
}
void copylist(struct node *temp3)
{
struct node *list3, *list1;
delete_ll(head1);
list3 = temp3->next;
while (list3 != NULL)
{
int num = list3->num;
enter_elements(head1, num);
list3 = list3->next;
}
delete_ll(head3);
}
void minus_two(struct node *temp1, struct node *temp2)
{
int bigger = get_bigger_list(temp1, temp2);
if(bigger == 1)
{
reverse(temp1);
reverse(temp2);
struct node *list1, *list2, *t;
list1 = temp1->next;
list2 = temp2->next;
int num;
while (list2 != NULL)
{
if (list1->num < list2->num)
{
list1->num = list1->num + 10;
t=list1->next;
if(t->num == 0)
{
while(t->next != NULL)
{
if(t->num != 0)
{break;}
else
{t->num = 9;}
t = t->next;
}
t->num = t->num - 1;
}
else
list1->next->num = list1->next->num - 1;
num = list1->num - list2->num;
enter_elements(head3, num);
}
else
{
num = list1->num - list2->num;
enter_elements(head3, num);
}
list2=list2->next;
list1=list1->next;
}
while (list1 != NULL)
{
num = list1->num;
enter_elements(head3, num);
list1=list1->next;
}
}
reverse(head1);
reverse(head2);
reverse(head3);
count_nodes(head3);
//display(head1); cout << " - "; display(head2);cout << "= ";display(head3);
//cout << "\n";
copylist(head3);
}
int main()
{
head1 = (NODEPTR)malloc(sizeof(struct node));
head2 = (NODEPTR)malloc(sizeof(struct node));
head3 = (NODEPTR)malloc(sizeof(struct node));
string str1, str2;
cout<<"Enter Numerator: ";
cin>>str1;
cout<<"Enter Denominator: ";
cin>>str2;
str1 = removeZero(str1);
str2 = removeZero(str2);
for(char& c : str1)
{
int i = c - '0';
enter_elements(head1, i);
}
for(char& c : str2) {
int i = c - '0';
enter_elements(head2, i);
}
while(1)
{
minus_two(head1, head2);
}
return 0;
}