-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1.cpp
executable file
·378 lines (320 loc) · 12 KB
/
main1.cpp
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
/***************************************************************************/
/* */
/* 234218 Data DSs 1, Winter 2016-2017 */
/* Homework : Wet 1 */
/* */
/***************************************************************************/
/***************************************************************************/
/* */
/* File Name : main1.cpp */
/* */
/* Holds the "int main()" function and the parser of the shell's */
/* command line. */
/***************************************************************************/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "library1.h"
#include <iostream>
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif
/* The command's strings */
typedef enum {
NONE_CMD = -2,
COMMENT_CMD = -1,
INIT_CMD = 0,
ADDSTUDENT_CMD = 1,
ADDTEAM_CMD = 2,
MOVESTUDENTTOTEAM_CMD = 3,
GETMOSTPOWERFUL_CMD = 4,
REMOVESTUDENT_CMD = 5,
GETALLSTUDENTS_CMD = 6,
INCREASELEVEL_CMD = 7,
QUIT_CMD = 8
} commandType;
static const int numActions = 9;
static const char *commandStr[] = {
"Init",
"AddStudent",
"AddTeam",
"MoveStudentToTeam",
"GetMostPowerful",
"RemoveStudent",
"GetAllStudentsByPower",
"IncreaseLevel",
"Quit" };
static const char* ReturnValToStr(int val) {
switch (val) {
case SUCCESS:
return "SUCCESS";
case ALLOCATION_ERROR:
return "ALLOCATION_ERROR";
case FAILURE:
return "FAILURE";
case INVALID_INPUT:
return "INVALID_INPUT";
default:
return "";
}
}
/* we assume maximum string size is not longer than 256 */
#define MAX_STRING_INPUT_SIZE (255)
#define MAX_BUFFER_SIZE (255)
#define StrCmp(Src1,Src2) ( strncmp((Src1),(Src2),strlen(Src1)) == 0 )
typedef enum {
error_free, error
} errorType;
static errorType parser(const char* const command);
#define ValidateRead(read_parameters,required_parameters,ErrorString) \
if ( (read_parameters)!=(required_parameters) ) { printf(ErrorString); return error; }
static bool isInit = false;
/***************************************************************************/
/* main */
/***************************************************************************/
int main(int argc, const char**argv) {
char buffer[MAX_STRING_INPUT_SIZE];
// Reading commands
while (fgets(buffer, MAX_STRING_INPUT_SIZE, stdin) != NULL) {
fflush(stdout);
if (parser(buffer) == error)
break;
};
return 0;
}
/***************************************************************************/
/* Command Checker */
/***************************************************************************/
static commandType CheckCommand(const char* const command,
const char** const command_arg) {
if (command == NULL || strlen(command) == 0 || StrCmp("\n", command))
return (NONE_CMD);
if (StrCmp("#", command)) {
if (strlen(command) > 1)
printf("%s", command);
return (COMMENT_CMD);
};
for (int index = 0; index < numActions; index++) {
if (StrCmp(commandStr[index], command)) {
*command_arg = command + strlen(commandStr[index]) + 1;
return ((commandType) index);
};
};
return (NONE_CMD);
}
/***************************************************************************/
/* Commands Functions */
/***************************************************************************/
static errorType OnInit(void** DS, const char* const command);
static errorType OnAddStudent(void* DS, const char* const command);
static errorType OnAddTeam(void* DS, const char* const command);
static errorType OnMoveStudentToTeam(void* DS, const char* const command);
static errorType OnRemoveStudent(void* DS, const char* const command);
static errorType OnIncreaseLevel(void* DS, const char* const command);
static errorType OnGetMostPowerful(void* DS, const char* const command);
static errorType OnGetAllStudentsByPower(void* DS, const char* const command);
static errorType OnQuit(void** DS, const char* const command);
/***************************************************************************/
/* Parser */
/***************************************************************************/
static errorType parser(const char* const command) {
static void *DS = NULL; /* The general data structure */
const char* command_args = NULL;
errorType rtn_val = error;
commandType command_val = CheckCommand(command, &command_args);
switch (command_val) {
case (INIT_CMD):
rtn_val = OnInit(&DS, command_args);
break;
case (ADDSTUDENT_CMD):
rtn_val = OnAddStudent(DS, command_args);
break;
case (ADDTEAM_CMD):
rtn_val = OnAddTeam(DS, command_args);
break;
case (MOVESTUDENTTOTEAM_CMD):
rtn_val = OnMoveStudentToTeam(DS, command_args);
break;
case (REMOVESTUDENT_CMD):
rtn_val = OnRemoveStudent(DS, command_args);
break;
case (INCREASELEVEL_CMD):
rtn_val = OnIncreaseLevel(DS, command_args);
break;
case (GETMOSTPOWERFUL_CMD):
rtn_val = OnGetMostPowerful(DS, command_args);
break;
case (GETALLSTUDENTS_CMD):
rtn_val = OnGetAllStudentsByPower(DS, command_args);
break;
case (QUIT_CMD):
rtn_val = OnQuit(&DS, command_args);
break;
case (COMMENT_CMD):
rtn_val = error_free;
break;
case (NONE_CMD):
rtn_val = error;
break;
default:
assert(false);
break;
};
return (rtn_val);
}
/***************************************************************************/
/* OnInit */
/***************************************************************************/
static errorType OnInit(void** DS, const char* const command) {
if (isInit) {
printf("Init was already called.\n");
return (error_free);
};
isInit = true;
*DS = Init();
if (*DS == NULL) {
printf("Init failed.\n");
return error;
};
printf("Init done.\n");
return error_free;
}
/***************************************************************************/
/* OnAddStudent */
/***************************************************************************/
static errorType OnAddStudent(void* DS, const char* const command) {
int studentID;
int Grade;
int power;
ValidateRead(
sscanf(command, "%d %d %d", &studentID, &Grade, &power),
3, "AddStudent failed.\n");
StatusType res = AddStudent(DS, studentID, Grade, power);
if (res != SUCCESS) {
printf("AddStudent: %s\n", ReturnValToStr(res));
return error_free;
}
printf("AddStudent: %s\n", ReturnValToStr(res));
return error_free;
}
/***************************************************************************/
/* OnAddTeam */
/***************************************************************************/
static errorType OnAddTeam(void* DS, const char* const command) {
int magiID;
ValidateRead(sscanf(command, "%d", &magiID), 1, "AddTeam failed.\n");
StatusType res = AddTeam(DS, magiID);
if (res != SUCCESS) {
printf("AddTeam: %s\n", ReturnValToStr(res));
return error_free;
} else {
printf("AddTeam: %s\n", ReturnValToStr(res));
}
return error_free;
}
/***************************************************************************/
/* OnAddStudentToTeam */
/***************************************************************************/
static errorType OnMoveStudentToTeam(void* DS, const char* const command) {
int studentID, teamID;
ValidateRead(sscanf(command, "%d %d", &studentID, &teamID), 2,
"MoveStudentToTeam failed.\n");
StatusType res = MoveStudentToTeam(DS, studentID, teamID);
if (res != SUCCESS) {
printf("MoveStudentToTeam: %s\n", ReturnValToStr(res));
return error_free;
}
printf("MoveStudentToTeam: %s\n", ReturnValToStr(res));
return error_free;
}
/***************************************************************************/
/* OnRemoveStudent */
/***************************************************************************/
static errorType OnRemoveStudent(void* DS, const char* const command) {
int studentID;
ValidateRead(sscanf(command, "%d", &studentID), 1,
"RemoveStudent failed.\n");
StatusType res = RemoveStudent(DS, studentID);
if (res != SUCCESS) {
printf("RemoveStudent: %s\n", ReturnValToStr(res));
return error_free;
}
printf("RemoveStudent: %s\n", ReturnValToStr(res));
return error_free;
}
/***************************************************************************/
/* OnIncreaseLevel */
/***************************************************************************/
static errorType OnIncreaseLevel(void* DS, const char* const command) {
int grade;
int powerIncrease;
ValidateRead(sscanf(command, "%d %d", &grade, &powerIncrease), 2,
"IncreaseLevel failed.\n");
StatusType res = IncreaseLevel(DS, grade, powerIncrease);
if (res != SUCCESS) {
printf("IncreaseLevel: %s\n", ReturnValToStr(res));
return error_free;
}
printf("IncreaseLevel: %s\n", ReturnValToStr(res));
return error_free;
}
/***************************************************************************/
/* OnGetMostPowerful */
/***************************************************************************/
static errorType OnGetMostPowerful(void* DS, const char* const command) {
int teamID;
ValidateRead(sscanf(command, "%d", &teamID), 1, "GetMostPowerful failed.\n");
int studentID;
StatusType res = GetMostPowerful(DS, teamID, &studentID);
if (res != SUCCESS) {
printf("GetMostPowerful: %s\n", ReturnValToStr(res));
return error_free;
}
cout << "Most powerful student is: " << studentID << endl;
return error_free;
}
/***************************************************************************/
/* PrintAll */
/***************************************************************************/
void PrintAll(int *studentIDs, int numOfStudents) {
if (numOfStudents > 0) {
cout << "Rank\t||\tStudent" << endl;
}
for (int i = 0; i < numOfStudents; i++) {
cout << i + 1 << "\t\t||\t" << studentIDs[i] << endl;
}
cout << "and there are no more students!" << endl;
free (studentIDs);
}
static errorType OnGetAllStudentsByPower(void* DS, const char* const command) {
int teamID;
ValidateRead(sscanf(command, "%d", &teamID), 1,
"GetAllStudentsByPower failed.\n");
int* studentIDs;
int numOfStudents;
StatusType res = GetAllStudentsByPower(DS, teamID, &studentIDs, &numOfStudents);
if (res != SUCCESS) {
printf("GetAllStudentsByPower: %s\n", ReturnValToStr(res));
return error_free;
}
PrintAll(studentIDs, numOfStudents);
return error_free;
}
/***************************************************************************/
/* OnQuit */
/***************************************************************************/
static errorType OnQuit(void** DS, const char* const command) {
Quit(DS);
if (*DS != NULL) {
printf("Quit failed.\n");
return error;
};
isInit = false;
printf("Quit done.\n");
return error_free;
}
#ifdef __cplusplus
}
#endif