-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBT19CSE118_Assign2(Code).c
348 lines (337 loc) · 8.06 KB
/
BT19CSE118_Assign2(Code).c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 500//1024 bits means nearly no of 309 digits
void add(char a[], char b[], int flag);
void strRev(char a[])
{
int n = strlen(a);
for(int i = 0; i < n/2; ++i)
{
char temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
}
void curtail_zeroes(char a[])
{
int n = strspn(a, "0");
char s[500];
int j = 0;
for(int i = n; a[i] != '\0'; ++i)
{
s[j++] = a[i];
}
s[j] = '\0';
strcpy(a, s);
}
void multiply(char str1[], char str2[])
{
int flag = 1;
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 + len2 > 310)
{
printf("Overflow\n");
return;
}
if((len1 == 1 && str1[0] == '0') || (len2 == 1 && str2[0] == '0'))
{
printf("0\n");
return;
}
else
{
if(str1[0] == '-' && str2[0] == '-')
{
str1[0] = '0';
str2[0] = '0';
flag = 1;
}
else if(str1[0] == '-')
{
str1[0] = '0';
flag = 0;
}
else if(str2[0] == '-')
{
str2[0] = '0';
flag = 0;
}
curtail_zeroes (str1);
curtail_zeroes (str2);
len1 = strlen(str1);
len2 = strlen(str2);
int ans[SIZE] = {0};
for(int i = len1 - 1; i >= 0; i--)
{
for(int j = len2 - 1; j >= 0; j--)
{
ans[i+j+1]+=(str1[i]-'0')*(str2[j]-'0');
ans[i+j]+=ans[i+j+1]/10;
ans[i+j+1]%=10;
}
}
int i = 0;
while(i < len1 + len2 && ans[i] == 0)
i++;
char s[SIZE];
int j = 0;
while(i < len1 + len2)
{
s[j++] = ans[i++] + '0';
}
s[j] = '\0';
curtail_zeroes (s);
if(strlen(s) == 0)
{
printf("0\n");
return;
}
printf("Answer is: \n");
if(flag == 0)
printf("-");
printf("%s\n", s);
return;
}
return;
}
int isSmaller(char str1[], char str2[])
{
// Calculate lengths of both string
int n1 = strlen(str1), n2 = strlen(str2);
if(n1 < n2)
return 1;
if(n2 < n1)
return 0;
for (int i = 0; i < n1; i++)
if (str1[i] < str2[i])
return 1;
else if (str1[i] > str2[i])
return 0;
return 0;
}
void findDiff(char str1[], char str2[])
{
int flag = 0;
if(str2[0] == '-' && str1[0] != '-')
{
str2[0] = '0';
curtail_zeroes (str2);
add(str1, str2, 0);
}
else if(str1[0] == '-' && str2[0] != '-')
{
str1[0] = '0';
curtail_zeroes (str1);
add(str1, str2, 1);
}
else
{
if(str1[0] == '-' && str2[0] == '-')
{
str1[0] = '0';
str2[0] = '0';
curtail_zeroes (str1);
curtail_zeroes (str2);
findDiff(str2, str1);
}
else
{
if (isSmaller(str1, str2))
{
char temp[SIZE];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
flag = 1;
}
char str[SIZE] = "";
int n1 = strlen(str1), n2 = strlen(str2);
strRev(str1);
strRev(str2);
int carry = 0;
int k = 0;
// Run loop till small string length
// and subtract digit of str1 to str2
for (int i = 0; i < n2; i++)
{
int sub = ((str1[i] - '0') - (str2[i] - '0') - carry);
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str[k++] = sub + '0';
if(k > 310)
{
printf("Overflow\n");
return ;
}
}
// subtract remaining digits of larger number
for (int i = n2; i < n1; i++)
{
int sub = ((str1[i] - '0') - carry);
// if the sub value is -ve, then make it positive
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str[k++] = sub + '0';
if(k > 310)
{
printf("Overflow\n");
return ;
}
}
str[k] = '\0';
// reverse resultant string
strRev(str);
curtail_zeroes (str);
printf("Answer is: \n");
if(flag == 1)
printf("-");
printf("%s\n", str);
}
}
}
void add(char a[], char b[], int flag)
{
int limitm = 0, limitn = 0;
if(a[0] == '-' && b[0] == '-')
{
limitm = 1;
limitn = 1;
}
else if(a[0] == '-')
{
a[0] = '0';
curtail_zeroes (a);
return findDiff(b, a);
}
else if(b[0] == '-')
{
b[0] = '0';
curtail_zeroes (b);
return findDiff(a, b);
}
int m = strlen(a);
int n = strlen(b);
char ans[SIZE];
int i = 0;
int carry = 0;
while(m > limitm && n > limitn)
{
int x = a[m - 1] - '0';
int y = b[n - 1] - '0';
int val = x + y + carry;
carry = val / 10;
val = val % 10;
char c = val + '0';
ans[i] = c;
i++;
m--;
n--;
if(i > 310)
{
printf("Overflow\n");
return ;
}
}
while(m > limitm)
{
int x = a[m - 1] - '0';
int val = x + carry;
carry = val / 10;
val = val % 10;
char c = val + '0';
ans[i] = c;
i++;
m--;
if(i > 310)
{
printf("Overflow\n");
return ;
}
}
while(n > limitn)
{
int x = b[n - 1] - '0';
int val = x + carry;
carry = val / 10;
val = val % 10;
char c = val + '0';
ans[i] = c;
i++;
n--;
if(i > 310)
{
printf("Overflow\n");
return ;
}
}
if(carry > 0)
{
ans[i] = carry + '0';
i++;
if(i > 310)
{
printf("Overflow\n");
return ;
}
}
ans[i] = '\0';
strRev(ans);
curtail_zeroes (ans);
if(strlen(ans) == 0)
{
printf("0\n");
return;
}
printf("Answer is: \n");
if((limitm == 1 && limitn == 1) || (flag == 1))
printf("-");
printf("%s\n", ans);
}
int main()
{
while(1)
{
printf("*******************************************************************************\n");
printf("If you want to\nadd-enter 1\nsubtract-enter 2\nmultiply-enter 3\nexit-enter 4\n");
int n;
scanf("%d", &n);
if(n < 1 || n > 3)
return 0;
printf("--------------------------------------------------------------------------------\n");
char a[SIZE], b[SIZE];
printf("Enter 1st number\n");
scanf("%s", a);
printf("Enter 2nd number\n");
scanf("%s", b);
int p = strlen(a);
int q = strlen(b);
if(p > 310 || q > 310)
{
printf("Overflow\n");
}
else
{
if(n == 1)
add(a, b, 0);
else if(n == 2)
findDiff(a, b);
else if(n == 3)
multiply(a, b);
else
return 0;
}
}
return 0;
}