dormmanage.cpp
来自「一个很简单的功能实现」· C++ 代码 · 共 331 行
CPP
331 行
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
struct Student
{
string Name;//姓名
double ID;//学号
string SpecialInfo;//专业信息
string DormNum;//学生宿舍房间号
string OtherInfor;//学生的特别说明和信息 程序可以通过这个变量来操作货物的信息追加功能
Student *next;
};
//////////////////////////////////////////////////
void InitStudent(Student *first);//初始化学生;
string GetStudentKind();//得到学生的种类
void ShowKindInfo(Student *first,string Infor);//根据传过来的值查看该专业的全部学生
void ShowOneInfo(Student *p);
Student* FindOneInfo(Student *first,string Infor,double style);
bool DelStudentInfo(Student *first,double id);//删除学生信息.
void EditStudentInfo(Student *first,double id);//重新编辑学生信息
void Sort(Student *first);//进行排序(使用选择排序法)
void ShowAllStudent(Student *first);
///////////////////////////////////////////////////
void ManageDorm(Student* first);
void FindDorm(Student* first);
int AllStudentNum=0;
////////////////////////////////////////////////////
void main()
{
void ManageDorm(Student *first);
void FindDorm(Student *first);
Student *header=new Student;
header->next=NULL;
InitStudent(header);
if (header==NULL)
{
cout<<"初始化学生信息不成功";return;
}
Sort(header);
system("cls");
cout<<"世界真是奇妙,现在又要开始做数据结构的课程设计了\n\n\n";
loop: cout<<"\t\t\t宿舍人员管理系统"<<endl;
cout<<" 承办业务 1.查询平台 2、管理平台 3、退出使用\n";
string choice;
cin>>choice;
system("cls");
if(choice=="3"||choice=="2"||choice=="1")
{
if (choice=="1")
{
FindDorm(header); goto loop;//查找业务主要包含了三个方向
//a.按姓名查询 ;b.按学号查询 ;c按房号查询 另外增加按专业查询
}
if (choice=="2")
{
ManageDorm(header); goto loop;
}
if (choice=="3")
{
cout<<"欢迎使用谢谢";
return;
}
}
else
{
system("cls");
goto loop;
}
}
void FindDorm(Student* first)//查找功能
{
cout<<"\t\t\t查询功能"<<endl;
loop: cout<<" 查询类型 1.姓名 2、学号 3、宿舍号 4、专业 5、退出使用\n";
string choice,infor;
Student *p=NULL;
double Num;
cin>>choice;
system("cls");
if(choice=="1"||choice=="2"||choice=="3"||choice=="4"||choice=="5")
{
if (choice=="1")
{ cout<<"请输入学生姓名:";cin>>infor;
p=FindOneInfo(first,infor,0);
}
if (choice=="2")
{
cout<<"请输入学号:";cin>>Num;
p=FindOneInfo(first,"",Num);
}
if (choice=="3")
{ cout<<"请输入学生宿舍号:";cin>>infor;
p=FindOneInfo(first,infor,0);
}
if (choice=="4")
{
infor=GetStudentKind();
p=FindOneInfo(first,infor,0);
}
if (choice=="5")
{
system("cls");
return;
}
goto loop;
}
else
{
system("cls");
goto loop;
}
}
void ManageDorm(Student* first)//对宿舍信息的管理
{
cout<<"\t\t\t宿舍信息管理系统"<<endl;
loop: cout<<" 功能区 1.添加信息 2、删除信息 3、修改信息 4、退出使用\n";
string choice,infor;cin>>choice;
double Num;
if(choice=="1"||choice=="2"||choice=="3"||choice=="4")
{
if (choice=="1")
{
InitStudent(first);
goto loop;
}
if (choice=="2")
{
cout<<"请输入学生学号 ";cin>>Num;
DelStudentInfo(first,Num); goto loop;
}
if (choice=="3")
{
cout<<"请输入学生学号 ";cin>>Num;
EditStudentInfo(first,Num); goto loop;
}
if (choice=="4")
{
Sort(first);
cout<<"欢迎使用谢谢";
return;
}
}
else
{
system("cls");
goto loop;
}
}
void InitStudent(Student *first)//初始化学生链表(同时还可以在添加学生信息的函数中使用到)
{
string name;
cout<<"请输入学生姓名(输入0推出添加)";cin>>name;
while (name!="0")//输入0退出添加
{
Student *p=new Student();
p->Name=name;
cout<<"学号 ";cin>>p->ID;
cout<<"学生所在宿舍号 ";cin>>p->DormNum;
p->SpecialInfo=GetStudentKind();
cout<<"您还可以输入学生的其他信息";cin>>p->OtherInfor;
AllStudentNum++;
p->next=first->next;
first->next=p;
system("cls");
cout<<"学生姓名";
cin>>name;
}
Sort(first);
system("cls");
}
string GetStudentKind()//得到学生所在的专业
{
int kind=0;
cout<<"本宿舍楼所有专业 1·计算机技术 2·软件工程 3·机械自动化 4·经济管理";
cin>>kind;
switch (kind)
{
case 1:
return "计算机技术";
case 2:
return "软件工程";
case 3:
return "机械自动化";
case 4:
return "经济管理";
}
return "其他专业";
}
void ShowAllStudent(Student *first)
{
Student *p=new Student;
p=first->next;
for(int i=0;i<AllStudentNum;i++)
{
ShowOneInfo(p);
p=p->next;
}
}
void ShowKindInfo(Student *first,string KindInfo)
{
Student *p=new Student();
p=first->next;
while (p!=NULL)
{
if (p->SpecialInfo==KindInfo)
{
ShowOneInfo(p);
}
p=p->next;
}
}
void ShowOneInfo(Student *p)
{
cout<<"学生专业:"<<p->SpecialInfo<<endl;
cout<<"\t姓名 "<<p->Name<<"\t 学号"<<p->ID<<"所在房间号 "<<p->DormNum<<"其他相关信息"<<p->OtherInfor<<endl;
}
Student* FindOneInfo(Student *first,string Infor,double style=0)//该函数主要用与根据学生姓名,学号,宿舍号查询(由于这三个元素实际上是不可能一样的所以可以写成一个函数)
{
Student *p=first->next;
while (p!=NULL)
{
if (p->Name==Infor||p->DormNum==Infor||p->ID==style)
{
ShowOneInfo(p);
if (style!=0&&p->ID==style)//第一种方式是根据学号查询结果是唯一的,此style起到了2种作用
{
return p;
}
}
p=p->next;
}
return NULL;
}
bool DelStudentInfo(Student *first,double id)//根据学号进行删除,因为学号是唯一的
{
Student *p=first->next,*q=first;
while (p!=NULL)
{
if (p->ID==id)
{
ShowOneInfo(p);
string choice;
cout<<"是否从学生信息中删除该学生(y/n)";cin>>choice;
if (choice=="Y"||choice=="y")
{
q->next=p->next;
delete p;
return true;
}
return false;
}
q=p;
p=p->next;
}
return false;
}
void EditStudentInfo(Student *first,double id)//利用了学号的唯一性
{
Student *p=NULL;
p=FindOneInfo(first,"",id);
int kind=0;
loop: cout<<"学生信息 1·姓名 2·学号 3·专业 4·房间号 5·其他信息 (输入0退出编辑)";
cin>>kind;
while (kind!=0)
{
switch (kind)
{
case 1:
cout<<"请输入新的学生姓名 ";cin>>p->Name;break;
case 2:
cout<<"输入学生学号 ";cin>>p->ID;break;
case 3:
p->SpecialInfo=GetStudentKind();break;
case 4:
cout<<"房间号";cin>>p->DormNum;break;
case 6:
cout<<"学生的其他信息";cin>>p->OtherInfor;break;
}
goto loop;
}
}
void Sort(Student *first)//使用选择排序法进行排序
{
double AllStudentsID[100];
int small=0;
double t;
Student *p=first->next;
Student *q=new Student;
q->next=NULL;
for(int i=0;p!=NULL;i++)
{
AllStudentsID[i]=p->ID;
p=p->next;
}
for(i=0;i<AllStudentNum;i++)//使用了选择法进行排序
{
small=i;
for(int j=i;j<AllStudentNum;j++)
{
if(AllStudentsID[j]<AllStudentsID[small])
small=j;
}
if(small!=i) //交换值
{t=AllStudentsID[i];AllStudentsID[i]=AllStudentsID[small];AllStudentsID[small]=t;}
}
for (i=0;i<AllStudentNum;i++)
{
p=first->next;
while (p!=NULL)
{
if (p->ID==AllStudentsID[i])
{
p->next=q->next;
q->next=p;
break;
}
p=p->next;
}
}
first=q;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?