bug fixed, program run flawlessly;

clean up on going;
This commit is contained in:
stmctommyau 2022-11-03 18:23:44 +08:00
parent 3646921c32
commit 04af514d7c
4 changed files with 57 additions and 26 deletions

View File

@ -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); 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'); 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{ }else{
bool isadmin = is_admin(db.row[map[i].key].role); int index = map[i].key;
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'); 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* sortUser(struct user db,int choice){
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count);
for (int i = 0; i < db.db.row_count; i++){ for (int i = 0; i < db.db.row_count; i++){
map[i].key = i;
switch(choice){ switch(choice){
case 1: case 1:
case 2: case 2:
@ -1020,11 +1021,15 @@ struct Map* sortUser(struct user db,int choice){
switch (choice){ switch (choice){
case 1: case 1:
case 3: case 3:
case 5: qsort(map, db.db.row_count, sizeof(struct Map), compare_decending_str);
qsort(map, db.db.row_count, sizeof(struct Map), compare_decending);
break; break;
case 2: case 2:
case 4: 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: case 6:
qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending); qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending);
break; break;
@ -1259,13 +1264,13 @@ void showRole(struct linkedlist* list,int index){
struct Map* sortRole(struct linkedlist* list,int choice){ struct Map* sortRole(struct linkedlist* list,int choice){
struct Map* map = malloc(sizeof(struct Map)*sizeofLinkedlist(list)); 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++){ for(int i = 0; i < sizeofLinkedlist(list); i++){
switch(choice){ switch(choice){
case 1: case 1:
case 2: case 2:
map[i].key = i; map[i].key = i;
bool temp = !is_admin(list->data); bool temp = !is_admin(cur->data);
bool* tempstar = malloc(sizeof(bool)); bool* tempstar = malloc(sizeof(bool));
*tempstar = temp; *tempstar = temp;
map[i].value = tempstar; map[i].value = tempstar;
@ -1273,18 +1278,20 @@ struct Map* sortRole(struct linkedlist* list,int choice){
case 3: case 3:
case 4: case 4:
map[i].key = i; map[i].key = i;
map[i].value = list->data; map[i].value = cur->data;
break; break;
} }
cur = cur->next;
} }
switch(choice){ switch(choice){
case 1: case 1:
case 3: case 3:
qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_decending); qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_decending_str);
break; break;
case 2: case 2:
case 4: case 4:
qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_ascending); qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_ascending_str);
break; break;
} }
return map;
} }

View File

@ -23,29 +23,31 @@ typedef struct linkedlist{
int sizeofLinkedlist(struct linkedlist* list){ int sizeofLinkedlist(struct linkedlist* list){
linkedlist* start = list; linkedlist* start = list;
int size = 0; int size = 0;
while (list != NULL){ linkedlist* current = list;
while (current != NULL){
size++; size++;
list = list->next; current = current->next;
} }
list = start; current = start;
while(list->prev != NULL){ while(current->prev != NULL){
size++; size++;
list = list->prev; current = current->prev;
} }
return size; return size;
} }
struct linkedlist* getLinkedList(struct linkedlist* list,int pos){ struct linkedlist* getLinkedList(struct linkedlist* list,int pos){
while(list->prev != NULL){ linkedlist* cur = list;
list = list->prev; while(cur->prev != NULL){
cur = cur->prev;
} }
struct linkedlist* start = list; struct linkedlist* start = cur;
for(int i=0;i<pos;i++){ for(int i=0;i<pos;i++){
list = list->next; cur = cur->next;
if(list == NULL){ if(cur == NULL){
return start;//return start if pos is out of bounds return start;//return start if pos is out of bounds
} }
} }
return list; return cur;
} }
typedef struct basic_db{ typedef struct basic_db{
int row_count; int row_count;

View File

@ -226,15 +226,19 @@ struct Map* sortItems(struct inventory db, int sort){
} }
switch (sort){ switch (sort){
case 1: case 1:
case 3:
case 5: case 5:
case 7: 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; break;
case 2: case 2:
case 4:
case 6: case 6:
case 8: 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); qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending);
break; break;
} }

View File

@ -3,6 +3,24 @@ typedef struct Map{
void* value; void* value;
}Map; }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){ int compare_decending(const void *a, const void *b){
if((*(struct Map *)b).value == NULL){//To prevent null pointer cause error if((*(struct Map *)b).value == NULL){//To prevent null pointer cause error
return -1; return -1;
@ -10,7 +28,7 @@ int compare_decending(const void *a, const void *b){
return 1; 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){ int compare_ascending(const void *a, const void *b){
if((*(struct Map *)b).value == NULL){ if((*(struct Map *)b).value == NULL){
@ -19,6 +37,6 @@ int compare_ascending(const void *a, const void *b){
return 1; return 1;
} }
return (*(struct Map *)a).value - (*(struct Map *)b).value; return (*(struct Map *)a).value -(*(struct Map *)b).value;
} }