-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operations.c
281 lines (270 loc) · 9.93 KB
/
Operations.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
#include<stdio.h>
#include"Number_List.h"
//get difference of two number lists
NUMBER_LIST subNumbers(NUMBER_LIST num1 , NUMBER_LIST num2 ){// num1 - num2
removeInsignificantZeroes(num1);
removeInsignificantZeroes(num2);
makeNumberLengthsEqual(num1, num2);
NUMBER_LIST difference;
init_num(&difference);
// if(num1->len_dec >= num2->len_dec) difference->len_dec = num1->len_dec;
// else difference->len_dec = num2->len_dec;
if(num1->sign == num2->sign){//numbers have same sign
if(num1->sign == PLUS){// both have + sign
if(getBiggerNumber(num1, num2) == NUMBER1){
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
while(ptr1 !=NULL){
if(ptr1->digit >= ptr2->digit){
addDigitAtStartOfNumberList(difference, (ptr1->digit)-(ptr2->digit));
}
else{
ptr1->prev->digit--;
ptr1->digit += 10;
addDigitAtStartOfNumberList(difference, (ptr1->digit)-(ptr2->digit));
}
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
return difference;
}
else if(getBiggerNumber(num1, num2) == NUMBER2){
NUMBER_LIST_NODE ptr2 = num2->tail;
NUMBER_LIST_NODE ptr1 = num1->tail;
while(ptr2 != NULL){
if(ptr2->digit >= ptr1->digit){
addDigitAtStartOfNumberList(difference, ptr2->digit - ptr1->digit);
}
else{
ptr2->prev->digit--;
ptr2->digit += 10;
addDigitAtStartOfNumberList(difference, ptr2->digit - ptr1->digit);
}
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
difference->sign = MINUS;
return difference;
}
else if(getBiggerNumber(num1, num2) == EQUAL){
addDigitAtStartOfNumberList(difference , 0);
return difference;
}
}
else if(num1->sign == MINUS){// both have - sign
if(getBiggerNumber(num1, num2) == NUMBER1){
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
while(ptr1!= NULL){
if(ptr1->digit >= ptr2->digit){
addDigitAtStartOfNumberList(difference, ptr1->digit - ptr2->digit);
}
else{
ptr1->prev->digit--;
ptr1->digit+=10;
addDigitAtStartOfNumberList(difference, ptr1->digit - ptr2->digit);
}
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
difference->sign = MINUS;
return difference;
}
else if(getBiggerNumber(num1, num2) == NUMBER2){
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
while(ptr2!= NULL){
if(ptr2->digit >= ptr1->digit){
addDigitAtStartOfNumberList(difference, ptr2->digit - ptr1->digit);
}
else{
ptr2->prev->digit--;
ptr2->digit+=10;
addDigitAtStartOfNumberList(difference, ptr2->digit - ptr1->digit);
}
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
return difference;
}
else if(getBiggerNumber(num1, num2) == EQUAL){
addDigitAtStartOfNumberList(difference, 0);
return(difference);
}
}
}
else if(num1->sign == MINUS){
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
int carry = 0;
while(ptr1!=NULL){
addDigitAtStartOfNumberList(difference, (carry + ptr1->digit + ptr2->digit)%10);
carry = (carry + ptr1->digit + ptr2->digit)/10;
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
difference->sign = MINUS;
return difference;
}
else{
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
int carry = 0;
while(ptr1!=NULL){
addDigitAtStartOfNumberList(difference, (carry + ptr1->digit + ptr2->digit)%10);
carry = (carry + ptr1->digit + ptr2->digit)/10;
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
difference->sign = PLUS;
return difference;
}
}
//get sum of two number lists
NUMBER_LIST addNumbers(NUMBER_LIST num1 , NUMBER_LIST num2){
removeInsignificantZeroes(num1);
removeInsignificantZeroes(num2);
makeNumberLengthsEqual(num1, num2);
NUMBER_LIST sumOfNum1Num2;// sum of numbers num1 and num2
init_num(&sumOfNum1Num2);
//if(num1->len_dec >= num2->len_dec) sumOfNum1Num2->len_dec = num1->len_dec;
//else sumOfNum1Num2->len_dec = num2->len_dec;
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
if(num1->sign == num2->sign){// numbers have the same sign
int carry = 0;
while(ptr1 != NULL){
addDigitAtStartOfNumberList(sumOfNum1Num2, (carry + (ptr1->digit) + (ptr2->digit))%10);
carry = (carry + (ptr1->digit) + (ptr2->digit)) /10;
ptr1 = ptr1->prev;
ptr2 = ptr2->prev;
}
addDigitAtStartOfNumberList(sumOfNum1Num2 , carry);
sumOfNum1Num2->sign = num1->sign;
return(sumOfNum1Num2);
}
else if(num1->sign == PLUS){
num2->sign = PLUS;
sumOfNum1Num2 = subNumbers(num1, num2);
return sumOfNum1Num2;
}
else{
num1->sign = PLUS;
sumOfNum1Num2 = subNumbers(num2, num1);
return sumOfNum1Num2;
}
}
//get product of two number lists
NUMBER_LIST multiplyNumbers(NUMBER_LIST num1, NUMBER_LIST num2){
removeInsignificantZeroes(num1);
removeInsignificantZeroes(num2);
NUMBER_LIST finalSum;
NUMBER_LIST currentSum;
init_num(&finalSum);
init_num(¤tSum);
append(finalSum, '0');
NUMBER_LIST_NODE ptr1 = num1->tail;
NUMBER_LIST_NODE ptr2 = num2->tail;
int numberOfZeroesToBeAdded = 0;// to match order of number
while(ptr1!=NULL){
ptr2 = num2->tail;
while(ptr2!=NULL){
int binaryProduct = (ptr2->digit) * (ptr1->digit);
addDigitAtStartOfNumberList(currentSum, binaryProduct);
ptr2 = ptr2->prev;
}
addZeroesAtEnd(currentSum, numberOfZeroesToBeAdded);
makeEachDigitLessThanTen(currentSum);
finalSum = addNumbers(finalSum, currentSum);
init_num(¤tSum);
numberOfZeroesToBeAdded+=1;
ptr1 = ptr1->prev;
}
if(num1->sign == num2->sign) finalSum->sign = PLUS;
else finalSum->sign = MINUS;
return finalSum;
}
//get quotient when num1 is divided by num2
NUMBER_LIST divideNumbers(NUMBER_LIST num1, NUMBER_LIST num2){
if(isZero(num2) == 1){
printf("%s" , "Division by zero");
return NULL;
}
NUMBER_LIST quo;
init_num(&quo);
if(getBiggerNumber(num1, num2) == NUMBER2){
append(quo, '0');
return quo;
}
else if(getBiggerNumber(num1, num2) == EQUAL){
append(quo, '1');
return quo;
}
else{
removeInsignificantZeroes(num1);
removeInsignificantZeroes(num2);
NUMBER_LIST_NODE ptr1 = num1->head;
NUMBER_LIST_NODE ptr2 = num2->head;
NUMBER_LIST subList;
init_num(&subList);
append(subList, '0');
int intermediateQuo = 0;
while(ptr1 != NULL){
append(subList, 48 + ptr1->digit);
ptr1 = ptr1->next;
while(getBiggerNumber(subList, num2) == NUMBER1 || getBiggerNumber(subList, num2) == EQUAL){
subList = subNumbers(subList, num2);
intermediateQuo++;
}
append(quo, 48 + intermediateQuo);
intermediateQuo = 0;
}
}
if(num1->sign == num2->sign) quo->sign = PLUS;
else quo->sign = MINUS;
return quo;
}
//get remainder when num1 is divided by num2;
NUMBER_LIST getRemainder(NUMBER_LIST num1, NUMBER_LIST num2){
NUMBER_LIST remainder;
init_num(&remainder);
if(getBiggerNumber(num1, num2) == NUMBER2){
remainder = num1;
return remainder;
}
if(isZero(num1) == 1){
append(remainder , '0');
return remainder;
}
if(isZero(num2) == 1){
printf("%s", "Division by Zero!");
return NULL;
}
remainder = subNumbers(num1, multiplyNumbers(divideNumbers(num1, num2), num2));
return remainder;
}
//get num1 raised to num2
NUMBER_LIST getExponent(NUMBER_LIST num1, NUMBER_LIST num2){
NUMBER_LIST ans;
init_num(&ans);
if(isZero(num1)){
append(ans, '0');
return ans;
}
if(isZero(num2)){
append(ans, '0');
return ans;
}
else{
append(ans, '1');
NUMBER_LIST oneList;
init_num(&oneList);
append(oneList, '1');
while(isZero(num2) == 0){
num2 = subNumbers(num2, oneList);
ans = multiplyNumbers(ans, num1);
}
if(num1->sign == MINUS && (num2->tail->digit %2 != 0)) ans->sign = MINUS;
return ans;
}
}