-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
128 lines (114 loc) · 3.19 KB
/
main.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
/* SAUVAGE Tao
27/10/11
Main:
Main menu useable by the user
*/
#include <main.h>
int main(void)
{
int choice = 0, lastOpe = 0;
unsigned long fact = 0;
char *str = NULL;
BigInteger biOne = NULL, biTwo = NULL, res = NULL, temp = NULL;
do
{
choice = display_menu();
switch(choice)
{
case 1:
clean_stdin();
str = new_str_of_big_int(1);
biOne = newBigInteger(str);
free(str);
str = NULL;
break;
case 2:
clean_stdin();
str = new_str_of_big_int(2);
biTwo = newBigInteger(str);
free(str);
str = NULL;
break;
case 3:
temp = fromBigIntegerToBigInteger(biOne);
biOne = fromBigIntegerToBigInteger(biTwo);
biTwo = fromBigIntegerToBigInteger(temp);
break;
case 4:
lastOpe = '+';
res = sumBigInt(biOne, biTwo);
break;
case 5:
lastOpe = '-';
res = diffBigInt(biOne, biTwo);
break;
case 6:
lastOpe = '*';
res = mulBigInt(biOne, biTwo);
break;
case 7:
printf("Enter the number :\t");
scanf("%lu", &fact);
res = factorial(fact);
printf("(%ld)! = ", fact);
printBigInteger(res);
break;
case 8:
printf("BigInteger 1 :\t");
printBigInteger(biOne);
printf("BigInteger 2:\t");
printBigInteger(biTwo);
if(lastOpe != 0)
{
printf("\n(1) %c (2) = ", lastOpe);
printBigInteger(res);
}
break;
}
printf("\n");
}while (choice != 9);
delete_big_int(biOne);
delete_big_int(biTwo);
delete_big_int(res);
delete_big_int(temp);
return 0;
}
int display_menu()
{
int choice = 0;
printf("Menu: \n");
printf("1. Define the first big integer\n");
printf("2. Define the second big integer\n");
printf("3. Swap the two big integers\n");
printf("\n4. Sum big integers\n");
printf("5. Diff big integers\n");
printf("6. Mul big integers\n");
printf("\n7. Factorial\n");
printf("\n8. Print the big integers\n");
printf("\n9. Quit\n");
printf("\nChoice : ");
scanf("%d", &choice);
printf("\n");
return choice;
}
char* new_str_of_big_int(int index)
{
char* wholeNumber;
char nb;
int i;
wholeNumber=(char*) calloc(1, sizeof(char));
printf("\nWrite the number %d : ", index);
i=0;
while(scanf("%c",&nb)==1 && nb!=10 && nb!=13)
{
/* If nb is a digit or if its first input is '+' or '-' */
if(isdigit(nb) || (i==0 && (nb=='+' || nb=='-')))
{
wholeNumber[i]=nb;
i++;
wholeNumber=(char*) realloc(wholeNumber,(i+1)*sizeof(char));
}
}
wholeNumber[i]='\0';
return wholeNumber;
}