Professional Documents
Culture Documents
Physic 1 Chapter 16
Physic 1 Chapter 16
Physic 1 Chapter 16
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 (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 (checkCurrent(cur_char))
{
valid = false;
break;
}
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;
}
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;
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);
}
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;
}
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;
}
{
return 0;
}