-
Notifications
You must be signed in to change notification settings - Fork 0
/
largeintcalculator.c
1407 lines (1188 loc) · 35.3 KB
/
largeintcalculator.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct digit Digit;
struct digit {
int Data;
Digit *Next;
Digit *Pre;
};
typedef struct results Queue;
typedef struct results QueueResults;
typedef struct results QueueMultiply;
typedef struct results QueueRemainder;
struct results {
Digit *res;
QueueResults *Next;
QueueResults *Pre;
};
////////////////// list
Digit *MakeEmpty(Digit *);
void DisposeList(Digit *);
int IsEmpty(Digit *);
int IsLast(Digit *, Digit *);
Digit *FindLast(Digit *);
void InsertLast(int, Digit *L);
void InsertFirst(int, Digit *);
void DeleteList(Digit *);
void PrintList(Digit *);
int Size(Digit *);
Digit *Duplicate(Digit *);
void PrintListToFile(FILE *, Digit *);
///////////// read
int numberCount();
Digit **readFile();
void DisposeArray(Digit **, int);
void PrintNumbers(Digit **, int);
///////////////////// calculator
void mainMenu();
void AddTwoNumbers(Digit *, Digit *, QueueResults *);
int Compare(Digit *, Digit *);
void MultiplyTwoNumbers(Digit *, Digit *, QueueResults *);
void SubtractTwoNumbers(Digit *, Digit *, QueueResults *);
Digit *Subtract(Digit *, Digit *);
Digit *AddNumbers(Digit *, Digit *);
void DivideTwoNumbers(Digit *, Digit *, QueueResults *, QueueRemainder *);
void RemoveZeros(Digit *);
////////// Queue
Queue *MakeEmptyQueue(Queue *);
void DisposeQueue(Queue *);
int IsEmptyQueue(Queue *);
void Enqueue(Digit *, Queue *);
Digit *Dequeue(Queue *);
void DeleteQueue(Queue *);
int IsLastQueue(Queue *, Queue *);
QueueResults *FindLastQueue(Queue *);
void PrintQueue(QueueResults *, QueueRemainder *);
int main() {
// Get the number of digits for the input numbers
int nums = 0;
// Allocate memory for the input numbers and the results queue
Digit **numbers = NULL;
// Make Empty Queue to store the results
QueueResults *Results = NULL;
Results = MakeEmptyQueue(Results);
// Make Empty Queue to store the remainders
QueueResults *Remainders = NULL;
Remainders = MakeEmptyQueue(Remainders);
int x;
int y;
while (1) // always ask the user for the choice
{
int choice;
// Display the main menu
mainMenu();
printf("What do you want to do: ");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 1:
if (numbers != NULL) // if it was allocated before free it
DisposeArray(numbers, nums);
nums = numberCount();
// Read the input numbers from file
printf("Reading numbers......\n");
numbers = readFile();
if (numbers != NULL)
printf("The numbers have been read\n");
// to check if there is empty number and delete it
for (int i = 0; i < nums; i++) {
if (numbers[i]->Next == NULL ||
(numbers[i]->Next->Data == 1 &&
numbers[i]->Next->Next == NULL)) {
for (int j = i; j < nums - 1; j++) {
numbers[j] = numbers[j + 1];
}
nums--; // Decrement the number of elements in the array
i--;
}
RemoveZeros(numbers[i]);
}
if (nums < 2) // Check that there are at least two numbers
{
printf("\nThis file must have at least two numbers\n");
exit(0);
}
break;
case 2:
// Check that input numbers have been read first
if (numbers == NULL) {
printf("\nYou should read the numbers first into input file "
"(input.txt) then choose the first option...\n");
break;
}
// Get the indices of the numbers to add
printf("Which numbers do you want to add (1, 2, ...)?\n");
// print the numbers to the user to choose
PrintNumbers(numbers, nums);
printf("First number: ");
scanf("%d", &x);
while (x > nums || x < 0) // to check if the first number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("First number: ");
scanf("%d", &x);
}
printf("Second number: ");
scanf("%d", &y);
while (y > nums || y < 0) // to check if the second number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("Second number: ");
scanf("%d", &y);
}
// Add the numbers and store the result in the results queue
AddTwoNumbers(numbers[--x], numbers[--y], Results);
Enqueue(NULL, Remainders);
break;
case 3:
// Check that input numbers have been read first
if (numbers == NULL) {
printf("\nYou should read the numbers first...\n");
break;
}
// Get the indices of the numbers to add
printf("Which numbers do you want to subtract (1, 2, ...)?\n");
// print the numbers to the user to choose
PrintNumbers(numbers, nums);
printf("First number: ");
scanf("%d", &x);
while (x > nums || x < 0) // to check if the first number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("First number: ");
scanf("%d", &x);
}
printf("Second number: ");
scanf("%d", &y);
while (y > nums || y < 0) // to check if the second number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("Second number: ");
scanf("%d", &y);
}
// Add the numbers and store the result in the results queue
SubtractTwoNumbers(numbers[--x], numbers[--y], Results);
Enqueue(NULL, Remainders);
break;
case 4:
// Check that input numbers have been read first
if (numbers == NULL) {
printf("\nYou should read the numbers first...\n");
break;
}
// Get the indices of the numbers to multiply
printf("Which numbers do you want to multiply (1, 2, ...)?\n");
// print the numbers to the user to choose
PrintNumbers(numbers, nums);
printf("First number: ");
scanf("%d", &x);
while (x > nums || x < 0) // to check if the first number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("First number: ");
scanf("%d", &x);
}
printf("Second number: ");
scanf("%d", &y);
while (y > nums || y < 0) // to check if the first number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("Second number: ");
scanf("%d", &y);
}
// Multiply the numbers and store the result in the results queue
MultiplyTwoNumbers(numbers[--x], numbers[--y], Results);
Enqueue(NULL, Remainders);
break;
case 5:
// Check that input numbers have been read first
if (numbers == NULL) {
printf("\nYou should read the numbers first...\n");
break;
}
// Get the indices of the numbers to Divide
printf("Which numbers do you want to Divide (1, 2, ...)?\n");
// print the numbers to the user to choose
PrintNumbers(numbers, nums);
printf("First number: ");
scanf("%d", &x);
while (x > nums || x < 0) // to check if the first number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("First number: ");
scanf("%d", &x);
}
printf("Second number: ");
scanf("%d", &y);
while (y > nums || y < 0) // to check if the first number is exists
// if not ask the user to enter it again
{
printf("Enter a number between 1 and %d\n", nums);
printf("Second number: ");
scanf("%d", &y);
}
// Divide the numbers and store the result in the results queue
DivideTwoNumbers(numbers[--x], numbers[--y], Results, Remainders);
break;
case 6:
// Print the results queue
PrintQueue(Results, Remainders);
printf("\nThe results Have been printed to the output file!\n");
break;
case 7:
// Free the memory allocated for the input numbers and the results
// queue
if (numbers != NULL) {
printf("....Disposing Lists\n");
DisposeArray(numbers, nums);
DisposeQueue(Results);
}
exit(0);
break;
default:
printf("Enter a choice between 1 and 7\n");
}
}
return 0;
}
/*
* Function: MakeEmpty
* --------------------
* Creates a new empty doubly linked list and returns a pointer to it.
*
* Returns:
* A pointer to the newly created empty linked list.
*/
Digit *MakeEmpty(Digit *L) {
if (L != NULL)
DeleteList(L);
L = (Digit *)malloc(sizeof(Digit));
if (L == NULL)
printf("Out Of Memory");
L->Next = NULL;
L->Pre = NULL;
return L;
}
/*
* Function: IsEmpty
* -----------------
* Checks if the given linked list is empty.
*
* Parameters:
* L: Pointer to the linked list.
*
* Returns:
* 1 if the list is empty, 0 otherwise.
*/
int IsEmpty(Digit *L) {
return L->Next == NULL; // returns 1 if the list is empty
}
/*
* Function: IsLast
* ----------------
* Checks if the given node in the linked list is the last node.
*
* Parameters:
* P: Pointer to the node in the linked list.
* L: Pointer to the linked list.
*
* Returns:
* 1 if the node is the last node, 0 otherwise.
*/
int IsLast(Digit *P, Digit *L) { return P->Next == NULL; }
/*
* Function: DeleteList
* --------------------
* Frees memory occupied by all nodes in the linked list.
*
* Parameters:
* L: Pointer to the linked list.
*/
void DeleteList(Digit *L) {
Digit *P, *temp;
P = L->Next;
L->Next = NULL;
while (P != NULL) {
temp = P->Next;
free(P);
P = temp;
}
}
/*
* Function: PrintList
* -------------------
* Prints the data of all nodes in the linked list.
*
* Parameters:
* L: Pointer to the linked list.
*/
void PrintList(Digit *L) {
Digit *P = L->Next;
if (IsEmpty(L)) {
printf("\nThe List Is Empty\n");
} else {
if (P->Data == 1) {
printf("-");
}
do {
P = P->Next;
printf("%d", P->Data);
} while (!IsLast(P, L));
}
}
/*
* Function: InsertLast
* --------------------
* Inserts a new node with data X at the end of the linked list.
*
* Parameters:
* X: Data to be inserted.
* L: Pointer to the linked list.
*/
void InsertLast(int X, Digit *L) {
Digit *temp;
temp = (Digit *)malloc(sizeof(Digit));
temp->Data = X;
if (!IsEmpty(L)) {
Digit *P = FindLast(L);
temp->Next = P->Next;
P->Next = temp;
temp->Pre = P;
} else {
temp->Pre = L;
temp->Next = NULL;
L->Next = temp;
}
}
/*
* Function: InsertFirst
* ---------------------
* Inserts a new node with data X at the beginning of the linked list.
*
* Parameters:
* X: Data to be inserted.
* L: Pointer to the linked list.
*/
void InsertFirst(int X, Digit *L) {
Digit *temp;
temp = (Digit *)malloc(sizeof(Digit));
temp->Data = X;
if (!IsEmpty(L)) {
Digit *SignNode = L->Next;
if (!IsEmpty(SignNode)) {
SignNode->Next->Pre = temp;
temp->Pre = SignNode;
temp->Next = SignNode->Next;
SignNode->Next = temp;
} else {
temp->Pre = SignNode;
SignNode->Next = temp;
temp->Next = NULL;
}
} else {
temp->Pre = L;
L->Next = temp;
temp->Next = NULL;
}
}
/*
* Function: Size
* --------------
* Returns the number of digits in the linked list.
*
* Parameters:
* L: Pointer to the linked list.
*
* Returns:
* Number of digits in the linked list.
*/
int Size(Digit *L) {
int count = 0;
Digit *P = L;
while (P->Next != NULL) {
count++;
P = P->Next;
}
return --count;
}
/*
* Function: FindLast
* ------------------
* Returns the last node in the linked list.
*
* Parameters:
* L: Pointer to the linked list.
*
* Returns:
* Pointer to the last node in the linked list.
*/
Digit *FindLast(Digit *L) {
Digit *P = L;
while (P->Next != NULL)
P = P->Next;
return P;
}
/*
* Function: DisposeList
* ---------------------
* Frees memory occupied by the linked list.
*
* Parameters:
* L: Pointer to the linked list.
*/
void DisposeList(Digit *L) {
DeleteList(L);
free(L);
}
/*
* Function: numberCount
* ---------------------
* Counts the number of lines in the input file, indicating the number of numbers.
*
* Returns:
* Number of lines (numbers) in the input file.
*/
int numberCount() {
FILE *fin = fopen("input.txt", "r");
char ch;
int lines = 0;
if (fin == NULL) {
printf("Failed to open file: \n");
return 1;
}
while ((ch = fgetc(fin)) != EOF) {
// If we find a newline character, increment the line counter
if (ch == '\n') {
lines++;
}
}
fclose(fin);
return lines;
}
/*
* Function: readFile
* ------------------
* Reads numbers from the input file and stores them in an array of linked lists.
*
* Returns:
* Array of pointers to the linked lists containing the read numbers.
*/
Digit **readFile() {
int numcount = numberCount();
Digit **numbers = (Digit **)malloc(sizeof(Digit *) * numcount);
for (int i = 0; i < numcount; i++) {
numbers[i] = MakeEmpty(NULL);
}
FILE *fin = fopen("input.txt", "r");
if (fin == NULL) {
printf("Failed to open file: \n");
exit(1);
}
char c = fgetc(fin);
int i = 0, flagNewNum = 1;
while (c != EOF) {
if (c == '\n') {
i++;
flagNewNum = 1;
} else {
if (flagNewNum == 1 && c == '-')
InsertFirst(1, numbers[i]);
else if (flagNewNum == 1)
InsertFirst(0, numbers[i]);
flagNewNum = 0;
int dig = c - 48;
if (dig >= 0 && dig <= 9)
InsertLast(dig, numbers[i]);
}
c = fgetc(fin);
}
fclose(fin);
return numbers;
}
/*
* Function: PrintNumbers
* ----------------------
* Prints the list of digits along with their order numbers.
*
* Parameters:
* Numbers: Array of pointers to the linked lists containing the numbers.
* length: Number of numbers in the array.
*/
void PrintNumbers(Digit **Numbers, int length) {
for (int i = 0; i < length; i++) {
printf("number %d): ", i + 1);
PrintList(Numbers[i]);
printf("\n");
}
}
/*
* Function: Duplicate
* -------------------
* Creates a copy of the given linked list and returns a pointer to the new list.
*
* Parameters:
* L: Pointer to the linked list to be duplicated.
*
* Returns:
* Pointer to the duplicated linked list.
*/
Digit *Duplicate(Digit *L) {
Digit *t = NULL, *P;
P = L;
t = MakeEmpty(t);
while (P->Next != NULL) {
InsertLast(P->Next->Data, t);
P = P->Next;
}
return t;
}
/*
* Function: DisposeArray
* ----------------------
* Frees memory occupied by the array of linked lists.
*
* Parameters:
* arr: Array of pointers to the linked lists.
* length: Number of linked lists in the array.
*/
void DisposeArray(Digit **arr, int length) {
for (int i = 0; i < length; i++) {
DisposeList(arr[i]);
}
free(arr);
}
/*
* Function: mainMenu
* ------------------
* Prints the main menu options to the console.
*/
void mainMenu() {
printf("\nChoose from the below operations:\n\n");
printf("1. Read the numbers from File\n");
printf("2. Add Numbers\n");
printf("3. Subtract Numbers\n");
printf("4. Multiply Numbers\n");
printf("5. Divide Numbers\n");
printf("6. print the results to an output file\n");
printf("7. Exit\n");
}
/*
* Function: AddNumbers
* --------------------
* Adds two numbers represented by linked lists and enqueues the result.
*
* Parameters:
* FirstNum: Pointer to the first number's linked list.
* SecondNum: Pointer to the second number's linked list.
* Results: Pointer to the results queue.
*/
Digit *AddNumbers(Digit *FirstNum, Digit *SecondNum) {
Digit *result = MakeEmpty(NULL);
InsertFirst(0, result);
Digit *num1 = FindLast(FirstNum);
Digit *num2 = FindLast(SecondNum);
int res;
int Cin = 0; // carry in
while (num1 != FirstNum->Next &&
num2 != SecondNum->Next) // iterates until reaching sign node in
// first number or the second
{
res = num1->Data + num2->Data + Cin;
if (res < 10) {
InsertFirst(res, result);
Cin = 0;
} else {
res -= 10;
InsertFirst(res, result);
Cin = 1;
}
num1 = num1->Pre;
num2 = num2->Pre;
}
// if it reach the sign node in the first number but not in the second i it
// iterates until reaching the sign node in the second number
while (num1 == FirstNum->Next && num2 != SecondNum->Next) {
res = num2->Data + Cin;
if (res < 10) {
InsertFirst(res, result);
Cin = 0;
} else {
res -= 10;
InsertFirst(res, result);
Cin = 1;
}
num2 = num2->Pre;
}
// if it reach the sign node in the second number but not in the second i it
// iterates until reaching the sign node in the first number
while (num1 != FirstNum->Next && num2 == SecondNum->Next) {
res = num1->Data + Cin;
if (res < 10) {
InsertFirst(res, result);
Cin = 0;
} else {
res -= 10;
InsertFirst(res, result);
Cin = 1;
}
num1 = num1->Pre;
}
// if there was carry after finishing the digits just insert it after the
// sign node
if (Cin != 0)
InsertFirst(Cin, result);
else
result->Next->Data = 0;
return result;
}
/*
* Function: AddTwoNumbers
* -----------------------
* Takes two numbers from the user, decides the appropriate operation based on their signs,
* performs addition, enqueues the result, and prints it.
*
* Parameters:
* FirstNum: Pointer to the first number's linked list.
* SecondNum: Pointer to the second number's linked list.
* Results: Pointer to the results queue.
*/
void AddTwoNumbers(Digit *FirstNum, Digit *SecondNum, QueueResults *Results) {
Digit *result = MakeEmpty(NULL);
if (FirstNum->Next->Data == 0 && SecondNum->Next->Data == 0) {
result = AddNumbers(FirstNum, SecondNum);
} else if (FirstNum->Next->Data == 1 && SecondNum->Next->Data == 1) {
result = AddNumbers(FirstNum, SecondNum);
result->Next->Data = 1;
} else if (FirstNum->Next->Data == 0 && SecondNum->Next->Data == 1) {
if (Compare(FirstNum, SecondNum) > 0)
result = Subtract(FirstNum, SecondNum);
else if (Compare(FirstNum, SecondNum) < 0) {
result = Subtract(SecondNum, FirstNum);
result->Next->Data = 1;
} else {
InsertFirst(0, result);
InsertFirst(0, result);
}
} else {
if (Compare(FirstNum, SecondNum) > 0) {
result = Subtract(FirstNum, SecondNum);
result->Next->Data = 1;
} else if (Compare(FirstNum, SecondNum) < 0)
result = Subtract(SecondNum, FirstNum);
else {
InsertFirst(0, result);
InsertFirst(0, result);
}
}
RemoveZeros(result);
printf("\n");
printf("The Result = ");
PrintList(result);
printf("\n");
Enqueue(result, Results);
}
/*
* Function: Compare
* -----------------
* Compares two numbers represented by linked lists.
*
* Parameters:
* FirstNum: Pointer to the first number's linked list.
* SecondNum: Pointer to the second number's linked list.
*
* Returns:
* 1 if the first number is greater, -1 if the second number is greater, 0 if equal.
*/
int Compare(Digit *FirstNum, Digit *SecondNum) // num1 bigger return 1 , smaller
// return -1 , equal return 0
{
// we can decide which number is bigger based on number of digits in each
// already zeros at the beginning were deleted
if (Size(FirstNum) > Size(SecondNum))
return 1;
else if (Size(FirstNum) < Size(SecondNum))
return -1;
// if numbers of digits are equal it iterates to compare digit by digit from
// the most significant digit
else {
Digit *num1 = FirstNum->Next->Next;
Digit *num2 = SecondNum->Next->Next;
while (num1 != NULL && num2 != NULL) {
if (num1->Data > num2->Data)
return 1;
else if (num1->Data < num2->Data)
return -1;
num1 = num1->Next;
num2 = num2->Next;
}
return 0;
}
}
/*
* Function: MultiplyNumbers
* -------------------------
* Multiplies two numbers represented by linked lists and stores the result in a pointer.
*
* Parameters:
* FirstNum: Pointer to the first number's linked list.
* SecondNum: Pointer to the second number's linked list.
* result: Pointer to store the result.
*/
void MultiplyNumbers(Digit *FirstNum, Digit *SecondNum, Digit **result) {
int i = 0;
Digit *PrevResult = NULL;
Digit *CurrResult = MakeEmpty(NULL);
InsertFirst(0, CurrResult);
InsertFirst(0, CurrResult);
Digit *num1;
Digit *num2 = FindLast(SecondNum);
while (num2 != SecondNum->Next) {
PrevResult = MakeEmpty(PrevResult);
InsertFirst(0, PrevResult);
num1 = FindLast(FirstNum);
int Cin = 0;
while (num1 != FirstNum->Next) {
int noderesult = num1->Data * num2->Data;
noderesult += Cin;
Cin = noderesult / 10;
noderesult %= 10;
InsertFirst(noderesult, PrevResult);
num1 = num1->Pre;
}
if (Cin != 0)
InsertFirst(Cin, PrevResult);
if (i > 0) // adding zeros to the last of the multiplication result
// (multiplying by 10 based on the number of the digit)
{
for (int z = 0; z < i; z++)
InsertLast(0, PrevResult);
}
CurrResult = AddNumbers(CurrResult, PrevResult);
i++;
num2 = num2->Pre;
}
*result = Duplicate(CurrResult);
DisposeList(PrevResult);
DisposeList(CurrResult);
}
/*
* Function: MultiplyTwoNumbers
* ----------------------------
* Takes two numbers from the user, performs multiplication, enqueues the result,
* and prints it along with the operation type.
*
* Parameters:
* FirstNum: Pointer to the first number's linked list.
* SecondNum: Pointer to the second number's linked list.
* Results: Pointer to the results queue.
*/
void MultiplyTwoNumbers(Digit *FirstNum, Digit *SecondNum,
QueueResults *Results) {
Digit *result;
MultiplyNumbers(FirstNum, SecondNum, &result);
if (FirstNum->Next->Data == SecondNum->Next->Data) {
result->Next->Data = 0;
} else {
result->Next->Data = 1;
}
RemoveZeros(result);
printf("\n");
printf("The Result = ");
PrintList(result);
printf("\n");
Enqueue(result, Results);
}
/*
* Function: Subtract
* ------------------
* Subtracts two numbers represented by linked lists (FirstNum - SecondNum) and returns the result.
*
* Parameters:
* FirstNum: Pointer to the first number's linked list.
* SecondNum: Pointer to the second number's linked list.
*
* Returns:
* Pointer to the linked list representing the subtraction result.
*/
Digit *Subtract(Digit *FirstNum, Digit *SecondNum) {
Digit *num1 = FindLast(FirstNum);
Digit *num2 = FindLast(SecondNum);
Digit *result = NULL;
result = MakeEmpty(result);
int Borrow = 0;
int res;
InsertFirst(0, result);
while (num1 != FirstNum->Next && num2 != SecondNum->Next) {
res = num1->Data - num2->Data - Borrow;
// if the result of the subtraction was negative then here we want a
// borrow from the next digit
if (res >= 0) {
InsertFirst(res, result);
Borrow = 0;
} else {
res += 10;
InsertFirst(res, result);
Borrow = 1;
}
num1 = num1->Pre;
num2 = num2->Pre;
}
// because the first number in bigger either they have the same number of
// digits or the first has more
while (num1 != FirstNum->Next && num2 == SecondNum->Next) {
res = num1->Data - Borrow;
if (res >= 0) {