You are on page 1of 11

// ------ Begin: Student Answer ------

enum CommandType getCommandType(char *command)


{
if (command == NULL || command[0] == '\0')
{
return INVALID;
}

char firstWord[20];
sscanf(command, "%s", firstWord);
if (strcmp(firstWord, "Quit") == 0)
{
return QUIT;
}
else if (strcmp(firstWord, "Delete") == 0)
{
return DELETE;
}
else if (strcmp(firstWord, "Add") == 0)
{
return ADD;
}
else if (strcmp(firstWord, "Edit") == 0)
{
return EDIT;
}
else if (strcmp(firstWord, "Show") == 0)
{
return SHOW;
}
else
{
return INVALID;
}
}
void getTitleFromAdd(char *command, char *out_title)
{
char *titleStart = strstr(command, "[");
char *titleEnd = strstr(titleStart, "]");

if (titleStart != NULL && titleEnd != NULL)


{
titleStart++;
int length = titleEnd - titleStart;
strncpy(out_title, titleStart, length);
out_title[length] = '\0';
}
else
{
strcpy(out_title, "");
}
}
void getDescriptionFromAdd(char *command, char *out_description)
{
char *descriptionStart = strstr(command, "[");
descriptionStart = strstr(descriptionStart + 1, "[");
char *descriptionEnd = strstr(descriptionStart, "]");

if (descriptionStart != NULL && descriptionEnd != NULL)


{
descriptionStart++;
int length = descriptionEnd - descriptionStart;
strncpy(out_description, descriptionStart, length);
out_description[length] = '\0';
}
else
{
strcpy(out_description, "");
}
}
void getTimeFromAdd(char *command, char *out_time)
{
char *timeStart = strstr(command, "[");
timeStart = strstr(timeStart + 1, "[");
timeStart = strstr(timeStart + 1, "[");
char *timeEnd = strstr(timeStart, "]");

if (timeStart != NULL && timeEnd != NULL)


{
timeStart++;
int length = timeEnd - timeStart;
strncpy(out_time, timeStart, length);
out_time[length] = '\0';
}
else
{
strcpy(out_time, "");
}
}
bool checkCurrent(char cur_char)
{
if (!((cur_char >= 'a' && cur_char <= 'z') ||
(cur_char >= 'A' && cur_char <= 'Z') ||
(cur_char >= '0' && cur_char <= '9') ||
cur_char == ' ' || cur_char == ',' ||
cur_char == '.' || cur_char == '-' ||
cur_char == ':' || cur_char == '|' ||
cur_char == '/'))
{
return true;
}
return false;
}
int checkTitle(char *raw_title)
{
int max_length = MAX_LENGTH_TITLE;
int length = strlen(raw_title);
if (length > max_length)
{
return length;
}
bool valid = true;
for (int i = 0; i < length; i++)
{
char cur_char = raw_title[i];
if (checkCurrent(cur_char))
{
valid = false;
break;
}

if (i == 0 && cur_char == ' ')


{
return 0;
}
if (i == length - 1 && cur_char == ' ')
{
return length - 1;
}
}

if (valid)
{
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
char cur_char = raw_title[i];
if (checkCurrent(cur_char))
{
return i;
}
}
}

return -1;
}
int checkDescription(char *raw_description)
{
int max_length = MAX_LENGTH_DESCRIPTION;
int length = strlen(raw_description);

if (length > max_length)


{
return length;
}
bool valid = true;
for (int i = 0; i < length; i++)
{
char cur_char = raw_description[i];

if (checkCurrent(cur_char))
{
valid = false;
break;
}

if (i == 0 && cur_char == ' ')


{
return 0;
}
if (i == length - 1 && cur_char == ' ')
{
return length - 1;
}
}

if (valid)
{
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
char cur_char = raw_description[i];
if (checkCurrent(cur_char))
{
return i;
}
}
}

return -1;
}

