This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
271 lines (258 loc) · 8.62 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "Headers/function.h"
#include "Headers/UnitConvert.h"
#include "Headers/Contact.h"
#include "Headers/calendar.h"
#include <mysql.h>
void GOTOXY(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main(int argc, char *argv[]) {
system("color B");
printf("Choose your script:\n");
printf("1. Hours_converter\n");
printf("2. Integer_to_string\n");
printf("3. BMI_calculator\n");
printf("4. UnitConvert\n");
printf("5. Contact manager - CSV usage\n");
printf("6. DB manager\n");
printf("7. Calendar - not by me\n");
printf("8. GOTOXY test\n");
printf("0. Exit\n");
int script;
scanf("%d", &script);
if (script == 1) {
int hours = 0, minutes = 564;
// Send variables addresses to functions
getHours(&hours, &minutes);
printf("%d hours et %d minutes\n", hours, minutes);
}
else if (script == 2) {
int number = 123;
char *string = NULL;
// Return the address of string to the function convertIntToString and store the result in string
convertIntToString(&number, &string);
printf("%s\n", string);
free(string);
}
else if (script == 3) {
int height, weight;
float BMI;
printf("Enter your height in cm: ");
scanf("%d", &height);
printf("Enter your weight in kg: ");
scanf("%d", &weight);
BMI_calculator(&height, &weight, &BMI);
printf("Your BMI is %.2f\n", BMI);
FILE *file = fopen("../extern/BMI.csv", "a");
if (file == NULL) {
printf("File didn't exist\n");
} else {
fprintf(file, "%d;%d;%.2f\n", height, weight, BMI);
fclose(file);
}
}
else if (script == 4) {
printf("Choose your category of convertion:\n");
printf("1. Temperature\n");
printf("2. Length\n");
printf("3. Weight\n");
printf("4. Exit\n");
int category;
scanf("%d", &category);
if (category == 1) {
printf("Choose your convertion:\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("3. Exit\n");
int convertion;
scanf("%d", &convertion);
if (convertion == 1) {
float Celsius = 0, Fahrenheit = 0;
printf("Enter the temperature in Celsius: ");
scanf("%f", &Celsius);
CelsiusToFahrenheit(&Celsius, &Fahrenheit);
printf("The temperature in Fahrenheit is %.2f\n", Fahrenheit);
} else if (convertion == 2) {
float Fahrenheit = 0, Celsius = 0;
printf("Enter the temperature in Fahrenheit: ");
scanf("%f", &Fahrenheit);
FahrenheitToCelsius(&Fahrenheit, &Celsius);
printf("The temperature in Celsius is %.2f\n", Celsius);
} else if (convertion == 3) {
printf("Exit\n");
} else {
printf("Wrong input\n");
}
}
else if (category == 2) {
printf("Choose your convertion:\n");
printf("1. Meters to kilometers\n");
printf("2. Kilometers to meters\n");
printf("3. Exit\n");
int convertion;
scanf("%d", &convertion);
if (convertion == 1) {
float meters = 0, kilometers = 0;
printf("Enter the length in meters: ");
scanf("%f", &meters);
metersToKilometers(&meters, &kilometers);
printf("The length in kilometers is %.2f\n", kilometers);
} else if (convertion == 2) {
float kilometers = 0, meters = 0;
printf("Enter the length in kilometers: ");
scanf("%f", &kilometers);
kilometersToMeters(&kilometers, &meters);
printf("The length in meters is %.2f\n", meters);
} else if (convertion == 3) {
printf("Exit\n");
} else {
printf("Wrong input\n");
}
}
else if (category == 3) {
printf("Choose your convertion:\n");
printf("1. Kilograms to pounds\n");
printf("2. Pounds to kilograms\n");
printf("3. Exit\n");
int convertion;
scanf("%d", &convertion);
if (convertion == 1) {
float kilograms = 0, pounds = 0;
printf("Enter the weight in kilograms: ");
scanf("%f", &kilograms);
kilogramsToPounds(&kilograms, £s);
printf("The weight in pounds is %.2f\n", pounds);
} else if (convertion == 2) {
float pounds = 0, kilograms = 0;
printf("Enter the weight in pounds: ");
scanf("%f", £s);
poundsToKilograms(£s, &kilograms);
printf("The weight in kilograms is %.2f\n", kilograms);
} else if (convertion == 3) {
printf("Exit\n");
} else {
printf("Wrong input\n");
}
}
else if (category == 4) {
printf("Exit\n");
}
else {
printf("Wrong input\n");
}
}
else if (script == 5) {
printf("Enter your password: ");
char password[100];
scanf("%s", password);
if (strcmp(password, "123") == 0) {
system("cls");
printf("Welcome\n");
printf("1. Add contact\n");
printf("2. Show contacts\n");
printf("3. Exit\n");
int category;
scanf("%d", &category);
if (category == 1) {
addContact();
system("cls");
} else if (category == 2) {
showContacts();
system("cls");
} else if (category == 3) {
printf("Exit\n");
} else {
printf("Wrong input\n");
}
} else {
printf("Wrong password\n");
}
}
else if (script == 6) {
system("cls");
char *host = (char *) malloc(17);
char *user = (char *) malloc(100);
char *password = (char *) malloc(100);
char *database = (char *) malloc(100);
printf("Enter your database connection parameters.\n");
printf("Host: ");
scanf("%s", host);
printf("User: ");
scanf("%s", user);
printf("Password: ");
scanf("%s", password);
printf("Database: ");
scanf("%s", database);
// connect to database
int port = 3306;
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error init\n");
system("pause");
exit(1);
}
if (mysql_real_connect(conn, host, user, password, database, port, NULL, 0) == NULL) {
printf("Error connect\n");
system("pause");
exit(1);
}
printf("Connected\n");
//end of connection to database
//char query[1000] = "select * from users";
char query[1000];
printf("Enter your query: ");
fflush(stdin);
fgets(query, 1000, stdin);
printf("%s\n", query);
if (mysql_query(conn, query)) {
printf("Error query\n");
system("pause");
exit(1);
}
printf("Query executed\n");
//end of query
//print result of query only if it is select
if (strncmp(query, "select", 6) == 0) {
MYSQL_RES *res;
MYSQL_ROW row;
res = mysql_store_result(conn);
int num_fields = mysql_num_fields(res);
int num_rows = mysql_num_rows(res);
printf("Number of rows: %d\n", num_rows);
printf("Number of fields: %d\n", num_fields);
while ((row = mysql_fetch_row(res))) {
for (int i = 0; i < num_fields; i++) {
printf("%s\t", row[i]);
}
printf("\n");
}
mysql_free_result(res);
}
//end of result
mysql_close(conn);
}
else if (script == 7) {
calend();
}
else if (script == 8) {
system("cls");
GOTOXY(20, 5);
printf("test\n\n");
}
else if (script == 0) {
printf("Exit\n");
}
else {
printf("Wrong input\n");
}
system("pause");
return 0;
}