Tải bản đầy đủ (.docx) (18 trang)

Code app todo btl nhập môn lập trình

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (36.69 KB, 18 trang )

enum CommandType getCommandType(char * command) {
// TODO
// Requirement 1:
char firstWord[MAX_LENGTH_COMMAND+1];
sscanf(command, "%s", firstWord);

if (strcmp(firstWord, "Add") == 0) {
return ADD;
} else if (strcmp(firstWord, "Edit") == 0) {
return EDIT;
} else if (strcmp(firstWord, "Show") == 0) {
return SHOW;
} else if (strcmp(firstWord, "Delete") == 0) {
return DELETE;
} else if (strcmp(firstWord, "Quit") == 0) {
return QUIT;
}
return INVALID;
}
// Requirement 2:
void getTitleFromAdd(const char* command, char* out_title)
{
sscanf(command, "Add [%[^]]", out_title);
}

void getDescriptionFromAdd(const char* command, char* out_description)
{
sscanf(command, "Add [%*[^]]] [%[^]]", out_description);
}



void getTimeFromAdd(const char* command, char* out_time)
{
sscanf(command, "Add [%*[^]]] [%*[^]]] [%[^]]", out_time);
}
// Requirement 3:
int checkTitle(char *raw_title) {
int titleLength = strlen(raw_title);
int i;

if (titleLength > 100) {
return titleLength;
}

if (isspace(raw_title[0]) || isspace(raw_title[titleLength - 1])) {
return 0;
}

for (i = 0; i < titleLength; i++) {
char currentChar = raw_title[i];
if (!(isalpha(currentChar) || isdigit(currentChar) || isspace(currentChar) ||
currentChar == ',' || currentChar == '.' || currentChar == '-' ||
currentChar == ':' || currentChar == '|' || currentChar == '/')) {
return i;
}
}

return -1;
}



// Requirement 4:
int checkDescription(char *raw_description) {
int descriptionLength = strlen(raw_description);
int i;

if (descriptionLength > 200) {
return descriptionLength;
}

if (isspace(raw_description[0]) || isspace(raw_description[descriptionLength - 1])) {
return 0;
}

for (i = 0; i < descriptionLength; i++) {
char currentChar = raw_description[i];
if (!(isalpha(currentChar) || isdigit(currentChar) || isspace(currentChar) ||
currentChar == ',' || currentChar == '.' || currentChar == '-' ||
currentChar == ':' || currentChar == '|' || currentChar == '/')) {
return i;
}
}

return -1;
}

int checkTime(char *raw_time) {
char datetime1[MAX_LENGTH_TIME+1];
char datetime2[MAX_LENGTH_TIME+1];



int i = 0;
while (raw_time[i] != '-') {
datetime1[i] = raw_time[i];
i++;
}
datetime1[i] = '\0';
i++;

int j = 0;
while (raw_time[i] != '\0') {
datetime2[j] = raw_time[i];
i++;
j++;
}
datetime2[j] = '\0';

int hh1, mm1, dd1, mo1, yyyy1;
if (sscanf(datetime1, "%d:%d|%d/%d/%d", &hh1, &mm1, &dd1, &mo1, &yyyy1) != 5) {
return atoi("1100") + hh1;
}

if (hh1 < 0 || hh1 > 23) {
return atoi("1100") + hh1;
}

if (mm1 < 0 || mm1 > 59) {


return atoi("2100") + mm1;
}


if (yyyy1 <= 0) {
return atoi("510000") + yyyy1;
}

int hh2, mm2, dd2, mo2, yyyy2;
if (sscanf(datetime2, "%d:%d|%d/%d/%d", &hh2, &mm2, &dd2, &mo2, &yyyy2) != 5) {
return atoi("1200") + hh2;
}

if (hh2 < 0 || hh2 > 23) {
return atoi("1200") + hh2;
}

if (mm2 < 0 || mm2 > 59) {
return atoi("2200") + mm2;
}

if (yyyy2 <= 0) {
return atoi("520000") + yyyy2;
}

int max_days;
switch (mo1) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:


max_days = 31;
break;
case 4: case 6: case 9: case 11:

max_days = 30;
break;
case 2:
if ((yyyy1 % 4 == 0 && yyyy1 % 100 != 0) || (yyyy1 % 400 == 0))
max_days = 29;
else
max_days = 28;
break;
default:
return atoi("4100") + mo1;
}

if (dd1 < 1 || dd1 > max_days) {
return atoi("3100") + dd1;
}

switch (mo2) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
max_days = 31;
break;
case 4: case 6: case 9: case 11:
max_days = 30;
break;
case 2:
if ((yyyy2 % 4 == 0 && yyyy2 % 100 != 0) || (yyyy2 % 400 == 0))


