C语言实现学生宿舍信息管理系统课程设计
#include
#include
#include
#include
#define N 40 //线性表存储空间的初始分配量
#define increase 10 //线性表存储空间的分配量增量
int choice; //定义全局变量
typedef struct {
char name[20];
int num; //学号和房号都为整型
int room;
} stu;
stu stud;
typedef struct {
int length; //当前长度
stu *elem; //存储空间基址
int listsize; //当前分配的存储容量
} linklist;
//线性表初始化
void Init(linklist &L) {
L.length = 0;
L.elem = (stu *)malloc(N * sizeof(stu));
L.listsize = N;
}
//操作菜单
void Menu() {
printf( "**************************************
" );
printf( "*** 欢迎进入宿舍管理系统 ***
" );
printf( "**************************************
" );
printf( "* 1. 新建宿舍名单 *
" );
printf( "* 2. 排序宿舍信息 *
" );
printf( "* 3. 查询宿舍信息 *
" );
printf( "* 4. 插入宿舍信息 *
" );
printf( "* 5. 删除宿舍信息 *
" );
printf( "* 0. 退出系统 *
" );
printf( "**************************************
" );
printf("请输入菜单(0-5):");
scanf("%d", &choice);
if (choice<0 || choice>5) {
system("cls");
printf("输入数字不对,请重新!
");
printf("
");
Menu();
}
}
//打印学生信息
void Display(linklist &L) {
int i;
printf("姓名 学号 房号
");
for (i = 0; i printf("%-10s %-2d %5d ", L.elem[i].name, L.elem[i].num, L.elem[i].room); } //返回主界面 void Ret() { char c; fflush(stdin); printf(" "); printf("请按任意键进入主界面:"); scanf("%c", &c); system("cls"); } //创建学生信息表 void Create(linklist &L) { if (L.length >= L.listsize) { //判断学生的人数是否超过初值,如果超过,则重新分配 stu *newbase; newbase = (stu*)realloc(L.elem, (N + increase) * sizeof(stu)); L.elem = newbase; L.listsize += increase; } int i = 2; char ch; printf("********开始创建学生信息********** "); printf(" "); printf("请输入第1个学生的信息 "); printf("请输入姓名:"); fflush(stdin); // 清空输入缓冲区,得到正确的输入数据 gets(stud.name); //输入一行字符串(姓名) printf("请输入学号:"); scanf("%d", &stud.num); printf("请输入房号:"); scanf("%d", &stud.room); ch = getchar(); strcpy(L.elem[L.length].name, stud.name); L.elem[L.length].num = stud.num; L.elem[L.length].room = stud.room; L.length++; printf(" "); printf("是否继续输入? scanf("%c", &ch); printf(" "); while (ch == 'y') { printf("请输入第%d个学生的信息 ", i); printf("请输入姓名:"); fflush(stdin); // 清空输入缓冲区,得到正确的输入数据 gets(stud.name); //输入一行字符串(姓名) printf("请输入学号:"); scanf("%d", &stud.num); printf("请输入房号:"); scanf("%d", &stud.room); strcpy(L.elem[L.length].name, stud.name); L.elem[L.length].num = stud.num; L.elem[L.length].room = stud.room; i++; L.length=i-1; ch = getchar(); printf(" "); printf("是否继续输入? scanf("%c", &ch); printf(" "); } if (ch == 'n') system("cls"); } //按姓名排序(采用冒泡排序) void sort1(linklist &L) { int i, j; stu temp; for (i = 0; i for (j = 0; j if (strcmp(L.elem[j].name, L.elem[j+1].name)>0) { temp = L.elem[j]; L.elem[j] = L.elem[j+1]; L.elem[j+1] = temp; } } //按学号排序(采用折半插入排序) void sort2(linklist &L) { int i, j, mid, low, high; stu temp; for (i = 1; i < L.length; i++) { if(L.elem[i].num temp = L.elem[i]; low = 0; high = i-1; while (low <= high) { mid = (low + high) / 2; if (temp.num < L.elem[mid].num) high = mid - 1; else low = mid + 1; } for (j = i - 1; j >= high+1; j--) L.elem[j+1]=L.elem[j]; L.elem[high+1]=temp; } } } //按房号排序(采用简单选择排序) void sort3(linklist &L) { int i,j,k; stu temp; for(i=0; i k=i; for(j=i+1; j if(L.elem[j].room k=j; if(k!=i){ temp = L.elem[i]; L.elem[i] = L.elem[k]; L.elem[k] = temp; } } } //排序函数 void Sort(linklist &L) { int c; printf("请输入排序的方式(1:按名字排序,2:按学号排序,3:按房号排序):"); scanf("%d", &c); switch (c) { case 1: sort1(L); if (L.length == 0) { printf("已无学生记录! "); Ret(); Menu(); } else { printf("按姓名排序: "); Display(L); Ret(); //调用返回主界面 Menu(); } break; case 2: sort2(L); if (L.length == 0) { printf("已无学生记录! "); Ret(); Menu(); } else { printf("按学号排序: "); Display(L); Ret(); //调用返回主界面 Menu(); } break; case 3: sort3(L); if (L.length == 0) { printf("已无学生记录! "); Ret(); Menu(); } else { printf("按房号排序: "); Display(L); Ret(); //调用返回主界面 Menu(); } break; default: break; } } //选择是否继续查找 int Select() { char ch; scanf("%c", &ch); printf("是否继续查找? fflush(stdin); scanf("%c", &ch); if (ch == 'y') { system("cls"); return 1; } else return 0; } //按姓名从小到大查找(采用二分查找) void search1(linklist &L) { if (L.length == 0) { printf("已无学生记录! "); Ret(); Menu(); } else { int low = 0, high = L.length, mid, flag = 0; printf(" "); printf("按姓名查找-->请输入要查找的姓名:"); char a[15], ch; scanf("%s", a); while (low <= high) { mid = (low + high) / 2; if (strcmp(a, L.elem[mid].name) == 0) { flag = 1; break; } else if (strcmp(a, L.elem[mid].name)>0) low = mid + 1; else high = mid - 1; } if (flag == 1) { printf("查找成功-->该学生信息为: "); printf("姓名 学号 房号 "); printf("%-10s %-2d %-5d ", L.elem[mid].name, L.elem[mid].num, L.elem[mid].room); if (Select()) search1(L); else { system("cls"); Menu(); } } else { printf("该学生不存在!"); if (Select()) search1(L); else { system("cls"); Menu(); } } } } //按学号从小到大查找(采用二分查找) void search2(linklist &L) { if (L.length == 0) { printf(" "); printf("已无学生记录! "); Ret(); Menu(); } else { int low = 0, high = L.length, mid, flag = 0; int n; char ch; printf(" "); printf("按学号查找-->请输入要查找的学号:"); scanf("%d", &n); while (low <= high) { mid = (low + high) / 2; if (n == L.elem[mid].num) { flag = 1; break; } else if (n>L.elem[mid].num) low = mid + 1; else high = mid - 1; } if (flag == 1) { printf("查找成功----->该学生信息为: "); printf("姓名 学号 房号 "); printf("%-1s0 %-2d %-5d ", L.elem[mid].name, L.elem[mid].num, L.elem[mid].room); if (Select()) search2(L); else { system("cls"); Menu(); } } else { printf("该学生不存在!"); if (Select()) search2(L); else { system("cls"); Menu(); } } } } //按房号从小到大查找(采用二分查找) void search3(linklist &L) { if (L.length == 0) { //此函数功能为:返回主界面 printf(" "); printf("已无学生记录! "); Ret(); Menu(); } else { int low = 0, high = L.length, mid, flag = 0;//flag作为标志符,为1则表示查找成功,否则没有所要查找的学生 int m; char ch; printf(" "); printf("按房号查找-->请输入要查找的房号:"); scanf("%d", &m); while (low <= high) { mid = (low + high) / 2; if (m == L.elem[mid].room) { flag = 1; break; } else if (m>L.elem[mid].room) low = mid + 1; else high = mid - 1; } if (flag == 1) { printf("查找成功-->该学生信息为: "); printf("姓名 学号 房号 "); printf("%-10s %-2d %-5d ", L.elem[mid].name, L.elem[mid].num, L.elem[mid].room); if (Select()) //调用判断函数1 search3(L); else { system("cls"); Menu(); } } else { printf("该学生不存在!"); if (Select()) //调用判断函数2 search3(L); else { system("cls"); Menu(); } } } } //查找函数 void Search(linklist &L) { int c; printf("请输入查找的方式(1:按名字查找,2:按学号查找,3:按房号查找):"); scanf("%d", &c); switch (c) { case 1: sort1(L); search1(L); break;//先进行二分查找排序 case 2: sort2(L); search2(L); break; case 3: sort3(L); search3(L); break; default: break; } } //按学号从小到大插入该学生 void Insert(linklist &L) { int i, j, k; char ch; printf(" "); printf("插入的学生信息为: "); printf("姓名:"); fflush(stdin);// 清空输入缓冲区,得到正确的输入数据 gets(stud.name); printf("学号:"); scanf("%d", &stud.num); printf("房号:"); scanf("%d", &stud.room); if (L.length == 0) { strcpy(L.elem[L.length].name, stud.name); L.elem[L.length].num = stud.num; L.elem[L.length].room = stud.room; } for (i = 0; i if (stud.num k = i; for (j = L.length; j>k; j--) L.elem[j] = L.elem[j - 1]; strcpy(L.elem[k].name, stud.name); L.elem[k].num = stud.num; L.elem[k].room = stud.room; break; } else { strcpy(L.elem[L.length].name, stud.name); L.elem[L.length].num = stud.num; L.elem[L.length].room = stud.room; } } L.length++; fflush(stdin); printf(" "); printf("是否继续插入? scanf("%c", &ch); if (ch == 'y') Insert(L); else system("cls"); } //按学号删除该学生 void Delete(linklist &L) { int i, j, k = -1; char ch; printf(" "); printf(" "); printf("请输入要删除学生的学号:"); scanf("%d", &stud.num); for (i = 0; i if (stud.num == L.elem[i].num) { printf("该学生的信息为: "); printf("姓名:%s 学号:%d 房号:%d ", L.elem[i].name, L.elem[i].num, L.elem[i].room); k = i; for (j = k; j L.elem[j] = L.elem[j + 1]; printf("已成功删除 "); break; } } if (i >= L.length) printf("该学生不存在 "); if (k >= 0)L.length--; fflush(stdin); printf(" "); printf("是否继续删除操作? scanf("%c", &ch); system("cls"); if (ch == 'y') Delete(L); else system("cls"); } //主函数 int main() { linklist L; //定义线性表 L Init(L); Menu(); //调用主菜单函数 while (choice != 0) { system("cls"); switch (choice) { case 1: Create(L); //调用线性表创建函数 Menu(); break; case 2: Sort(L); break;//调用排序函数 case 3: Search(L); break;//调用查找函数进行(二分)查找 case 4: sort2(L); //调用学号排序函数 Insert(L); //按学号序列插入 system("cls"); printf("插入后的学生信息: "); Display(L); Ret(); Menu(); break; case 5: Delete(L); //调用删除函数 if (L.length == 0) { printf(" "); printf("学生记录已被删除完! "); Ret(); Menu(); } else { printf("显示删除后的学生信息: "); Display(L); Ret(); Menu(); } break; } } }