bool checkCondition(bool valid_hour1, bool valid_minute1, bool valid_year1, bool


valid_month1, bool valid_day1, bool valid_hour2, bool valid_minute2, bool
valid_year2, bool valid_month2, bool valid_day2)
{
if (valid_hour1 && valid_minute1 && valid_year1 && valid_month1 && valid_day1
&&
valid_hour2 && valid_minute2 && valid_year2 && valid_month2 && valid_day2)
{
return true;
}
return false;
}
int checkTime(char *raw_time)
{
int hh1, mm1, dd1, mo1, yyyy1;
int hh2, mm2, dd2, mo2, yyyy2;

if (sscanf(raw_time, "%d:%d|%d/%d/%d-%d:%d|%d/%d/%d",
&hh1, &mm1, &dd1, &mo1, &yyyy1, &hh2, &mm2, &dd2, &mo2, &yyyy2) ==
10)
{
bool valid_hour1 = (hh1 >= 0 && hh1 < 24);
bool valid_minute1 = (mm1 >= 0 && mm1 < 60);
bool valid_year1 = (yyyy1 > 0 && yyyy1 < 10000);
bool valid_month1 = (mo1 > 0 && mo1 < 13);
bool valid_day1 = false;

bool valid_hour2 = (hh2 >= 0 && hh2 < 24);


bool valid_minute2 = (mm2 >= 0 && mm2 < 60);
bool valid_year2 = (yyyy2 > 0 && yyyy2 < 10000);
bool valid_month2 = (mo2 > 0 && mo2 < 13);
bool valid_day2 = false;

if (mo1 == 2)
{
if ((yyyy1 % 4 == 0 && yyyy1 % 100 != 0) || (yyyy1 % 400 == 0))
{
valid_day1 = (dd1 > 0 && dd1 < 30);
}
else
{
valid_day1 = (dd1 > 0 && dd1 < 29);
}
}
else if (mo1 == 4 || mo1 == 6 || mo1 == 9 || mo1 == 11)
{
valid_day1 = (dd1 >= 1 && dd1 < 31);
}
else
{
valid_day1 = (dd1 >= 1 && dd1 < 32);
}

if (mo2 == 2)
{
if ((yyyy2 % 4 == 0 && yyyy2 % 100 != 0) || (yyyy2 % 400 == 0))
{
valid_day2 = (dd2 >= 1 && dd2 < 30);
}
else
{
valid_day2 = (dd2 >= 1 && dd2 < 29);
}
}
else if (mo2 == 4 || mo2 == 6 || mo2 == 9 || mo2 == 11)
{
valid_day2 = (dd2 > 0 && dd2 < 31);
}
else
{
valid_day2 = (dd2 > 0 && dd2 < 32);
}

if (checkCondition(valid_hour1, valid_minute1, valid_year1, valid_month1,


valid_day1, valid_hour2, valid_minute2, valid_year2, valid_month2, valid_day2))
{
if (yyyy2 > yyyy1 || (yyyy2 == yyyy1 && (mo2 > mo1 || (mo2 == mo1 &&
(dd2 > dd1 || (dd2 == dd1 && (hh2 > hh1 || (hh2 == hh1 && mm2 >= mm1))))))))
{
return -1;
}
else
{
return 0;
}
}
else
{
if (!valid_hour1)
{
return 1100 + hh1;
}
else if (!valid_hour2)
{
return 1200 + hh2;
}
else if (!valid_minute1)
{
return 2100 + mm1;
}
else if (!valid_minute2)
{
return 2200 + mm2;
}
else if (!valid_month1)
{
return 4100 + mo1;
}
else if (!valid_month2)
{
return 4200 + mo2;
}
else if (!valid_day1)
{
return 3100 + dd1;
}
else if (!valid_day2)
{
return 3200 + dd2;
}
else if (!valid_year1)
{
return 510000 + yyyy1;
}
else if (!valid_year2)
{
return 520000 + yyyy2;
}
}
}

return 0;
}

void getTitleFromEdit(char *command, char *out_title)


