问题描述
某高校学生的住宿安排比较灵活,一个学院有可能住在不同的宿舍楼,为方便学生管理者快速查找学生的住宿信息,设计一个小型的宿舍管理查询软件。其中学生信息包括:学号、姓名、学院、楼号、房间号。
一堆废话
很久没有更新博客了,小白刚刚历经考试周的打击,百废待兴,赶紧跑来CSDN和小伙伴们取暖安慰受伤的心灵。近期会陆续写四篇数据结构课设的博客,然后再写两篇编写小游戏的博客,一个是基于Python的飞机大战,另一个是基于C++并在funcode上完成的海底世界游戏。
数据描述
(1)首先定义一个结构体,用来存放某个学生的信息,包括学号、姓名、学院、楼号、房间号。
//定义学生信息结构体
typedef struct{
int number;
char name[20];
char college[50];
int building;
int room;
}Student;
(4)再定义一个顺序表,用具有20个元素顺序表来存放20个学生的信息。每个元素分别表示一个药品信息的结构体。
//定义存储顺序表
typedef struct {
Student s[20];
int length;
}SqList;
程序代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定义学生信息结构体
typedef struct{
int number;
char name[20];
char college[50];
int building;
int room;
}Student;
//定义存储顺序表
typedef struct {
Student s[20];
int length;
}SqList;
void CreatStudent(SqList &L,int n)//输入学生信息
{
L.length=0;
for(int i=1;i<=n;i++)
{
printf("输入第%d个学生信息\n",i);
printf ("学号:");
scanf("%d",&L.s[i].number);
printf ("姓名:");
scanf("%s",&L.s[i].name);
printf ("学院:");
scanf("%s",&L.s[i].college);
printf ("楼号:");
scanf("%d",&L.s[i].building);
printf ("房间号:");
scanf("%d",&L.s[i].room);
L.length++;
}
FILE *fp;
fp=fopen("room.txt","w+");
for(int i=1;i<=L.length;i++)
{
fprintf(fp,"%d %s %s %d %d\n",L.s[i].number,L.s[i].name,L.s[i].college,L.s[i].building,L.s[i].room);
}
printf("添加成功!\n");
fclose(fp);
}
void NumberSort(SqList &L)//按学号进行排序
{
int i=1;
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
for(i=2;i<=L.length;i++)
{
if(L.s[i].number<L.s[i-1].number)
{
L.s[0]=L.s[i];
L.s[i]=L.s[i-1];
int j;
for(j=i-2;L.s[0].number<L.s[j].number;j--)
{
L.s[j+1]=L.s[j];
}
L.s[j+1]=L.s[0];
}
}
printf("按学号排序为:\n");
for(int i=1;i<=L.length;i++)
{
printf("%d %s %s %d %d\n",L.s[i].number,L.s[i].name,L.s[i].college,L.s[i].building,L.s[i].room);
}
printf("排序成功!\n");
}
void NameSort(SqList &L)//按姓名排序
{
int i=1,j;
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
for(i=1;i<L.length;i++)
{
for(j=1;j<=L.length-i;j++)
{
if(strcmp(L.s[j].name,L.s[j+1].name)>=0)
{
L.s[0]=L.s[j];
L.s[j]=L.s[j+1];
L.s[j+1]=L.s[0];
}
}
}
printf("按姓名排序为:\n");
for(int i=1;i<=L.length;i++)
{
printf("学号:%d 姓名:%s 学院:%s 楼号:%d 房间号:%d\n",L.s[i].number,L.s[i].name,L.s[i].college,L.s[i].building,L.s[i].room);
}
printf("排序成功!\n");
} void BuildingSort(SqList &L)//按楼号进行排序
{
int i=1;
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
for(i=2;i<=L.length;i++)
{
if(L.s[i].building<L.s[i-1].building)
{
L.s[0]=L.s[i];
L.s[i]=L.s[i-1];
int j;
for(j=i-2;L.s[0].building<L.s[j].building;j--)
{
L.s[j+1]=L.s[j];
}
L.s[j+1]=L.s[0];
}
}
printf("按楼号排序为:\n");
for(int i=1;i<=L.length;i++)
{
printf("学号:%d 姓名:%s 学院:%s 楼号:%d 房间号:%d\n",L.s[i].number,L.s[i].name,L.s[i].college,L.s[i].building,L.s[i].room);
}
printf("排序成功!\n");
}
void NumberSearch(SqList &L) //按学号查询
{
int i=1,k,flag=0;
int num;
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
printf("输入要查询学生的学号:\n");
scanf("%d",&num);
for(i=1;i<=L.length;i++)
{
if(L.s[i].number==num)
{
k=i;
flag=1;
}
}
if(flag==0)
printf("没有找到该学生,查询失败!\n");
else
printf("学号:%d 姓名:%s 学院:%s 楼号:%d 房间号:%d\n",L.s[k].number,L.s[k].name,L.s[k].college,L.s[k].building,L.s[k].room);
}
void NameSearch(SqList &L)
{
int i=1,k,flag=0;
char na[20];
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
printf("输入要查询学生的姓名:\n");
scanf("%s",na);
for(i=1;i<=L.length;i++)
{
if(strcmp(L.s[i].name,na)==0)
{
k=i;
flag=1;
}
}
if(flag==0)
printf("没有找到该学生,查询失败!\n");
else
printf("学号:%d 姓名:%s 学院:%s 楼号:%d 房间号:%d\n",L.s[k].number,L.s[k].name,L.s[k].college,L.s[k].building,L.s[k].room);
}
void BuildingSearch(SqList &L)
{
int i=1,flag=0;
int bu;
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
printf("输入要查询学生的楼号:\n");
scanf("%d",&bu);
for(i=1;i<=L.length;i++)
{
if(L.s[i].building==bu)
{
printf("学号:%d 姓名:%s 学院:%s 楼号:%d 房间号:%d\n",L.s[i].number,L.s[i].name,L.s[i].college,L.s[i].building,L.s[i].room);
flag=1;
}
}
if(flag==0)
printf("没有找到该楼号,查询失败!\n");
}
void ShowRoom(SqList &L)
{
int i=1;
L.length=0;
FILE *fp1;
fp1=fopen("room.txt","r+");
while(!feof(fp1))
{
fscanf(fp1,"%d %s %s %d %d",&L.s[i].number,L.s[i].name,L.s[i].college,&L.s[i].building,&L.s[i].room);
L.length++;
i++;
}
L.length--;
fclose(fp1);
for(i=1;i<=L.length+1;i++)
{
printf("学号:%d 姓名:%s 学院:%s 楼号:%d 房间号:%d\n",L.s[i].number,L.s[i].name,L.s[i].college,L.s[i].building,L.s[i].room);
}
}
int main()
{
SqList L;
int n,option;
printf("********欢迎进入宿舍管理查询系统**************\n");
ShowRoom(L);
while(1)
{
printf("****************************************");
printf("\n1.创建\t2.按学号排序\n");
printf("\n3.按姓名排序\t4.按楼号排序\n");
printf("\n5.按学号查询\t6.按姓名查询\n");
printf("\n7.按楼号查询\t8.退出\n");
printf("****************************************");
printf("\n请选择需要的服务:");
scanf("%d",&option);
switch(option)
{
case 1:
printf("请输入学生个数:");
scanf("%d",&n);
CreatStudent(L,n);
break;
case 2:
NumberSort(L);
break;
case 3:
NameSort(L);
break;
case 4:
BuildingSort(L);
break;
case 5:
NumberSearch(L);
break;
case 6:
NameSearch(L);
break;
case 7:
BuildingSearch(L);
break;
case 8:
exit(0);
break;
}
}
return 0;
}
测试数据
(1)开始界面(显示文件信息和系统服务选项)
(2)按学生学号进行排序
(3)按销学生姓名进行排序
(4)按楼号进行排序
(5)按学号查询
(6)按姓名查询
(7)按楼号查询