-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrade_School_Calculation.h
1137 lines (1031 loc) · 41.5 KB
/
Grade_School_Calculation.h
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>
static int NumberArrayLength;
void toWholeNumber(int* (*num), char* n); //Change the input into int array
char* WholeNumberAddition(char* number1, char* number2); //Add two whole numbers
char* WholeDecimalAddition(char* number1, char* number2); //Add one decimal number, one whole number
char* DecimalNumberAddition(char* number1, char* number2); //Add two decimal numbers
char* WholeNumberSubtraction(char* number1, char* number2); //Subtract two whole numbers
char* DecimalNumberSubtraction(char* number1, char* number2); //Subtract two decimal numbers + one decimal number, one whole number
char* WholeNumberMultiplication(char* number1, char* number2);
int* AddOperation(int num1[], int num2[], int ArrayLength);
char* DecimalNumberMultiplication(char* number1, char* number2);
/*
* This function changes the input from char array to int array
* Notice that the output is void, and the input is int* (*num), we are passing the reference of the pointer
* By doing this can we only free the variable once
*/
void toWholeNumber(int* (*num), char* n)
{
*num = malloc(NumberArrayLength * sizeof(int));
int i;
int j = 0;
for (i = 0; i < NumberArrayLength-strlen(n); i++)
{
(*num)[i] = 0; //Again, num is the reference, *num is the int pointer
}
while (i < NumberArrayLength)
{
(*num)[i] = n[j] - 48;
i++;
j++;
}
}
/*
*This is the Grade School Addition Function
*Notice: all of the calculation function has no output; all of them are call by reference ie. char*(*finalresult)
*/
char* WholeNumberAddition(char* number1, char* number2)
{
//NumberArrayLength is determined by the length of the bigger input
if (strlen(number1) >= strlen(number2))
NumberArrayLength = strlen(number1);
else
NumberArrayLength = strlen(number2);
//Initialize 2 int arrays and set each of them to each number (input)
int* num1 = NULL;
int* num2 = NULL;
toWholeNumber(&num1, number1);
toWholeNumber(&num2, number2);
//Create some dynamic arrays
int* result = malloc((NumberArrayLength + 1) * sizeof(int)); //reason for NumberArrayLength + 1 is because 789+789=1578
char* finalresult = malloc(NumberArrayLength + 2); //reason NumberArrayLength + 2 is because of '\0' at the last
int i;
int carry = 0;
int digit = 0;
//Grad school addition part (the boring part)
for (i = NumberArrayLength - 1; i > 0; i--)
{
digit = num1[i] + num2[i];
result[i + 1] = digit % 10;
carry = digit / 10;
num1[i - 1] = num1[i - 1] + carry;
}
digit = num1[0] + num2[0];
result[1] = digit % 10;
carry = digit / 10;
if (carry == 1)
result[0] = 1;
else
result[0] = 0;
//Now the result array has contained the answer, we just need to transfer the answer to *finalresult
for (i = 0; i < NumberArrayLength + 1; i++) //be aware that this the length of result is NumberArrayLength + 1
{
finalresult[i] = result[i] + 48; //Since *finalresult is char*, we need to change it from int to char by adding 48
}
finalresult[i] = '\0'; //Don't forget to put '\0' at last otherwise it will pop up some weird symbols
//Sometimes there are useless 0s at the front, we need to clear them
//Simply keep the digits after the last digit of 0s
for (i = 0; i < NumberArrayLength + 1; i++)
{
if (finalresult[i] != '0')
{
finalresult = &finalresult[i]; //This is like string.substring(i) in java
break;
}
}
if (i == NumberArrayLength + 1) //If the result is just 0, we change the *finalresult to 0 as well
{
finalresult = "0";
}
//Finally, we free those dynamic arrays
free(num1);
free(num2);
free(result);
return finalresult;
}
/*
* Addition function for one whole number and one decimal number
* Because only one input has decimal number, this becomes really easy.
* We can simply calculate the whole number part by using WholeNumberAddition and then add the decimal places using strcat
*/
char* WholeDecimalAddition(char* number1, char* number2)
{
//First, we need to figure out which number has decimal number
char* WholeNum1 = NULL;
char* DecimalNum1 = NULL;
char* WholeNum2 = NULL;
char* DecimalNum2 = NULL;
char* Number1 = strdup(number1);
char* Number2 = strdup(number2);
WholeNum1 = strtok(Number1, ".");
DecimalNum1 = strtok(NULL, ".");
WholeNum2 = strtok(Number2, ".");
DecimalNum2 = strtok(NULL, ".");
char* WholeNumberSum = NULL; //This contains the whole number result
char* finalresult = NULL;
if (DecimalNum1 == NULL && DecimalNum2 != NULL) //If the first number is a whole number and the second one is a decimal number
{
WholeNumberSum = WholeNumberAddition(WholeNum1, WholeNum2);
//The length of *finalresult should the the length of WholeNumberSum plus the length of decimal places plus 1 for '.'
finalresult = malloc(strlen(WholeNumberSum)+strlen(DecimalNum2)+1);
//Combine everything to *finalresult
strcpy(finalresult, WholeNumberSum);
strcat(finalresult, ".");
strcat(finalresult, DecimalNum2);
}
else //If the second number is a whole number and the first one is a decimal number
{
//Same as above
WholeNumberSum = WholeNumberAddition(WholeNum1, WholeNum2);
finalresult = malloc(strlen(WholeNumberSum)+strlen(DecimalNum1)+1);
strcpy(finalresult, WholeNumberSum);
strcat(finalresult, ".");
strcat(finalresult, DecimalNum1);
}
//There may be useless 0s in the decimal places, we need to get rid of them
for (int i = strlen(finalresult) - 1; i >= 0; i--)
{
if (finalresult[i] != '0') //If the digit we find is non-zero, we know that everything after that are 0s and should be eliminated
{
finalresult[i + 1] = '\0'; //Simply putting '\0' behind the first non-zero digit will eliminate the zeros
if (finalresult[i] == '.') //If the first non-zero digit is the '.', it means the answer is a whole number
finalresult[i] = '\0'; //We can ignore '.' as well
break;
}
}
free(WholeNumberSum);
free(Number1);
free(Number2);
return finalresult;
}
/*
* Addition function for two decimal numbers, this can be a little bit tricky
* The idea is to get rid of the decimals and then treat them as two whole numbers
* After doing the WholeNumberAddition, we can plug back in the decimal
* It's very important to line up two numbers by their decimals, the idea is to add
* 0s to the number that has less amount of DECIMAL numbers to make them equal size
*/
char* DecimalNumberAddition(char* number1, char* number2)
{
//In this case, we know that both numbers have decimals, so we can just do it!
char* WholeNum1 = NULL;
char* DecimalNum1 = NULL;
char* WholeNum2 = NULL;
char* DecimalNum2 = NULL;
char* Number1 = strdup(number1);
char* Number2 = strdup(number2);
WholeNum1 = strtok(Number1, ".");
DecimalNum1 = strtok(NULL, ".");
WholeNum2 = strtok(Number2, ".");
DecimalNum2 = strtok(NULL, ".");
int i; //Counting variable i
//Those two variables store two decimal numbers in Whole Number form.
char* Decimal_Whole1 = NULL;
char* Decimal_Whole2 = NULL;
char* finalresult = NULL;
/*
* Here we have to split into cases since we don't know which number has more decimal numbers, or equal?
* The first case is the situation where the first number has more decimal digits.
*/
if (strlen(DecimalNum1) > strlen(DecimalNum2))
{
//Transferring decimal numbers into whole numbers, straight forward
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum1));
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum1)); //Be aware of the decimal digits! It's the length of the larger decimal digits!
strcpy(Decimal_Whole1, WholeNum1);
strcat(Decimal_Whole1, DecimalNum1);
strcpy(Decimal_Whole2, WholeNum2);
strcat(Decimal_Whole2, DecimalNum2);
//We know strlen(DecimalNum1) > strlen(DecimalNum2), simply adding 0s to the back of the second number
//to make them match in length
for (i = 0; i < strlen(DecimalNum1) - strlen(DecimalNum2); i++)
strcat(Decimal_Whole2, "0");
finalresult = WholeNumberAddition(Decimal_Whole1, Decimal_Whole2); //Now we do the addition calculation, putting the result into finalresult
/*
* This part is important!
* Now we need to add the decimal back, we an easily figure out that the decimal should locate at strlen(*finalresult) - strlen(DecimalNum1)
* Now we need to split *finalresult into two parts, just like substring in Java (too bad there's no such function in C!)
* The idea is to create a temp variable which has length of strlen(*finalresult) + 1 (1 for the decimal) and move (copy) all the digits in
* *finalresult into temp, including decimal
* (I tried to use realloc() but it didn't work for some weird reasons....)
*/
char* Temp = malloc(strlen(finalresult) + 2); //Declaring Temp variable
for (i = 0; i < strlen(finalresult) - strlen(DecimalNum1); i++)
Temp[i] = finalresult[i]; //This is the whole number part
Temp[i++] = '.'; //After that is the decimal
while (finalresult[i - 1] != '\0') //Be aware that the index here is i-1 since previous line Temp[i++]
{
Temp[i] = finalresult[i - 1]; //Copying decimal number part into Temp
i++;
}
Temp[i] = '\0'; //Don't forget '\0' at the very end
//If the answer is less than 1, it has to show "0."+something, right now the program will output "."+something.
//The way to check is to check whether the first char is '.'
if (Temp[0] == '.')
{
strcpy(finalresult, "0");
strcat(finalresult, Temp);
free(Temp);
}
else //Otherwise, just copy Temp variable to *finalresult and free Temp
{
strcpy(finalresult, Temp);
free(Temp);
}
}
//This case is the situation where the first number has less decimal digits.
else if (strlen(DecimalNum1) < strlen(DecimalNum2))
{
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum2));
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum2));
strcpy(Decimal_Whole1, WholeNum1);
strcat(Decimal_Whole1, DecimalNum1);
strcpy(Decimal_Whole2, WholeNum2);
strcat(Decimal_Whole2, DecimalNum2);
//Pay a little attention on which has more digits than which
for (i = 0; i < strlen(DecimalNum2) - strlen(DecimalNum1); i++)
strcat(Decimal_Whole1, "0");
finalresult = WholeNumberAddition(Decimal_Whole1, Decimal_Whole2);
//Basically the same thing
char* Temp = malloc(strlen(finalresult) + 2);
for (i = 0; i < strlen(finalresult) - strlen(DecimalNum2); i++)
Temp[i] = finalresult[i];
Temp[i++] = '.';
while (finalresult[i - 1] != '\0')
{
Temp[i] = finalresult[i - 1];
i++;
}
Temp[i] = '\0';
if (Temp[0] == '.')
{
strcpy(finalresult, "0");
strcat(finalresult, Temp);
free(Temp);
}
else {
strcpy(finalresult, Temp);
free(Temp);
}
}
//This case is the situation where both numbers have the same decimal digits.
else
{
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum1));
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum2));
strcpy(Decimal_Whole1, WholeNum1);
strcat(Decimal_Whole1, DecimalNum1);
strcpy(Decimal_Whole2, WholeNum2);
strcat(Decimal_Whole2, DecimalNum2);
finalresult = WholeNumberAddition(Decimal_Whole1, Decimal_Whole2);
char* Temp = malloc(strlen(finalresult) + 2);
for (i = 0; i < strlen(finalresult) - strlen(DecimalNum1); i++)
Temp[i] = finalresult[i];
Temp[i++] = '.';
while (finalresult[i - 1] != '\0')
{
Temp[i] = finalresult[i - 1];
i++;
}
Temp[i] = '\0';
if (Temp[0] == '.') {
strcpy(finalresult, "0");
strcat(finalresult, Temp);
free(Temp);
}
else {
strcpy(finalresult, Temp);
free(Temp);
}
}
//There may be useless 0s in the decimal places, we need to get rid of them
for (int i = strlen(finalresult) - 1; i >= 0; i--) {
if (finalresult[i] != '0')//If the digit we find is non-zero, we know that everything after that are 0s and should be eliminated
{
finalresult[i + 1] = '\0';//Simply putting '\0' behind the first non-zero digit will eliminate the zeros
if (finalresult[i] == '.')//If the first non-zero digit is the '.', it means the answer is a whole number
finalresult[i] = '\0'; //We can ignore '.' as well
break;
}
}
//Finally, don't forget to free those variables!
free(Number1);
free(Number2);
free(Decimal_Whole1);
free(Decimal_Whole2);
return finalresult;
}
/*
* This is the grade school subtraction function.
* Subtraction can be tricky because the result can be negative; the solution is to compare two inputs first, figure out which sign the result will have;
* then do the subtraction properly.
*/
char* WholeNumberSubtraction(char* number1, char* number2)
{
char* Big = NULL; //Stores big number
char* Small = NULL; //Opposite
char* finalresult = NULL;
int isNegative = 0;
int i = 0;
//First we need to eliminate 0s (if any) at the front of each number.(Sometimes users can be a pain in the ass....)
//Similiar method used in the addition function
for (i = 0; i < strlen(number1); i++)
{
if (number1[i] != '0')
{
number1 = &number1[i]; //This is like string.substring(i) in java
break;
}
}
for (i = 0; i < strlen(number2); i++)
{
if (number2[i] != '0')
{
number2 = &number2[i];
break;
}
}
/*
* This part checks if the result has a negative sign.
* The idea is to compare the length of two numbers. If one number is longer than the other then we know the longer number has to be bigger.
* If two number have the same length, then we have to compare each corresponding digit and figure out which number is bigger
*/
if (strlen(number1) > strlen(number2))
{
NumberArrayLength = strlen(number1); //Set the NumberArrayLength be the length of larger number
//Be aware that you need to dynamic allocate Big and Small first (if not, then the program will crash)
Big = strdup(number1);
Small = strdup(number2);
}
else if (strlen(number1) < strlen(number2))
{
NumberArrayLength = strlen(number2);
Big = strdup(number2);
Small = strdup(number1);
isNegative = 1;
}
else
{
NumberArrayLength = strlen(number1); //Since the length is the same, we can set NumberArrayLength ahead
for (i = 0; i < strlen(number1); i++) //Compare each digit
{
if (number1[i] > number2[i])
{
Big = strdup(number1);
Small = strdup(number2);
break;
}
else if (number1[i] < number2[i])
{
Big = strdup(number2);
Small = strdup(number1);
isNegative = 1;
break;
}
}
//There is a chance that two numbers are exactly the same, in that case we just set *finalresult to "0"
if (i == strlen(number1))
{
finalresult = "0";
free(Big);
free(Small);
return finalresult;
}
}
int* num1 = NULL;
int* num2 = NULL;
toWholeNumber(&num1, Big);
toWholeNumber(&num2, Small);
int* result = malloc(NumberArrayLength * sizeof(int)); //The length of result has to be at most NumberArrayLength
finalresult = malloc(NumberArrayLength + 1);
//Boring Calculation steps
int digit = 0;
for (i = NumberArrayLength - 1; i > 0; i--)
{
if (num1[i] - num2[i] >= 0)
{
digit = num1[i] - num2[i];
result[i] = digit;
}
else
{
digit = num1[i] + 10 - num2[i];
result[i] = digit;
num1[i - 1] -= 1;
}
}
digit = num1[0] - num2[0];
result[0] = digit;
//Now the result array has contained the answer, we just need to transfer the answer to *finalresult
for (i = 0; i < NumberArrayLength; i++)
finalresult[i] = result[i] + 48; //Since *finalresult is char*, we need to change it from int to char by adding 48
finalresult[i] = '\0'; //Be aware
//Sometimes there are useless 0s at the front, we need to clear them
//Simply keep the digits after the last digit of 0s
for (i = 0; i < strlen(finalresult); i++)
{
if (finalresult[i] != '0')
{
finalresult = &finalresult[i];
if (isNegative) //if the result is negative, we have to manually add "-" sign
{
char* Temp = strdup(finalresult); //We duplicate *finalrsult to Temp
strcpy(finalresult, "-");
strcat(finalresult, Temp);
free(Temp);
}
break;
}
}
//Finally, we free those dynamic arrays
free(Big);
free(Small);
free(num1);
free(num2);
free(result);
return finalresult;
}
/*
* This function combines DecimalWholeSubtraction and DecimalNumberSubtraction.
* In addition, we separate them apart because there is a trick to do DecimalWholeAddition. In subtraction however, the trick remains, but only works in one case (1.05-1), so it's
* not worth to make an separated function. The idea is basically the same as DecimalNumberAddition, we eliminate the decimal and treat them as two whole numbers subtracting.
*/
char* DecimalNumberSubtraction(char* number1, char* number2)
{
//First, we assume that both numbers have decimal numbers
char* WholeNum1 = NULL;
char* DecimalNum1 = NULL;
char* WholeNum2 = NULL;
char* DecimalNum2 = NULL;
char* Number1 = strdup(number1);
char* Number2 = strdup(number2);
WholeNum1 = strtok(Number1, ".");
DecimalNum1 = strtok(NULL, ".");
WholeNum2 = strtok(Number2, ".");
DecimalNum2 = strtok(NULL, ".");
int i;
char* Decimal_Whole1 = NULL;
char* Decimal_Whole2 = NULL;
char* TempResult = NULL; //Contains some temporary string, mostly for *finalresult
char* Temp1 = NULL; //Contains other miscellaneous strings
char* finalresult = NULL;
/*
* There are three cases but we treat them as two cases ONLY. First case is the first number is a whole number and the second one is a decimal number, OR both of them are
* decimal numbers but the length of decimal digits of the first number is LESS than the length of decimal digits of the second number;
* second case is the first number is a decimal number and the second one is a whole number, OR both of them are decimal numbers but the length of decimal digits
* of the first number is MORE than the length of decimal digits of the second number;
* The third case is both numbers are decimal numbers and they have the same length of decimal digits, but this case can be combined into any of the cases above.
*/
if (DecimalNum1 == NULL || (DecimalNum2 != NULL && (strlen(DecimalNum1) < strlen(DecimalNum2)))) //If the first number is a whole number and the second one is a decimal number
{
if (DecimalNum1 != NULL) //If the first number has decimal
{
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum2)); //Be aware that the length of decimals should be equal to the larger decimal digits
strcpy(Decimal_Whole1, WholeNum1);
strcat(Decimal_Whole1, DecimalNum1);
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum2));
strcpy(Decimal_Whole2, WholeNum2);
strcat(Decimal_Whole2, DecimalNum2);
for (i = 0; i < strlen(DecimalNum2) - strlen(DecimalNum1); i++) //Add 0s to the back of the first number
strcat(Decimal_Whole1, "0");
}
else //If the first number is a whole number
{
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum2));
strcpy(Decimal_Whole2, WholeNum2);
strcat(Decimal_Whole2, DecimalNum2);
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum2));
strcpy(Decimal_Whole1, WholeNum1);
for (i = 0; i < strlen(DecimalNum2); i++)
strcat(Decimal_Whole1, "0");
}
//Now do the Subtraction
finalresult = WholeNumberSubtraction(Decimal_Whole1, Decimal_Whole2);
/*
* This is important! Now we need to add decimal to the correct position. There are several cases.
* Case 1: If the length of *finalresult is less than the length of DecimalNum2 (ex. 1-0.9996=0.0004, but right now *finalresult shows 4)
* Case 2: If the length of *finalresult is equal to the length of DecimalNum2 (ex. 1-0.5346=0.4654, but right now *finalresult shows 4654)
* Case 3: See Case 3
* Case 4: General Case (ex. 456-123.05=332.95)
*/
if (strlen(finalresult) < strlen(DecimalNum2)) //Case 1
{
if (finalresult[0] == '-') //If *finalresult is a negative number, we have to remove the "-" sign first and then do the operation
{
finalresult = &finalresult[1];
TempResult = malloc(strlen(finalresult) + strlen(DecimalNum2));
strcpy(TempResult, "");
for (i = 0; i < strlen(DecimalNum2); i++) //The idea is we add 0s to the front of *finalresult, it's okay if we add more 0s than we need
strcat(TempResult, "0");
strcat(TempResult, finalresult); //Now TempResult will look like 000000+*finalresult
//Now we add the decimal, this method is from DecimalNumberAddition
Temp1 = malloc(strlen(TempResult) + 2);
for (i = 0; i < strlen(TempResult) - strlen(DecimalNum2); i++)
Temp1[i] = TempResult[i];
Temp1[i++] = '.';
while (TempResult[i - 1] != '\0')
{
Temp1[i] = TempResult[i - 1];
i++;
}
Temp1[i] = '\0';
//Since the answer is negative, we have to put "-" sign back
strcpy(finalresult, "-");
strcat(finalresult, Temp1);
}
else //If *finalresult is positive, then we do the same thing except removing "-" sign
{
TempResult = malloc(strlen(finalresult) + strlen(DecimalNum2));
strcpy(TempResult, "");
for (i = 0; i < strlen(DecimalNum2); i++)
strcat(TempResult, "0");
strcat(TempResult, finalresult);
Temp1 = malloc(strlen(TempResult) + 2);
for (i = 0; i < strlen(TempResult) - strlen(DecimalNum2); i++)
Temp1[i] = TempResult[i];
Temp1[i++] = '.';
while (TempResult[i - 1] != '\0')
{
Temp1[i] = TempResult[i - 1];
i++;
}
Temp1[i] = '\0';
strcpy(finalresult, Temp1);
}
}
//Case 2
else if (strlen(finalresult) == strlen(DecimalNum2))
{
if (finalresult[0] == '-') //Ex. 1-1.0534=-0.0534, but *finalresult is -534, which length is equal to the length of DecimalNum2
{
finalresult = &finalresult[1];
//basically do *fianlresult = "-0.0" + *finalresult in Java
TempResult = strdup(finalresult);
strcpy(finalresult, "-0.0");
strcat(finalresult, TempResult);
}
else
{
TempResult = strdup(finalresult);
strcpy(finalresult, "0.");
strcat(finalresult, TempResult);
}
}
//Case 3: Ex. 1-1.978=-0.978, but *finalresult is -978
else if (strlen(finalresult) == (strlen(DecimalNum2) + 1) && finalresult[0] == '-')
{
finalresult = &finalresult[1];
TempResult = strdup(finalresult);
strcpy(finalresult, "-0.");
strcat(finalresult, TempResult);
}
//Case 4: General Case, we use the method in DecimalNumberAddition
else
{
Temp1 = malloc(strlen(finalresult) + 2);
for (i = 0; i < strlen(finalresult) - strlen(DecimalNum2); i++)
Temp1[i] = finalresult[i];
Temp1[i++] = '.';
while (finalresult[i - 1] != '\0')
{
Temp1[i] = finalresult[i - 1];
i++;
}
Temp1[i] = '\0';
strcpy(finalresult, Temp1);
}
}
//If the second number is a whole number and the first one is a decimal number, plus equal decimal length subtraction
//The idea is the same as above.
else if (DecimalNum2 == NULL || (DecimalNum1 != NULL && (strlen(DecimalNum1) >= strlen(DecimalNum2))))
{
if (DecimalNum2 != NULL)
{
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum1));
strcpy(Decimal_Whole1, WholeNum1);
strcat(Decimal_Whole1, DecimalNum1);
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum1));
strcpy(Decimal_Whole2, WholeNum2);
strcat(Decimal_Whole2, DecimalNum2);
for (i = 0; i < strlen(DecimalNum1) - strlen(DecimalNum2); i++)
strcat(Decimal_Whole2, "0");
}
else
{
Decimal_Whole1 = malloc(strlen(WholeNum1) + strlen(DecimalNum1));
strcpy(Decimal_Whole1, WholeNum1);
strcat(Decimal_Whole1, DecimalNum1);
Decimal_Whole2 = malloc(strlen(WholeNum2) + strlen(DecimalNum1));
strcpy(Decimal_Whole2, WholeNum2);
for (i = 0; i < strlen(DecimalNum1); i++)
strcat(Decimal_Whole2, "0");
}
finalresult = WholeNumberSubtraction(Decimal_Whole1, Decimal_Whole2);
if(strcmp(finalresult, "0") == 0)
return "0";
if(strlen(finalresult) < strlen(DecimalNum1))
{
if(finalresult[0] == '-')
{
finalresult = &finalresult[1];
TempResult = malloc(strlen(finalresult) + strlen(DecimalNum1));
strcpy(TempResult, "");
for(i=0; i<strlen(DecimalNum1); i++)
strcat(TempResult, "0");
strcat(TempResult, finalresult);
Temp1 = malloc(strlen(TempResult) + 2);
for (i = 0; i < strlen(TempResult) - strlen(DecimalNum1); i++)
Temp1[i] = TempResult[i];
Temp1[i++] = '.';
while (TempResult[i - 1] != '\0')
{
Temp1[i] = TempResult[i - 1];
i++;
}
Temp1[i] = '\0';
strcpy(finalresult, "-");
strcat(finalresult, Temp1);
}
else
{
TempResult = malloc(strlen(finalresult) + strlen(DecimalNum1));
strcpy(TempResult, "");
for (i = 0; i < strlen(DecimalNum1); i++)
strcat(TempResult, "0");
strcat(TempResult, finalresult);
Temp1 = malloc(strlen(TempResult) + 2);
for (i = 0; i < strlen(TempResult) - strlen(DecimalNum1); i++)
Temp1[i] = TempResult[i];
Temp1[i++] = '.';
while (TempResult[i - 1] != '\0')
{
Temp1[i] = TempResult[i - 1];
i++;
}
Temp1[i] = '\0';
strcpy(finalresult, Temp1);
}
}
else if(strlen(finalresult) == strlen(DecimalNum1))
{
if(finalresult[0] == '-')
{
finalresult = &finalresult[1];
TempResult = strdup(finalresult);
strcpy(finalresult, "-0.0");
strcat(finalresult, TempResult);
}
else
{
TempResult = strdup(finalresult);
strcpy(finalresult, "0.");
strcat(finalresult, TempResult);
}
}
else if(strlen(finalresult) == (strlen(DecimalNum1)+1) && finalresult[0] == '-')
{
finalresult = &finalresult[1];
TempResult = strdup(finalresult);
strcpy(finalresult, "-0.");
strcat(finalresult, TempResult);
}
else
{
Temp1 = malloc(strlen(finalresult) + 2);
for (i = 0; i < strlen(finalresult) - strlen(DecimalNum1); i++)
Temp1[i] = finalresult[i];
Temp1[i++] = '.';
while (finalresult[i - 1] != '\0')
{
Temp1[i] = finalresult[i - 1];
i++;
}
Temp1[i] = '\0';
strcpy(finalresult, Temp1);
}
}
else //I don't know...Is there other cases?
{
printf("WTF?\n");
}
//Now we need to check the useless 0s in front, methods are the same
if(finalresult[0] == '-')
{
finalresult = &finalresult[1];
for(i=0; i<strlen(finalresult); i++)
{
if(finalresult[i] != '0')
{
finalresult = &finalresult[i];
if(finalresult[0] == '.')
{
Temp1 = strdup(finalresult);
strcpy(finalresult, "0");
strcat(finalresult, Temp1);
}
break;
}
}
Temp1 = strdup(finalresult);
strcpy(finalresult, "-");
strcat(finalresult, Temp1);
}
else
{
for (i = 0; i < strlen(finalresult); i++)
{
if (finalresult[i] != '0')
{
finalresult = &finalresult[i];
if (finalresult[0] == '.')
{
Temp1 = strdup(finalresult);
strcpy(finalresult, "0");
strcat(finalresult, Temp1);
}
break;
}
}
}
//Now the back
for(i=strlen(finalresult)-1; i>=0; i--)
{
if(finalresult[i] != '0')
{
finalresult[i+1] = '\0';
if(finalresult[i] == '.')
finalresult[i] = '\0';
break;
}
}
//Free dynamic variables
free(Number1);
free(Number2);
free(Decimal_Whole1);
free(Decimal_Whole2);
free(TempResult);
free(Temp1);
return finalresult;
}
/*
*This is the grade school Multiplication method
* This method is more complex than addition and subtraction, but we don't have to deal with sign.
* The idea is to identify which number is bigger first; then we create an 2 dimensional array; we add up each result
*/
char* WholeNumberMultiplication(char* number1, char* number2)
{
int NumberofColumn = 0; //This is the number of columns the 2 dimensional array should have
//It's determined by the length of the smaller number
//Create two variables for comparing (you don't have to)
char* Number1 = NULL; //Contains bigger number
char* Number2 = NULL; //Contains smaller number
int i, j; //counting variables
//First we need to check if two numbers have useless 0s at the front (again, people are mean...)
//We don't have to in addition and subtraction but we do need this in multiplication and division; the algorithm depends on the true length of two inputs
for(i=0; i<strlen(number1); i++)
{
if(number1[i] != '0')
{
number1 = &number1[i];
break;
}
}
for(i=0; i<strlen(number2); i++)
{
if(number2[i] != '0')
{
number2 = &number2[i];
break;
}
}
//Now we identify the bigger number by comparing the length of two numbers
if(strlen(number1) >= strlen(number2))
{
//This is the reason why we do this, the NumberArrayLength is the length of bigger number and the column is the length of smaller number
NumberArrayLength = strlen(number1);
NumberofColumn = strlen(number2);
Number1 = number1;
Number2 = number2;
}
else
{
NumberArrayLength = strlen(number2);
NumberofColumn = strlen(number1);
Number2 = number1;
Number1 = number2;
}
int* num1 = NULL;
int* num2 = NULL;
toWholeNumber(&num1, Number1);
toWholeNumber(&num2, Number2);
int MaxLength = strlen(Number1) + strlen(Number2); //Th width of the 2 dimensional array
int **ProductResult = (int **) malloc(NumberofColumn * sizeof(int *)); //This is the 2 dimensional array
for(i=0; i<NumberofColumn; i++) //Initialize elements to 0
{
ProductResult[i] = (int*)malloc(MaxLength * sizeof(int));
memset(ProductResult[i], 0, MaxLength*sizeof(int));
}
//Multiplication calculation I'm sure you don't have interest on that
int digit = 0;
int carry = 0;
int temp = 0;
int power_of_ten = 0;
int k = 0, l = 1;
int* WholeSum = malloc(MaxLength* sizeof(int));
memset(WholeSum, 0, MaxLength* sizeof(int));
for(i=NumberArrayLength-1; i>=NumberArrayLength-NumberofColumn; i--)
{
for(j=NumberArrayLength-1; j>=0; j--)
{
digit = num2[i] * num1[j];
if(ProductResult[k][MaxLength-power_of_ten-l] + digit >= 10)
{
temp = ProductResult[k][MaxLength-power_of_ten-l];
ProductResult[k][MaxLength-power_of_ten-l] = (temp + digit) % 10;
carry = (temp + digit) / 10;
l++;
ProductResult[k][MaxLength-power_of_ten-l] = carry;
}
else
{
ProductResult[k][MaxLength-power_of_ten-l] += digit;
l++;
}
}
WholeSum = AddOperation(ProductResult[k], WholeSum, MaxLength);
l = 1;
k++;
power_of_ten++;
}
char* finalresult= malloc(MaxLength+1); //now we create finalresult to contain the answer
for(i=0; i<MaxLength; i++)
finalresult[i] = WholeSum[i]+48; //Be aware of this
finalresult[i] = '\0'; //And this
for(i=0; i<strlen(finalresult); i++) //Now we check if there is any useless 0s in the front
{
if(finalresult[i] != '0')
{
finalresult = &finalresult[i];
break;
}
}
if(i == strlen(finalresult)) //If it's just 0, print 0
finalresult = "0";
//finally we free all the pointers
for(i=0; i<NumberofColumn; i++)
free(ProductResult[i]);
free(ProductResult);
free(num1);
free(num2);
free(WholeSum);
return finalresult;
}
/*
* This is a simple addition method for multiplication only.
*/
int* AddOperation(int* num1, int* num2, int ArrayLength)
{
int* finalresult = malloc(ArrayLength * sizeof(int));
int* temp = malloc(ArrayLength * sizeof(int));
int i;
for(i=0; i<ArrayLength; i++)
{
temp[i] = num1[i];
}
int carry = 0;
int digit =0;
for(i=ArrayLength-1; i>0; i--)
{
digit = temp[i] + num2[i];
finalresult[i] = digit % 10;
carry = digit / 10;
temp[i-1] = temp[i-1] + carry;
}
digit = temp[0] + num2[0];
finalresult[0] = digit;
free(temp);
return finalresult;
}
/*
* Same as subtraction, this function combines DecimalWholeMultiplication and DecimalNumberMultiplication.
* Similar idea: We remove the decimal first and do the WholeNumberMultiplication, then we put the decimal back
*/
char* DecimalNumberMultiplication(char* number1, char* number2)
{
//First, we assume that both numbers have decimal numbers