{
char *title_start = strstr(command, "title:[");
if (title_start != NULL)
{
title_start = title_start + 7;
char *title_end = strstr(title_start, "]");
if (title_end != NULL)
{
strncpy(out_title, title_start, title_end - title_start);
out_title[title_end - title_start] = '\0';
}
}
}
void getDescriptionFromEdit(char *command, char *out_description)
{
char *description_start = strstr(command, "description:[");
if (description_start != NULL)
{
description_start = description_start + 13;
char *description_end = strstr(description_start, "]");
if (description_end != NULL)
{
strncpy(out_description, description_start, description_end -
description_start);
out_description[description_end - description_start] = '\0';
}
}
}
void getTimeFromEdit(char *command, char *out_time)
{
char *time_start = strstr(command, "time:[");
if (time_start != NULL)
{
time_start = time_start + 6;
char *time_end = strstr(time_start, "]");
if (time_end != NULL)
{
strncpy(out_time, time_start, time_end - time_start);
out_time[time_end - time_start] = '\0';
}
}
}
int getNumFromCommand(char *command)
{
int num;
char *numStart = strchr(command, '#');
if (numStart == NULL)
{
return -1;
}

if (sscanf(numStart + 1, "%d", &num) != 1)


{
return 0;
}

if (num < 1)
{
return 0;
}

return num;
}
bool checkGetFieldFromEdit(char description_pos, char title_pos, char time_pos,
char status_pos)
{
if ((description_pos == NULL || title_pos < description_pos) &&
(time_pos == NULL || title_pos < time_pos) &&
(status_pos == NULL || title_pos < status_pos))
{
return true;
}
return false;
}
int getFieldFromEdit(char *edit_cmd)
{
char *title_pos = strstr(edit_cmd, "title:");
char *description_pos = strstr(edit_cmd, "description:");
char *time_pos = strstr(edit_cmd, "time:");
char *status_pos = strstr(edit_cmd, "status:");
if (title_pos != NULL)
{
if (checkGetFieldFromEdit(description_pos, title_pos, time_pos,
status_pos))
{
return 1;
}
}

if (description_pos != NULL)
{
if ((title_pos == NULL || description_pos < title_pos) &&
(time_pos == NULL || description_pos < time_pos) &&
(status_pos == NULL || description_pos < status_pos))
{
return 2;
}
}

if (time_pos != NULL)
{
if ((title_pos == NULL || time_pos < title_pos) &&
(description_pos == NULL || time_pos < description_pos) &&
(status_pos == NULL || time_pos < status_pos))
{
return 3;
}
}

if (status_pos != NULL)
{
if ((title_pos == NULL || status_pos < title_pos) &&
(description_pos == NULL || status_pos < description_pos) &&
(time_pos == NULL || status_pos < time_pos))
{
return 4;
}
}
return 0;
}
enum Status getStatusFromEdit(char *edit_cmd)
{
char *status_pos = strstr(edit_cmd, "status:[");
if (status_pos != NULL)
{
status_pos = status_pos + 8;
char status_char = status_pos[0];

switch (status_char)
{
case 'I':
case 'i':
return IN_PROGRESS;
case 'D':
case 'd':
return DONE;
case 'A':
case 'a':
return ARCHIVED;
}
}

return -1;
}

void printAllTasks(struct Task *array_tasks, int no_tasks)


{
int i = 0;
while (i < no_tasks)
{
printTask(&array_tasks[i]);
i++;
}
}
void printTaskByNum(struct Task *array_tasks, int no_tasks, int num)
{
int i = 0;
while (i < no_tasks)
{
if (array_tasks[i].num == num)
{
printTask(&array_tasks[i]);
return;
}
i++;
}
}
void printHeadTasks(struct Task *array_tasks, int no_tasks, int quan)
{
int i = 0;
while (i < quan && i < no_tasks)
{
printTask(&array_tasks[i]);
i++;
}
}
void printTailTasks(struct Task *array_tasks, int no_tasks, int quan)
{
bool check = false;
if (no_tasks >= quan)
{
check = true;
}
int start;
if (check)
{
start = no_tasks - quan;
}
else
{
start = 0;
}

for (int i = start; i < no_tasks; ++i)


{
printTask(&array_tasks[i]);
}
}
void printFilteredTasksByTitle(struct Task *array_tasks, int no_tasks, char
*filter_title)
{
for (int i = 0; i < no_tasks; ++i)
{
if (strstr(array_tasks[i].title, filter_title) != NULL)
{
printTask(&array_tasks[i]);
}
}
}
void printFilteredTasksByDescription(struct Task *array_tasks, int no_tasks, char
*filter_description)
{
for (int i = 0; i < no_tasks; ++i)
{
if (strstr(array_tasks[i].description, filter_description) != NULL)
{
printTask(&array_tasks[i]);
}
}
}
void printFilteredTasksByStatus(struct Task *array_tasks, int no_tasks, enum Status
filter_status)
{
for (int i = 0; i < no_tasks; ++i)
{
if (array_tasks[i].status == filter_status)
{
printTask(&array_tasks[i]);
}
}
}
bool addTask(struct Task *array_tasks, int no_tasks, char *new_title, char
*new_description, char *new_time)
{
if (array_tasks == NULL || no_tasks < 0 || new_title == NULL || new_description
== NULL || new_time == NULL)
{
return false;
}
if (no_tasks >= MAX_NO_TASKS)
{
return false;
}
array_tasks[no_tasks].num = no_tasks + 1;
strncpy(array_tasks[no_tasks].title, new_title, MAX_LENGTH_TITLE);
strncpy(array_tasks[no_tasks].description, new_description,
MAX_LENGTH_DESCRIPTION);
strncpy(array_tasks[no_tasks].time, new_time, MAX_LENGTH_TIME);
array_tasks[no_tasks].status = IN_PROGRESS;

return true;
}
bool deleteTask(struct Task *array_tasks, int no_tasks, int num)
{
array_tasks[num - 1] = array_tasks[no_tasks - 1];
for (int i = 0; i < no_tasks - 1; i++)
{
if (array_tasks[i].num > num)
{
array_tasks[i].num--;
}
}

return true;
}

int printWeekTime(struct Task *array_tasks, int no_tasks, char *date)

{
return 0;
}

You might also like