max_days = 29;
else
max_days = 28;

break;
default:
return atoi("4200") + mo2;
}

if (dd2 < 1 || dd2 > max_days) {
return atoi("3200") + dd2;
}

if (yyyy2 < yyyy1 ||
(yyyy2 == yyyy1 && mo2 < mo1) ||
(yyyy2 == yyyy1 && mo2 == mo1 && dd2 < dd1) ||
(yyyy2 == yyyy1 && mo2 == mo1 && dd2 == dd1 && hh2 < hh1) ||
(yyyy2 == yyyy1 && mo2 == mo1 && dd2 == dd1 && hh2 == hh1 && mm2 < mm1)) {
return 0;
}

return -1;
}
//Requirement 6:
void getTitleFromEdit(char* command, char* out_title)
{
char* start = strstr(command, "title:[");
if (start != NULL)
{


start += strlen("title:[");
char* end = strchr(start, ']');
if (end != NULL)

{
strncpy(out_title, start, end - start);
out_title[end - start] = '\0';
}
}
}

void getDescriptionFromEdit(char* command, char* out_description)
{
char* start = strstr(command, "description:[");
if (start != NULL)
{
start += strlen("description:[");
char* end = strchr(start, ']');
if (end != NULL)
{
strncpy(out_description, start, end - start);
out_description[end - start] = '\0';
}
}
}

void getTimeFromEdit(char* command, char* out_time)
{
char* start = strstr(command, "time:[");
if (start != NULL)


{
start += strlen("time:[");

char* end = strchr(start, ']');
if (end != NULL)
{
strncpy(out_time, start, end - start);
out_time[end - start] = '\0';
}
}
}

// Requirement 7:
int getNumFromCommand(char* command)
{
char* start = strchr(command, '#');
if (start == NULL)
{
return -1;
}
start++;

int num = atoi(start);

if (num <= 0)
{
return 0;
}

return num;


}


// Requirement 8:
int getFieldFromEdit(char* edit_cmd) {
char* first_space = strchr(edit_cmd, ' ');
if (first_space == NULL) {
return 0; // No first space found
}

char* second_space = strchr(first_space + 1, ' ');
if (second_space == NULL) {
return 0; // No second space found
}

char* first_colon = strchr(second_space, ':');
if (first_colon == NULL || first_colon == second_space + 1) {
return 0; // No first colon found or empty field name
}

char field[20];
strncpy(field, second_space + 1, first_colon - second_space - 1);
field[first_colon - second_space - 1] = '\0';

if (strcmp(field, "title") == 0) {
return 1; // Field to be changed is "title"
} else if (strcmp(field, "description") == 0) {
return 2; // Field to be changed is "description"
} else if (strcmp(field, "time") == 0) {
return 3; // Field to be changed is "time"



} else if (strcmp(field, "status") == 0) {
return 4; // Field to be changed is "status"
}

return 0; // Invalid field to be changed
}
// Requirement 9:
enum Status getStatusFromEdit(char* edit_cmd)
{
char* first_space = strchr(edit_cmd, ' ');
if (first_space == NULL)
{
return IN_PROGRESS; // Khơng tìm th?y kho?ng tr?ng th? hai, m?c d?nh là In Progress
}

char* second_space = strchr(first_space + 1, ' ');
if (second_space == NULL) {
return IN_PROGRESS; // No second space found
}

char* colon = strchr(second_space, ':');
if (colon == NULL || colon == second_space + 1)
{
return IN_PROGRESS; // Khơng tìm th?y d?u hai ch?m ho?c khơng có chu?i c?n thay d?i, m?c d?nh là
In Progress
}

char status = *(colon + 2);



if (status == 'I' || status == 'i')
{
return IN_PROGRESS; // Tr?ng thái là In Progress
}
else if (status == 'D' || status == 'd')
{
return DONE; // Tr?ng thái là Done
}
else if (status == 'A' || status == 'a')
{
return ARCHIVED; // Tr?ng thái là Archived
}

return IN_PROGRESS; // Tr?ng thái không h?p l?, m?c d?nh là In Progress
}

//cau 10
void printAllTasks(struct Task * array_tasks, int no_tasks)

{int i;
for ( i = 0; i < no_tasks; i++)
{
printTask(&array_tasks[i]);
}
}
// yêu c?u 11
void printTaskByNum(struct Task * array_tasks, int no_tasks,int num)

