From 04af514d7c182df324a271121573d8af3ec32320 Mon Sep 17 00:00:00 2001 From: stmctommyau Date: Thu, 3 Nov 2022 18:23:44 +0800 Subject: [PATCH] bug fixed, program run flawlessly; clean up on going; --- admin_user.h | 27 +++++++++++++++++---------- database.h | 24 +++++++++++++----------- normal_user.h | 10 +++++++--- sorting.h | 22 ++++++++++++++++++++-- 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/admin_user.h b/admin_user.h index 890c3e8..f537c55 100644 --- a/admin_user.h +++ b/admin_user.h @@ -984,8 +984,9 @@ void print_user(struct user db, int cur, int end,struct Map* map){ bool isadmin = is_admin(db.row[i].role); printf("%-5d%-20s%-10ld%-10s%-10c\n",i+7,db.row[i].name,db.row[i].id,db.row[i].role,isadmin?'Y':'N'); }else{ - bool isadmin = is_admin(db.row[map[i].key].role); - printf("%-5s%-20s%-10ld%-10s%-10c\n",i+7,db.row[map[i].key].name,db.row[map[i].key].id,db.row[map[i].key].role,isadmin?'Y':'N'); + int index = map[i].key; + bool isadmin = is_admin(db.row[index].role); + printf("%-5d%-20s%-10ld%-10s%-10c\n",i+7,db.row[index].name,db.row[index].id,db.row[index].role,isadmin?'Y':'N'); } } } @@ -993,7 +994,7 @@ void print_user(struct user db, int cur, int end,struct Map* map){ struct Map* sortUser(struct user db,int choice){ struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); for (int i = 0; i < db.db.row_count; i++){ - + map[i].key = i; switch(choice){ case 1: case 2: @@ -1020,11 +1021,15 @@ struct Map* sortUser(struct user db,int choice){ switch (choice){ case 1: case 3: - case 5: - qsort(map, db.db.row_count, sizeof(struct Map), compare_decending); + qsort(map, db.db.row_count, sizeof(struct Map), compare_decending_str); break; case 2: case 4: + qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending_str); + break; + case 5: + qsort(map, db.db.row_count, sizeof(struct Map), compare_decending); + break; case 6: qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending); break; @@ -1259,13 +1264,13 @@ void showRole(struct linkedlist* list,int index){ struct Map* sortRole(struct linkedlist* list,int choice){ struct Map* map = malloc(sizeof(struct Map)*sizeofLinkedlist(list)); - list = getLinkedList(list,0); + struct linkedlist* cur = getLinkedList(list,0); for(int i = 0; i < sizeofLinkedlist(list); i++){ switch(choice){ case 1: case 2: map[i].key = i; - bool temp = !is_admin(list->data); + bool temp = !is_admin(cur->data); bool* tempstar = malloc(sizeof(bool)); *tempstar = temp; map[i].value = tempstar; @@ -1273,18 +1278,20 @@ struct Map* sortRole(struct linkedlist* list,int choice){ case 3: case 4: map[i].key = i; - map[i].value = list->data; + map[i].value = cur->data; break; } + cur = cur->next; } switch(choice){ case 1: case 3: - qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_decending); + qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_decending_str); break; case 2: case 4: - qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_ascending); + qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_ascending_str); break; } + return map; } \ No newline at end of file diff --git a/database.h b/database.h index 375b2c4..09507e5 100644 --- a/database.h +++ b/database.h @@ -23,29 +23,31 @@ typedef struct linkedlist{ int sizeofLinkedlist(struct linkedlist* list){ linkedlist* start = list; int size = 0; - while (list != NULL){ + linkedlist* current = list; + while (current != NULL){ size++; - list = list->next; + current = current->next; } - list = start; - while(list->prev != NULL){ + current = start; + while(current->prev != NULL){ size++; - list = list->prev; + current = current->prev; } return size; } struct linkedlist* getLinkedList(struct linkedlist* list,int pos){ - while(list->prev != NULL){ - list = list->prev; + linkedlist* cur = list; + while(cur->prev != NULL){ + cur = cur->prev; } - struct linkedlist* start = list; + struct linkedlist* start = cur; for(int i=0;inext; - if(list == NULL){ + cur = cur->next; + if(cur == NULL){ return start;//return start if pos is out of bounds } } - return list; + return cur; } typedef struct basic_db{ int row_count; diff --git a/normal_user.h b/normal_user.h index e963e57..9de73bf 100644 --- a/normal_user.h +++ b/normal_user.h @@ -226,15 +226,19 @@ struct Map* sortItems(struct inventory db, int sort){ } switch (sort){ case 1: - case 3: case 5: case 7: - qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending); + qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending_str); break; case 2: - case 4: case 6: case 8: + qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending_str); + break; + case 3: + qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending); + break; + case 4: qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending); break; } diff --git a/sorting.h b/sorting.h index ba95a30..c347283 100644 --- a/sorting.h +++ b/sorting.h @@ -3,6 +3,24 @@ typedef struct Map{ void* value; }Map; +int compare_decending_str(const void *a, const void *b){ + if((*(struct Map *)b).value == NULL){//To prevent null pointer cause error + return -1; + }else if((*(struct Map *)a).value == NULL){ + return 1; + + } + return strcmp((*(struct Map *)b).value,(*(struct Map *)a).value); +} +int compare_ascending_str(const void *a, const void *b){ + if((*(struct Map *)b).value == NULL){ + return -1; + }else if((*(struct Map *)a).value == NULL){ + return 1; + + } + return strcmp((*(struct Map *)a).value,(*(struct Map *)b).value); +} int compare_decending(const void *a, const void *b){ if((*(struct Map *)b).value == NULL){//To prevent null pointer cause error return -1; @@ -10,7 +28,7 @@ int compare_decending(const void *a, const void *b){ return 1; } - return (*(struct Map *)b).value - (*(struct Map *)a).value; + return (*(struct Map *)b).value-(*(struct Map *)a).value; } int compare_ascending(const void *a, const void *b){ if((*(struct Map *)b).value == NULL){ @@ -19,6 +37,6 @@ int compare_ascending(const void *a, const void *b){ return 1; } - return (*(struct Map *)a).value - (*(struct Map *)b).value; + return (*(struct Map *)a).value -(*(struct Map *)b).value; }