本文实例为大家分享了C++实现学生宿舍管理系统的具体代码,供大家参考,具体内容如下
非常简易,完成个作业够用,仅供初学者参考,不喜勿喷。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
/*先用结构体定义学生信息*/
struct stud
{
int num; //学号
char name[10]; //姓名
char sex[8]; //性别
int age; //年龄
char major[18]; //专业
int grade; //年级
int classes; //班级
int building; //楼号
int floor; //楼层
int dormitary; //宿舍号
struct stud *next; /*next是指向本结构体的类型的指针类型*/
};
struct stud *h; /*定义结构体的指针变量*/
int N=sizeof(struct stud);//获取结构体总长度,用于分配内存空间
/*输入模块: 建立链表*/
void input_message()
{ struct stud *p1,*p2;
int i,k;
system("cls");
printf("********** 您当前正在进行的操作是,输入学生信息 **********\n\n");
printf("请输入本次注册的生总数:");scanf("%d",&k);
h=NULL;
printf("\n您共有%d个学生信息需要录入\n",k);
if(k>0)
{
h=p2=p1=(struct stud*)malloc(N); /*为head,p2,p1申请存储空间*/
for(i=1;i<=k;i++)
{
printf("\n请输入第%d名学生的信息:\n\n",i);
p1=(struct stud*)malloc(N);
printf("学号:");
scanf("%d",&p1->num);
printf("姓名:");
scanf("%s",p1->name);
printf("性别(男或女):");
scanf("%s",p1->sex);
printf("年龄:");
scanf("%d",&p1->age);
printf("专业:");
scanf("%s",p1->major);
printf("年级:");
scanf("%d",&p1->grade);
printf("班级:");
scanf("%d",&p1->classes);
printf("楼号:");
scanf("%d",&p1->building);
printf("楼层:");
scanf("%d",&p1->floor);
printf("宿舍号:");
scanf("%d",&p1->dormitary);
p2->next=p1; /*将新结点连到表尾*/
p2=p1; /*p2指向新的表尾*/
}
p2->next=NULL;
h=h->next;
}
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("\n%*s%s\n",10," ","输入完成,按任意键进入主菜单!!!!");
getchar();
}
/*输出模块:显示链表*/
void output_message()
{
char v;
struct stud *p=h;
system("cls");
printf("********** 您当前正在进行的操作是,显示学生信息 **********\n\n");
if(p==NULL)
{
printf("%*s%s\n",10," ","当前没有找到任何学生信息,请您先输入信息,再执行本操作!\n");
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("%*s%s\n",10," ","按任意键返回主菜单!!!!");
getchar();
return;
}
printf(" 学号 姓名 性别 年龄 专业 年级 班级 楼号 楼层 宿舍号\n\n");
while(p!=NULL)
{
printf("%6d%10s%6s%6d%10s%6d%6d%6d%6d%8d\n",p->num,p->name,p->sex,p->age,p->major,p->grade,p->classes,p->building,p->floor,p->dormitary);
p=p->next;
}
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("\n%*s%s\n",10," ","输出完成,按任意键返回主菜单!!!!");
getchar();
}
/*插入新学生信息:插入链表*/
void insert_message()
{
struct stud *p0,*p1;
p1=(struct stud *)malloc(N);
system("cls");
printf("********** 您当前正在进行的操作是,插入学生信息 **********\n\n");
printf("\n请输入要插入学生的信息:\n\n");
p1=(struct stud*)malloc(N);
printf("学号:");
scanf("%d",&p1->num);
printf("姓名:");
scanf("%s",p1->name);
printf("性别(男或女):");
scanf("%s",p1->sex);
printf("年龄:");
scanf("%d",&p1->age);
printf("专业:");
scanf("%s",p1->major);
printf("年级:");
scanf("%d",&p1->grade);
printf("班级:");
scanf("%d",&p1->classes);
printf("楼号:");
scanf("%d",&p1->building);
printf("楼层:");
scanf("%d",&p1->floor);
printf("宿舍号:");
scanf("%d",&p1->dormitary);
p1->next=NULL;
if(h==NULL)//如果链表为空,插入的节点设为表头
{
h=p1;
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("\n%*s%s\n",10," ","插入成功! 按任意键返回主菜单!!!!");
getchar();
return;
}
p0=h;
while((p0->next)!=NULL&&(p0->next->num)<(p1->num))
{
p0=p0->next;
}
if(p0->next==NULL)
{
p0->next=p1; //如果到尾部了加到尾部
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("\n%*s%s\n",10," ","插入成功! 按任意键返回主菜单!!!!");
getchar();
}
else if((p0->next->num)==(p1->num))
{
free(p1); //如果已经存在这个学号,那就不能再插入了,释放
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("\n%*s%s\n",10," ","插入失败,本学号信息已经存在! 按任意键返回主菜单!!!!");
getchar();
}
else
{
//插入
p1->next=p0->next;
p0->next=p1;
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("\n%*s%s\n",10," ","插入成功! 按任意键返回主菜单!!!!");
getchar();
}
}
/*删除学生信息*/
void del_message()
{
int num;
bool delOk=false;
struct stud *p0,*p1;
system("cls");
printf("********** 您当前正在进行的操作是,删除学生信息 **********\n\n");
if(h==NULL)
{
printf("%*s%s\n",10," ","当前没有找到任何学生信息,请您先输入信息,再执行本操作!\n");
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
printf("%*s%s\n",10," ","按任意键返回主菜单!!!!");
getchar();
return;
}
printf("请输入要删除学生的学号:");
scanf("%d",&num);
p0=h;
if(p0->num==num)
{
h=p0->next;
free(p0); /*若首结点是所要删除的数据,则释放首结点*/
delOk=true;
}
else
{
p1=p0->next;
while(p1!=NULL)
{
if(p1->num==num)
{
p0->next=p1->next;
free(p1);
delOk=true;
break; //跳出循环
}
p0=p1;
p1=p1->next;
}
}
getchar();//吸收掉上一行录入时键入的回车符号,否则卡不住
if(delOk)
printf("%*s%s\n",10," ","删除成功! 按任意键返回主菜单!!!!");
else
printf("%*s%s\n",10," ","没有找到匹配学号,删除失败! 按任意键返回主菜单!!!!");
getchar();
}
//主函数
int main(void){
int choice;
do{
system("cls");
printf("\n\n\n");
printf("%*s%s",10," ","********** 学 生 宿 舍 管 理 系 统 **********\n\n");
printf("%*s%s",25," ","1.输入学生信息\n\n");
printf("%*s%s",25," ","2.输出学生信息\n\n");
printf("%*s%s",25," ","3.插入学生信息\n\n");
printf("%*s%s",25," ","4.删除学生信息\n\n");
printf("%*s%s",25," ","0.退出系统\n\n");
printf("%*s%s",10," ","*********************************************\n\n");
printf("%*s%s",10," ","请选择0-4之间任意整数:");
scanf("%d",&choice);
switch(choice)
{
case 1:input_message();break;
case 2:output_message();break;
case 3:insert_message();break;
case 4:del_message();break;
case 0:break;
}
}while(choice!=0);
}
可以用 Dev-C++ 进行编译调试