自己实现的如有缺漏欢迎提出
/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10325628.html */
设计内容:
设计一个排序和查找系统。能够实现对给定的一组学生的借书证信息(如:卡号、姓名、系别、班号等)进行排序和查找。
1)按照卡号顺序进行排序;
2)能够实现查找某个系的所有的借书卡号并输出。
设计要求:
(1)建立关于借书证信息结点的结构体;
(2)定义借书证信息的记录并录入基本信息;
(3)写出用某种排序算法(如冒泡排序)按关键字对记录进行排序的算法函数;
(4)对借书证信息的记录按系名建立索引查找结构;
(5)输入某个要查找的系名,用索引查找方法查找并输出该系的所有借书证信息。
是学期末的课程设计,自己弄完觉得还可以,提供给有需要的人做参考。
有图万岁:
下面是代码实现:
1.头文件:(my_head.h)
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<windows.h>
#include<stdlib.h> //这里改系别总个数
#define depratment_number 3 typedef int once_sort; typedef struct card_node { once_sort is_sort; long long number;
char *name;
char *department;
long long class_number;
struct card_node *next_node;
struct card_node *same_department_next_node;
}cr_node; cr_node* initialize(cr_node** head);
int initialize_sort(cr_node *head);
//
int print(cr_node* head); int print_one(cr_node*node); int print_on_same_depart(cr_node *same_depaet_head); int print_same_depart(cr_node *index_list[depratment_number], char *str_depratment[depratment_number]); //
int input(cr_node*head); //
cr_node * find(cr_node * head, long long number); //
cr_node* delete_cr_crde(cr_node * head, long long number); //
cr_node *make_indexes_list(cr_node *head, char _str_depratment[]); cr_node *get_indexes_list(cr_node *head, char *department[], cr_node *index_list[depratment_number]); //
int sort_class_number(cr_node **head);
int get_class_sort_list(cr_node *head);
int sort_class_link(cr_node **head, int total); int sort_crad_number(cr_node **head);
int get_crad_number_sort_list(cr_node *head);
int sort_card_link(cr_node **head, int total);
2.main函数入口:(source_1.c)
#include"my_head.h" int CARD_NUMBER = ; //point menu
void print_menu()
{
printf(" \n");
printf(" /-----------------------------------------------/\n");
printf(" / *** 借书证管理系统 ******/\n");
printf(" / -->选项 : ******/\n");
printf(" / *** 1.按卡号排序并输出前后结果 < ******/\n");
printf(" / *** 2.差找并输一个系的所有借书卡号 < ******/\n");
printf(" / *** 3.录入一个借书证 < ******/\n");
printf(" / *** 4.删除一个借书证 < ******/\n");
printf(" / *** 5.按关键字(例如 班号)进行排序 < ******/\n");
printf(" / *** 6.建立索引表并用其查找 < ******/\n");
printf(" / *** 7.退出本程序 < ******/\n");
printf(" / *** 8.打印现有的借书证信息 < ******/\n");
printf(" / *** 9.惊喜 最好不要选 < ******/\n");
printf(" /-----------------------------------------------/\n");
printf("\n \a #_# 请输入所要执行的操作 #_#\n");
printf("\n (请输入数字 1-8 选择操作)\n"); Sleep();
} int main()
{
system("color d"); system("title . #借书证信息管理系统# "); cr_node* head=NULL;
cr_node* temp_node = NULL;
cr_node *index_list[depratment_number]; cr_node *sort_list[];
int i = ; char str_find_depart_temp[]; long long temp = ;
int i_find_same = ; initialize(&head); //添加系别步骤 1.在下方字符串里添加系名 2。在头文件定义里更改系别总个数
char *str_depratment[depratment_number] = { "计算机系","外语系","艺术系" }; int operation;
int exit_op;
operation = ; while ()
{
print_menu();
operation = ;
printf("\n请输入你要进行的操作 (确认输入时可能有时需两次按下 Enter 键): \n"); scanf("%d",&operation); {getchar(); getchar(); } switch (operation)
{
case : //排序 sort_crad_number(&head); system("pause");
Sleep();
system("cls");
break; case : //查找一个系的全部成员并输出 get_indexes_list(head, str_depratment, index_list); printf("请输入你要查找的系的名称 注: 先有 如下系名记录在系统 \n"); gets_s(str_find_depart_temp,sizeof(str_find_depart_temp));
printf("%s\n", str_find_depart_temp);
if ("\\0" == str_find_depart_temp||"" == str_find_depart_temp)
{
printf("输入的系别有误 请重新输入\n");
goto END;
} {
int x=; while (x < depratment_number)
{
if (strstr(index_list[x]->department, str_find_depart_temp))
{
print_on_same_depart(index_list[x]);
goto END;
}
++x;
}
if (x > depratment_number)
{
printf("输入的系别有误 请重新输入\n");
goto END;
}
} END: system("pause");
Sleep();
system("cls");
break; case : //录入一个 input(head); system("pause");
Sleep();
system("cls");
break; case : //删除 printf("please input card_number : \n");
scanf("%lld", &temp); head = delete_cr_crde(head, temp); system("pause");
Sleep();
system("cls");
break; case : //排序 sort_class_number(&head); system("pause");
Sleep();
system("cls");
break; case : //建立索引表并用其查找 get_indexes_list(head, str_depratment,index_list); print_same_depart(index_list, str_depratment); system("pause");
Sleep();
system("cls");
break; case : //exit() printf("\a"); //are you want quit ? //warning you soon quit
exit_op= MessageBox(NULL, "确认过眼神,是要退出的人 !! ", "挽留一下下可以吗?", MB_YESNO);
if ( == exit_op)
{
exit(-);
}
printf("\t 您已取消退出 请继续使用 *_* \n\n"); system("pause");
Sleep();
system("cls");
break; case : //打印现有的 print(head); system("pause"); Sleep();
system("cls");
break;
case : printf("\a People always have choicse。。。。\n");
printf("\n But always the wrong choice \n");
printf("\n Cherish what you have at the moment\n");
printf("\n");
Sleep(); system("cls"); while ()
{
static int t = ;
if (t == )
{
t = ;
}
char str[];
sprintf(str, "color %d%d", t, t + );
if ( == t % )
{
system("title . 让你乱点 哈哈哈 ");
}
else
{
system("title . 下次记得做个乖宝宝 !!! ");
}
system(str);
printf("%s\n","且行且珍惜");
++t;
//Sleep(150);
} system("pause"); Sleep();
system("cls");
break; default:
//please input select operation number of you
printf("\a !!!!请输入 1 - 6 的数字来选择你的操作!!!!!\n");
Sleep();
//tips : refresh after on 3 seconds ,again try input
printf("\a *_* 提示: 2 秒后刷新 您可再次输入选项 *_* \n"); Sleep();
system("cls");
break;
}
}
return ;
}
3.删除一个信息:(delete_cr_node.c)
#include"my_head.h" extern CARD_NUMBER; cr_node* delete_cr_crde(cr_node * head,long long number)
{
cr_node *t1, *t2;
t1 = head;
t2 = head;
if (number == head->number)
{
t2 = head;
head = head->next_node;
free(t2);
return head;
}
while (t1 != NULL && t1->number != number)
{
t2 = t1;
t1 = t1->next_node;
}
if (NULL == t1)
{
printf("\n **** !!!!!!! key unexistent of in library_card_system !!!!!!! ****\n\n");
goto Exception;
}
if (number == t1->number)
{
t2->next_node = t1->next_node;
CARD_NUMBER--;
free(t1); return head;
} Exception: return head; }
4.查找:(find.c)
#include"my_head.h" extern CARD_NUMBER; cr_node * find(cr_node * head, long long number)
{
if (NULL == head)
{
return NULL;
}
else
{
while (head->number != number)
{
head = head->next_node;
}
if (NULL == head)
{
return NULL;
}
else
{
return head;
}
} }
5.获取排序后的顺序:(get_indexes_list.c)
#include "my_head.h"
extern CARD_NUMBER;
cr_node *make_indexes_list(cr_node *head, char _str_depratment[])
{
cr_node *p1 = NULL;
cr_node *p2 = head;
if (NULL == head)
{
printf("输入为空 请检查 \n");
return NULL;
}
while (head)
{
if (strstr(head->department, _str_depratment))
{
p1 = head;
p2 = p1;
head = head->next_node;
break;
}
else
{
head = head->next_node;
}
}
while (head)
{
if (strstr(head->department, _str_depratment))
{
p2->same_department_next_node = head;
p2 = p2->same_department_next_node;
head = head->next_node;
}
else
{
head = head->next_node;
}
}
if (NULL == p1)
{
printf("提示:有未录入的系 如有需要请添加 \n");
return NULL;
}
p2->same_department_next_node = NULL;
return p1;
}
cr_node*get_indexes_list(cr_node*head, char*department[],
cr_node *index_list[depratment_number])
{
int i = ;
while (i < depratment_number)
{
index_list[i] = make_indexes_list(head, department[i]);
++i;
}
return index_list;
}
6.初始化系统中的信息:(initialize.c)//这步可不执行 不影响使用
#include"my_head.h" extern CARD_NUMBER; cr_node* initialize(cr_node** head)
{
cr_node *p1=NULL, *p2 = NULL, *p3 = NULL; p1 = (cr_node*)malloc(sizeof(cr_node));
p2 = (cr_node*)malloc(sizeof(cr_node));
p3 = (cr_node*)malloc(sizeof(cr_node)); p1->name = "张三";
p1->number = ;
p1->class_number = ;
p1->department = "计算机系";
p1->next_node = p2;
p1->same_department_next_node = NULL;
p1->is_sort = ;
CARD_NUMBER++; p2->name = "王五";
p2->number = ;
p2->class_number = ;
p2->department = "外语系";
p2->next_node = p3;
p2->same_department_next_node = NULL;
p2->is_sort = ;
CARD_NUMBER++; p3->name = "李四";
p3->number = ;
p3->class_number = ;
p3->department = "艺术系";
p3->next_node = NULL;
p3->same_department_next_node = NULL;
p3->is_sort = ;
CARD_NUMBER++; *head = p1;
}
7. 初始化链表的顺序:(init_sort.c)//博主的排序实现使用了和往常不一样的方法 有兴趣的可以好好研究哦
#include"my_head.h" int initialize_sort(cr_node *head)
{
while (head)
{
head->is_sort = ;
head = head->next_node;
}
return ;
}
8.录入一个信息的实现:(input_one.c):
#include"my_head.h" extern CARD_NUMBER; int input(cr_node *head)
{
cr_node *p1 = NULL; char str[sizeof(cr_node)]; static long long number;
char *name = (char *)malloc();
char *department = (char *)malloc();
static long long class_number; p1 = (cr_node *)malloc(sizeof(cr_node));
p1->next_node = NULL; while (head->next_node)
{
head = head->next_node;
} getchar(); printf("请输入需要录入的信息 \n\n(输入格式提示 \
例如:[卡号 姓名 系别 班号] \
中括号里面的为一次录入,不用写中括号。 )\n\n"); gets_s(str, sizeof(cr_node)); sscanf(str,"%lld %s %s %lld",
&number, name,
department,&class_number); p1->number = number;
p1->name= name;
p1->department = department;
p1->class_number = class_number;
p1->is_sort = ;
p1->same_department_next_node = NULL; printf("你刚刚的输入是:\n");
printf("card_number \tname \tdepartment \tclass_number\n\n\a"); printf("%lld\t\t%s \t%s \t\t%lld\n\n",
p1->number, p1->name,
p1->department, p1->class_number); head->next_node=p1;
CARD_NUMBER++; system("pause");
return ;
}
9.打印信息的实现: //由于要求打印实现了很多 有兴趣的朋友可以用回调改一改 看起来会好一点
(print_all.c):
#include"my_head.h" extern CARD_NUMBER; int print(cr_node* head)
{
printf("当前系统共存储 %d 个借书证信息,所有信息如下:\n\n", CARD_NUMBER);
printf("card_number \tname \tdepartment \tclass_number\n\n\a");
while (NULL!=head)
{
printf(" %lld\t\t%s \t%s \t%lld\n\n",
head->number, head->name,
head->department, head->class_number);
head = head->next_node;
}
return ;
}
(print_all_same_depart.c):
#include"my_head.h" extern CARD_NUMBER; int print_same_depart(cr_node *index_list[depratment_number], char *str_depratment[depratment_number])
{
cr_node *list_head = NULL; int i = ; while (i < depratment_number)
{
list_head = index_list[i]; if (NULL == list_head)
{
printf("-->%s未录入 :\n",str_depratment[i]); ++i;
break;
}
printf("-->%s有如下 :\n", str_depratment[i]);
printf("ncard_number \tname \tdepartment \tclass_number\n\n\a");
while (NULL != list_head)
{ printf(" %lld\t\t%s \t%s \t\t%lld\n\n",
list_head->number, list_head->name,
list_head->department, list_head->class_number); list_head = list_head->same_department_next_node; }
++i;
} return ;
}
(print_on_same_department.c):
#include"my_head.h" extern CARD_NUMBER; int print_on_same_depart(cr_node *same_depaet_head)
{
printf("ncard_number \tname \tdepartment \tclass_number\n\n\a");
while (NULL != same_depaet_head)
{
printf(" %lld\t\t%s \t%s \t\t%lld\n\n",
same_depaet_head->number, same_depaet_head->name,
same_depaet_head->department, same_depaet_head->class_number);
same_depaet_head = same_depaet_head->same_department_next_node;
}
return ;
}
(print_one.c):
#include"my_head.h" extern CARD_NUMBER; int print_one(cr_node* head)
{
printf("card_number \tname \tdepartment \tclass_number\n\n\a"); printf(" %lld\t\t%s \t%s \t%lld\n\n",
head->number, head->name,
head->department, head->class_number);
return ;
}
10.两个排序的实现:(分别按 班号 卡号 的排序)
(sort_class_number.c):
#include"my_head.h" static cr_node **array_save = NULL; int get_class_sort_list(cr_node *head)
{ int total = ; int x_save = ; int flag=; cr_node *temp_total = head; cr_node *temp_2 = head; cr_node *min_node = NULL;
cr_node *current_node = NULL; while (temp_total)
{
temp_total = temp_total->next_node;
++total;
} flag = total; array_save = (cr_node **)malloc(sizeof(cr_node*)*total); for(int j=;j<total;++j)
{
min_node = NULL;
current_node = head;
while (!min_node)
{
if (flag < )
{
goto END;
}
if (temp_2->is_sort == )
{
min_node = temp_2;
}
else
{
temp_2 = temp_2->next_node;
}
} for (int i=; i < total; ++i)
{ if (min_node->class_number > current_node->class_number&&==current_node->is_sort)
{
min_node = current_node;
current_node = current_node->next_node;
}
else
{
current_node = current_node->next_node;
}
}
min_node->is_sort = ;
*(array_save+ x_save)= min_node; ++x_save;
--flag; } END: return total; } int sort_class_number(cr_node **head)
{
printf(" 按班号排序前 : \n");
print(*head); initialize_sort(*head); int total= get_class_sort_list(*head); sort_class_link(head, total); printf(" 按班号排序后 : \n"); print(*head);
} int sort_class_link(cr_node **head, int total)
{
int flag__=; cr_node *p1 = NULL;
cr_node *p2 = *head;
cr_node **p3 = NULL;
cr_node **p4 = NULL; p1 = p2; while (p1 != NULL && p2 != NULL)
{
p1 = p2;
p2 = p1->next_node; p1->next_node = NULL; } *head = array_save[]; p3 = array_save; while (flag__<total-)
{
//p4 = &(*p3)->next_node; (*p3)->next_node = *(p3+);
++p3;
++flag__;
} }
(sort_crad_number.c):
#include"my_head.h" static cr_node **array_save = NULL; int get_crad_number_sort_list(cr_node *head)
{ int total = ; int x_save = ; int flag = ; cr_node *temp_total = head; cr_node *temp_2 = head; cr_node *min_node = NULL;
cr_node *current_node = NULL; while (temp_total)
{
temp_total = temp_total->next_node;
++total;
} flag = total; array_save = (cr_node **)malloc(sizeof(cr_node*)*total); for (int j = ; j < total; ++j)
{
min_node = NULL;
current_node = head;
while (!min_node)
{
if (flag < )
{
goto END;
}
if (temp_2->is_sort == )
{
min_node = temp_2;
}
else
{
temp_2 = temp_2->next_node;
}
} for (int i = ; i < total; ++i)
{ if (min_node->number > current_node->number && == current_node->is_sort)
{
min_node = current_node;
current_node = current_node->next_node;
}
else
{
current_node = current_node->next_node;
}
}
min_node->is_sort = ;
*(array_save + x_save) = min_node; ++x_save;
--flag; } END: return total; } int sort_card_link(cr_node **head, int total)
{
int flag__ = ; cr_node *p1 = NULL;
cr_node *p2 = *head;
cr_node **p3 = NULL;
cr_node **p4 = NULL; p1 = p2; while (p1 != NULL && p2 != NULL)
{
p1 = p2;
p2 = p1->next_node; p1->next_node = NULL; } *head = array_save[]; p3 = array_save; while (flag__ < total - )
{ (*p3)->next_node = *(p3 + );
++p3;
++flag__;
} } int sort_crad_number(cr_node **head)
{
printf(" 按卡号排序前 : \n");
print(*head); initialize_sort(*head); int total = get_crad_number_sort_list(*head); sort_card_link(head, total); printf(" 按卡号排序后 : \n"); print(*head);
}
结语:有问题欢迎提在下方 ,本人在校学生,时间较为充裕, 有时间会回复的。
/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10325628.html */