借书证信息管理系统,C语言实现
自己实现的如有缺漏欢迎提出
/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10325628.html */
设计内容:
设计一个排序和查找系统。能够实现对给定的一组学生的借书证信息(如:卡号、姓名、系别、班号等)进行排序和查找。
1)按照卡号顺序进行排序;
2)能够实现查找某个系的所有的借书卡号并输出。
设计要求:
(1)建立关于借书证信息结点的结构体;
(2)定义借书证信息的记录并录入基本信息;
(3)写出用某种排序算法(如冒泡排序)按关键字对记录进行排序的算法函数;
(4)对借书证信息的记录按系名建立索引查找结构;
(5)输入某个要查找的系名,用索引查找方法查找并输出该系的所有借书证信息。
是学期末的课程设计,自己弄完觉得还可以,提供给有需要的人做参考。
有图万岁:
下面是代码实现:
1.头文件:(my_head.h)
1 #pragma once 2 #define _crt_secure_no_warnings 3 #include<stdio.h> 4 #include<windows.h> 5 #include<stdlib.h> 6 7 //这里改系别总个数 8 #define depratment_number 3 9 10 typedef int once_sort; 11 12 typedef struct card_node { 13 14 once_sort is_sort; 15 16 long long number; 17 char *name; 18 char *department; 19 long long class_number; 20 struct card_node *next_node; 21 struct card_node *same_department_next_node; 22 }cr_node; 23 24 25 cr_node* initialize(cr_node** head); 26 int initialize_sort(cr_node *head); 27 // 28 int print(cr_node* head); 29 30 int print_one(cr_node*node); 31 32 int print_on_same_depart(cr_node *same_depaet_head); 33 34 int print_same_depart(cr_node *index_list[depratment_number], char *str_depratment[depratment_number]); 35 36 // 37 int input(cr_node*head); 38 39 // 40 cr_node * find(cr_node * head, long long number); 41 42 // 43 cr_node* delete_cr_crde(cr_node * head, long long number); 44 45 // 46 cr_node *make_indexes_list(cr_node *head, char _str_depratment[]); 47 48 cr_node *get_indexes_list(cr_node *head, char *department[], cr_node *index_list[depratment_number]); 49 50 // 51 int sort_class_number(cr_node **head); 52 int get_class_sort_list(cr_node *head); 53 int sort_class_link(cr_node **head, int total); 54 55 int sort_crad_number(cr_node **head); 56 int get_crad_number_sort_list(cr_node *head); 57 int sort_card_link(cr_node **head, int total);
2.main函数入口:(source_1.c)
1 #include"my_head.h" 2 3 int card_number = 0; 4 5 //point menu 6 void print_menu() 7 { 8 printf(" \n"); 9 printf(" /-----------------------------------------------/\n"); 10 printf(" / *** 借书证管理系统 ******/\n"); 11 printf(" / -->选项 : ******/\n"); 12 printf(" / *** 1.按卡号排序并输出前后结果 < ******/\n"); 13 printf(" / *** 2.差找并输一个系的所有借书卡号 < ******/\n"); 14 printf(" / *** 3.录入一个借书证 < ******/\n"); 15 printf(" / *** 4.删除一个借书证 < ******/\n"); 16 printf(" / *** 5.按关键字(例如 班号)进行排序 < ******/\n"); 17 printf(" / *** 6.建立索引表并用其查找 < ******/\n"); 18 printf(" / *** 7.退出本程序 < ******/\n"); 19 printf(" / *** 8.打印现有的借书证信息 < ******/\n"); 20 printf(" / *** 9.惊喜 最好不要选 < ******/\n"); 21 printf(" /-----------------------------------------------/\n"); 22 printf("\n \a #_# 请输入所要执行的操作 #_#\n"); 23 printf("\n (请输入数字 1-8 选择操作)\n"); sleep(500); 24 } 25 26 int main() 27 { 28 system("color d"); 29 30 system("title . #借书证信息管理系统# "); 31 32 cr_node* head=null; 33 cr_node* temp_node = null; 34 cr_node *index_list[depratment_number]; 35 36 cr_node *sort_list[10]; 37 int i = 0; 38 39 char str_find_depart_temp[64]; 40 41 long long temp = 0; 42 int i_find_same = 0; 43 44 initialize(&head); 45 46 //添加系别步骤 1.在下方字符串里添加系名 2。在头文件定义里更改系别总个数 47 char *str_depratment[depratment_number] = { "计算机系","外语系","艺术系" }; 48 49 50 int operation; 51 int exit_op; 52 operation = 0; 53 54 while (1) 55 { 56 print_menu(); 57 operation = 10; 58 printf("\n请输入你要进行的操作 (确认输入时可能有时需两次按下 enter 键): \n"); 59 60 scanf("%d",&operation); 61 62 {getchar(); getchar(); } 63 64 switch (operation) 65 { 66 case 1: //排序 67 68 sort_crad_number(&head); 69 70 system("pause"); 71 sleep(200); 72 system("cls"); 73 break; 74 75 case 2: //查找一个系的全部成员并输出 76 77 get_indexes_list(head, str_depratment, index_list); 78 79 printf("请输入你要查找的系的名称 注: 先有 如下系名记录在系统 \n"); 80 81 gets_s(str_find_depart_temp,sizeof(str_find_depart_temp)); 82 printf("%s\n", str_find_depart_temp); 83 if ("\\0" == str_find_depart_temp||"" == str_find_depart_temp) 84 { 85 printf("输入的系别有误 请重新输入\n"); 86 goto end; 87 } 88 89 { 90 int x=0; 91 92 while (x < depratment_number) 93 { 94 if (strstr(index_list[x]->department, str_find_depart_temp)) 95 { 96 print_on_same_depart(index_list[x]); 97 goto end; 98 } 99 ++x; 100 } 101 if (x > depratment_number) 102 { 103 printf("输入的系别有误 请重新输入\n"); 104 goto end; 105 } 106 } 107 108 109 110 111 112 113 end: system("pause"); 114 sleep(200); 115 system("cls"); 116 break; 117 118 case 3: //录入一个 119 120 input(head); 121 122 system("pause"); 123 sleep(200); 124 system("cls"); 125 break; 126 127 case 4: //删除 128 129 printf("please input card_number : \n"); 130 scanf("%lld", &temp); 131 132 head = delete_cr_crde(head, temp); 133 134 system("pause"); 135 sleep(200); 136 system("cls"); 137 break; 138 139 case 5: //排序 140 141 sort_class_number(&head); 142 143 system("pause"); 144 sleep(200); 145 system("cls"); 146 break; 147 148 case 6: //建立索引表并用其查找 149 150 get_indexes_list(head, str_depratment,index_list); 151 152 print_same_depart(index_list, str_depratment); 153 154 system("pause"); 155 sleep(200); 156 system("cls"); 157 break; 158 159 case 7: //exit() 160 161 printf("\a"); //are you want quit ? //warning you soon quit 162 exit_op= messagebox(null, "确认过眼神,是要退出的人 !! ", "挽留一下下可以吗?", mb_yesno); 163 if (6 == exit_op) 164 { 165 exit(-1); 166 } 167 printf("\t 您已取消退出 请继续使用 *_* \n\n"); 168 169 system("pause"); 170 sleep(200); 171 system("cls"); 172 break; 173 174 case 8: //打印现有的 175 176 print(head); 177 178 system("pause"); 179 180 sleep(200); 181 system("cls"); 182 break; 183 case 9: 184 185 printf("\a people always have choicse。。。。\n"); 186 printf("\n but always the wrong choice \n"); 187 printf("\n cherish what you have at the moment\n"); 188 printf("\n"); 189 sleep(2000); 190 191 system("cls"); 192 193 while (1) 194 { 195 static int t = 0; 196 if (t == 8) 197 { 198 t = 0; 199 } 200 char str[20]; 201 sprintf(str, "color %d%d", t, t + 1); 202 if (0 == t % 2) 203 { 204 system("title . 让你乱点 哈哈哈 "); 205 } 206 else 207 { 208 system("title . 下次记得做个乖宝宝 !!! "); 209 } 210 system(str); 211 printf("%s\n","且行且珍惜"); 212 ++t; 213 //sleep(150); 214 } 215 216 system("pause"); 217 218 sleep(200); 219 system("cls"); 220 break; 221 222 default: 223 //please input select operation number of you 224 printf("\a !!!!请输入 1 - 6 的数字来选择你的操作!!!!!\n"); 225 sleep(500); 226 //tips : refresh after on 3 seconds ,again try input 227 printf("\a *_* 提示: 2 秒后刷新 您可再次输入选项 *_* \n"); 228 229 sleep(2000); 230 system("cls"); 231 break; 232 } 233 } 234 return 0; 235 }
3.删除一个信息:(delete_cr_node.c)
1 #include"my_head.h" 2 3 extern card_number; 4 5 cr_node* delete_cr_crde(cr_node * head,long long number) 6 { 7 cr_node *t1, *t2; 8 t1 = head; 9 t2 = head; 10 if (number == head->number) 11 { 12 t2 = head; 13 head = head->next_node; 14 free(t2); 15 return head; 16 } 17 while (t1 != null && t1->number != number) 18 { 19 t2 = t1; 20 t1 = t1->next_node; 21 } 22 if (null == t1) 23 { 24 printf("\n **** !!!!!!! key unexistent of in library_card_system !!!!!!! ****\n\n"); 25 goto exception; 26 } 27 if (number == t1->number) 28 { 29 t2->next_node = t1->next_node; 30 card_number--; 31 free(t1); 32 33 return head; 34 } 35 36 37 38 exception: return head; 39 40 }
4.查找:(find.c)
1 #include"my_head.h" 2 3 extern card_number; 4 5 cr_node * find(cr_node * head, long long number) 6 { 7 if (null == head) 8 { 9 return null; 10 } 11 else 12 { 13 while (head->number != number) 14 { 15 head = head->next_node; 16 } 17 if (null == head) 18 { 19 return null; 20 } 21 else 22 { 23 return head; 24 } 25 } 26 27 }
5.获取排序后的顺序:(get_indexes_list.c)
1 #include "my_head.h" 2 extern card_number; 3 cr_node *make_indexes_list(cr_node *head, char _str_depratment[]) 4 { 5 cr_node *p1 = null; 6 cr_node *p2 = head; 7 if (null == head) 8 { 9 printf("输入为空 请检查 \n"); 10 return null; 11 } 12 while (head) 13 { 14 if (strstr(head->department, _str_depratment)) 15 { 16 p1 = head; 17 p2 = p1; 18 head = head->next_node; 19 break; 20 } 21 else 22 { 23 head = head->next_node; 24 } 25 } 26 while (head) 27 { 28 if (strstr(head->department, _str_depratment)) 29 { 30 p2->same_department_next_node = head; 31 p2 = p2->same_department_next_node; 32 head = head->next_node; 33 } 34 else 35 { 36 head = head->next_node; 37 } 38 } 39 if (null == p1) 40 { 41 printf("提示:有未录入的系 如有需要请添加 \n"); 42 return null; 43 } 44 p2->same_department_next_node = null; 45 return p1; 46 } 47 cr_node*get_indexes_list(cr_node*head, char*department[], 48 cr_node *index_list[depratment_number]) 49 { 50 int i = 0; 51 while (i < depratment_number) 52 { 53 index_list[i] = make_indexes_list(head, department[i]); 54 ++i; 55 } 56 return index_list; 57 }
6.初始化系统中的信息:(initialize.c)//这步可不执行 不影响使用
1 #include"my_head.h" 2 3 extern card_number; 4 5 cr_node* initialize(cr_node** head) 6 { 7 cr_node *p1=null, *p2 = null, *p3 = null; 8 9 p1 = (cr_node*)malloc(sizeof(cr_node)); 10 p2 = (cr_node*)malloc(sizeof(cr_node)); 11 p3 = (cr_node*)malloc(sizeof(cr_node)); 12 13 p1->name = "张三"; 14 p1->number = 1003; 15 p1->class_number = 2003; 16 p1->department = "计算机系"; 17 p1->next_node = p2; 18 p1->same_department_next_node = null; 19 p1->is_sort = 0; 20 card_number++; 21 22 p2->name = "王五"; 23 p2->number = 1001; 24 p2->class_number = 2002; 25 p2->department = "外语系"; 26 p2->next_node = p3; 27 p2->same_department_next_node = null; 28 p2->is_sort = 0; 29 card_number++; 30 31 p3->name = "李四"; 32 p3->number = 1002; 33 p3->class_number = 2001; 34 p3->department = "艺术系"; 35 p3->next_node = null; 36 p3->same_department_next_node = null; 37 p3->is_sort = 0; 38 card_number++; 39 40 *head = p1; 41 }
7. 初始化链表的顺序:(init_sort.c)//博主的排序实现使用了和往常不一样的方法 有兴趣的可以好好研究哦
1 #include"my_head.h" 2 3 4 int initialize_sort(cr_node *head) 5 { 6 while (head) 7 { 8 head->is_sort = 0; 9 head = head->next_node; 10 } 11 return 1; 12 }
8.录入一个信息的实现:(input_one.c):
1 #include"my_head.h" 2 3 extern card_number; 4 5 int input(cr_node *head) 6 { 7 cr_node *p1 = null; 8 9 char str[sizeof(cr_node)]; 10 11 static long long number;2 12 char *name = (char *)malloc(10); 13 char *department = (char *)malloc(64); 14 static long long class_number; 15 16 p1 = (cr_node *)malloc(sizeof(cr_node)); 17 p1->next_node = null; 18 19 while (head->next_node) 20 { 21 head = head->next_node; 22 } 23 24 getchar(); 25 26 printf("请输入需要录入的信息 \n\n(输入格式提示 \ 27 例如:[卡号 姓名 系别 班号] \ 28 中括号里面的为一次录入,不用写中括号。 )\n\n"); 29 30 gets_s(str, sizeof(cr_node)); 31 32 sscanf(str,"%lld %s %s %lld", 33 &number, name, 34 department,&class_number); 35 36 p1->number = number; 37 p1->name= name; 38 p1->department = department; 39 p1->class_number = class_number; 40 p1->is_sort = 0; 41 p1->same_department_next_node = null; 42 43 printf("你刚刚的输入是:\n"); 44 printf("card_number \tname \tdepartment \tclass_number\n\n\a"); 45 46 printf("%lld\t\t%s \t%s \t\t%lld\n\n", 47 p1->number, p1->name, 48 p1->department, p1->class_number); 49 50 head->next_node=p1; 51 card_number++; 52 53 system("pause"); 54 return 0; 55 }
9.打印信息的实现: //由于要求打印实现了很多 有兴趣的朋友可以用回调改一改 看起来会好一点
(print_all.c):
1 #include"my_head.h" 2 3 extern card_number; 4 5 int print(cr_node* head) 6 { 7 printf("当前系统共存储 %d 个借书证信息,所有信息如下:\n\n", card_number); 8 printf("card_number \tname \tdepartment \tclass_number\n\n\a"); 9 while (null!=head) 10 { 11 printf(" %lld\t\t%s \t%s \t%lld\n\n", 12 head->number, head->name, 13 head->department, head->class_number); 14 head = head->next_node; 15 } 16 return 0; 17 }
(print_all_same_depart.c):
1 #include"my_head.h" 2 3 extern card_number; 4 5 int print_same_depart(cr_node *index_list[depratment_number], char *str_depratment[depratment_number]) 6 { 7 cr_node *list_head = null; 8 9 int i = 0; 10 11 while (i < depratment_number) 12 { 13 list_head = index_list[i]; 14 15 16 if (null == list_head) 17 { 18 printf("-->%s未录入 :\n",str_depratment[i]); 19 20 ++i; 21 break; 22 } 23 printf("-->%s有如下 :\n", str_depratment[i]); 24 printf("ncard_number \tname \tdepartment \tclass_number\n\n\a"); 25 while (null != list_head) 26 { 27 28 printf(" %lld\t\t%s \t%s \t\t%lld\n\n", 29 list_head->number, list_head->name, 30 list_head->department, list_head->class_number); 31 32 list_head = list_head->same_department_next_node; 33 34 } 35 ++i; 36 } 37 38 39 return 0; 40 }
(print_on_same_department.c):
1 #include"my_head.h" 2 3 extern card_number; 4 5 int print_on_same_depart(cr_node *same_depaet_head) 6 { 7 printf("ncard_number \tname \tdepartment \tclass_number\n\n\a"); 8 while (null != same_depaet_head) 9 { 10 printf(" %lld\t\t%s \t%s \t\t%lld\n\n", 11 same_depaet_head->number, same_depaet_head->name, 12 same_depaet_head->department, same_depaet_head->class_number); 13 same_depaet_head = same_depaet_head->same_department_next_node; 14 } 15 return 0; 16 }
(print_one.c):
1 #include"my_head.h" 2 3 extern card_number; 4 5 int print_one(cr_node* head) 6 { 7 printf("card_number \tname \tdepartment \tclass_number\n\n\a"); 8 9 printf(" %lld\t\t%s \t%s \t%lld\n\n", 10 head->number, head->name, 11 head->department, head->class_number); 12 return 0; 13 }
10.两个排序的实现:(分别按 班号 卡号 的排序)
(sort_class_number.c):
1 #include"my_head.h" 2 3 static cr_node **array_save = null; 4 5 int get_class_sort_list(cr_node *head) 6 { 7 8 int total = 0; 9 10 int x_save = 0; 11 12 int flag=0; 13 14 cr_node *temp_total = head; 15 16 cr_node *temp_2 = head; 17 18 cr_node *min_node = null; 19 cr_node *current_node = null; 20 21 while (temp_total) 22 { 23 temp_total = temp_total->next_node; 24 ++total; 25 } 26 27 flag = total; 28 29 array_save = (cr_node **)malloc(sizeof(cr_node*)*total); 30 31 for(int j=0;j<total;++j) 32 { 33 min_node = null; 34 current_node = head; 35 while (!min_node) 36 { 37 if (flag < 0) 38 { 39 goto end; 40 } 41 if (temp_2->is_sort == 0) 42 { 43 min_node = temp_2; 44 } 45 else 46 { 47 temp_2 = temp_2->next_node; 48 } 49 } 50 51 52 for (int i=0; i < total; ++i) 53 { 54 55 if (min_node->class_number > current_node->class_number&&0==current_node->is_sort) 56 { 57 min_node = current_node; 58 current_node = current_node->next_node; 59 } 60 else 61 { 62 current_node = current_node->next_node; 63 } 64 } 65 min_node->is_sort = 1; 66 *(array_save+ x_save)= min_node; 67 68 ++x_save; 69 --flag; 70 71 72 } 73 74 75 end: return total; 76 77 78 } 79 80 81 int sort_class_number(cr_node **head) 82 { 83 printf(" 按班号排序前 : \n"); 84 print(*head); 85 86 initialize_sort(*head); 87 88 int total= get_class_sort_list(*head); 89 90 sort_class_link(head, total); 91 92 printf(" 按班号排序后 : \n"); 93 94 print(*head); 95 } 96 97 98 int sort_class_link(cr_node **head, int total) 99 { 100 int flag__=0; 101 102 cr_node *p1 = null; 103 cr_node *p2 = *head; 104 cr_node **p3 = null; 105 cr_node **p4 = null; 106 107 p1 = p2; 108 109 while (p1 != null && p2 != null) 110 { 111 p1 = p2; 112 p2 = p1->next_node; 113 114 p1->next_node = null; 115 116 } 117 118 *head = array_save[0]; 119 120 p3 = array_save; 121 122 123 while (flag__<total-1) 124 { 125 //p4 = &(*p3)->next_node; 126 127 (*p3)->next_node = *(p3+1); 128 ++p3; 129 ++flag__; 130 } 131 132 133 134 }
(sort_crad_number.c):
1 #include"my_head.h" 2 3 static cr_node **array_save = null; 4 5 int get_crad_number_sort_list(cr_node *head) 6 { 7 8 int total = 0; 9 10 int x_save = 0; 11 12 int flag = 0; 13 14 cr_node *temp_total = head; 15 16 cr_node *temp_2 = head; 17 18 cr_node *min_node = null; 19 cr_node *current_node = null; 20 21 while (temp_total) 22 { 23 temp_total = temp_total->next_node; 24 ++total; 25 } 26 27 flag = total; 28 29 array_save = (cr_node **)malloc(sizeof(cr_node*)*total); 30 31 for (int j = 0; j < total; ++j) 32 { 33 min_node = null; 34 current_node = head; 35 while (!min_node) 36 { 37 if (flag < 0) 38 { 39 goto end; 40 } 41 if (temp_2->is_sort == 0) 42 { 43 min_node = temp_2; 44 } 45 else 46 { 47 temp_2 = temp_2->next_node; 48 } 49 } 50 51 52 for (int i = 0; i < total; ++i) 53 { 54 55 if (min_node->number > current_node->number && 0 == current_node->is_sort) 56 { 57 min_node = current_node; 58 current_node = current_node->next_node; 59 } 60 else 61 { 62 current_node = current_node->next_node; 63 } 64 } 65 min_node->is_sort = 1; 66 *(array_save + x_save) = min_node; 67 68 ++x_save; 69 --flag; 70 71 72 } 73 74 75 end: return total; 76 77 } 78 79 80 int sort_card_link(cr_node **head, int total) 81 { 82 int flag__ = 0; 83 84 cr_node *p1 = null; 85 cr_node *p2 = *head; 86 cr_node **p3 = null; 87 cr_node **p4 = null; 88 89 p1 = p2; 90 91 while (p1 != null && p2 != null) 92 { 93 p1 = p2; 94 p2 = p1->next_node; 95 96 p1->next_node = null; 97 98 } 99 100 *head = array_save[0]; 101 102 p3 = array_save; 103 104 while (flag__ < total - 1) 105 { 106 107 (*p3)->next_node = *(p3 + 1); 108 ++p3; 109 ++flag__; 110 } 111 112 } 113 114 int sort_crad_number(cr_node **head) 115 { 116 printf(" 按卡号排序前 : \n"); 117 print(*head); 118 119 initialize_sort(*head); 120 121 int total = get_crad_number_sort_list(*head); 122 123 sort_card_link(head, total); 124 125 printf(" 按卡号排序后 : \n"); 126 127 print(*head); 128 }
结语:有问题欢迎提在下方 ,本人在校学生,时间较为充裕, 有时间会回复的。
/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10325628.html */
上一篇: struts2 18拦截器详解(十一)
下一篇: struts2 18拦截器详解(七)