{int i;



for (i = 0; i < no_tasks; i++)
{
if (array_tasks[i].num == num) // ki?m tra trong c?u trúc task có num trùng v?i num c?a l?nh show
hay không
{
printTask(&array_tasks[i]); // in ra màn hình task c?a num
return; // ng?t vịng l?p
}
}
printf("Task with num %d not found.\n", num); // n?u khơng tìm th?y s? báo l?i
}
// yêu c?u 12
void printHeadTasks(struct Task * array_tasks, int no_tasks,int quan)
{int i;
if(quan <= no_tasks)
{
for( i=0;i{
printTask(&array_tasks[i]);
}
}
if(quan > no_tasks)
{
printAllTasks(array_tasks,no_tasks);
}

}
// yêu c?u 13
void printTailTasks(struct Task * array_tasks, int no_tasks,int quan)



{int i;
int start = no_tasks - quan;
if(quan <= no_tasks)
{
for( i = no_tasks-1; i >= start; i--)
{
printTask(&array_tasks[i]);
}
}
if(quan > no_tasks)
{
printAllTasks(array_tasks,no_tasks);
}
}
// yêu c?u 14
void printFilteredTasksByTitle(struct Task * array_tasks,int no_tasks, char * filter_title)
{int i;
int titleCheck = checkTitle(filter_title);
if(titleCheck != -1)
{
printf("Invalid title %i.\n", titleCheck);
return;
}

else
{

for(i = 0; i < no_tasks; i++)

{


if(strstr(array_tasks[i].title,filter_title) != NULL )
{
printTask(&array_tasks[i]);
}
}
}
}
// yêu c?u 15
void printFilteredTasksByDescription(struct Task * array_tasks,int no_tasks, char * filter_description)
{int i;
int descriptionCheck = checkDescription(filter_description);
if (descriptionCheck != -1)
{
printf("Invalid description %i.\n", descriptionCheck);
return;
}
else
{
for(i=0;i{
if(strstr(array_tasks[i].description,filter_description) != NULL)
{
printTask(&array_tasks[i]);
}
}
}
}

// yêu c?u 16
void printFilteredTasksByStatus(struct Task * array_tasks,int no_tasks, enum Status filter_status)


{int i;
for( i = 0; i < no_tasks; i++)
{
if(array_tasks[i].status == filter_status)
{
printTask(&array_tasks[i]);
}
}
}
// yêu c?u 17
bool addTask(struct Task * array_tasks, int no_tasks, char* new_title, char * new_description, char *
new_time)
{
if (no_tasks >= MAX_NO_TASKS) {
return false;
}
int titleCheck = checkTitle(new_title);
int descriptionCheck = checkDescription(new_description);
int timeCheck = checkTime(new_time);
if(titleCheck != -1)
{
return false;
}
if (descriptionCheck != -1) {
return false;
}

if (timeCheck != -1) {
printf("Invalid time %i.\n", timeCheck);
return false;


}
strcpy(array_tasks[no_tasks].title, new_title);
strcpy(array_tasks[no_tasks].description, new_description);
strcpy(array_tasks[no_tasks].time, new_time);
array_tasks[no_tasks].num = no_tasks+1;
array_tasks[no_tasks].status = IN_PROGRESS;
return true;

}
// yêu c?u 18
bool deleteTask(struct Task * array_tasks, int no_tasks, int num)
// l?u ý quan tr?ng vì hàm bool ch? tr? v? true ho?c false nên khi ta thay ??i no_task sau khi xóa xong s?
ko ?c c?p nh?p ? main
// vd có 2 task sau khi xóa task a thì khi ta show all nó s? hi?n lên 2 task b
// ?? kh?c ph?c ta b?ng cách s? d?ng con tr?. ?i?u này cho phép b?n truy c?p và thay ??i giá tr? c?a bi?n
g?c trong hàm.
{int i;
if (num < 1 || num > no_tasks) // báo l?i n?u hàm in ra sai
{
return false;
}
for (i = num - 1; i < no_tasks - 1; i++) {
strcpy(array_tasks[i].title, array_tasks[i + 1].title);
strcpy(array_tasks[i].description, array_tasks[i + 1].description);
strcpy(array_tasks[i].time, array_tasks[i + 1].time);

array_tasks[i].status = array_tasks[i+1].status;

}
no_tasks--; //nó s? ?i ??n ??a chi l?u no_tasks và th?c hi?n phép tính gi?m ?i 1


return true;
}
// yêu c?u 19
int printWeekTime(struct Task * array_tasks, int no_tasks,char * date)
{
//t?o c?u trúc weektime
struct week
{
int num;
char week_title[MAX_LENGTH_TITLE+1];
char week_description[MAX_LENGTH_DESCRIPTION+1];
char week_time[MAX_LENGTH_TIME+1];
};
struct week *weektime[7][24];
return 1;

}

// Other functions



×