案例描述:
在学校毕业设计项目中,每名老师带领2个学生,设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放2名学生的数组作为成员,学生的结构体中有姓名、考试分数,系统中需要实现的功能如下:
-
添加学生信息:向学生管理系统中添加新人,信息包括(老师姓名、学生姓名、学生分数)最多记录1000人
-
显示排序后学生信息:对学生管理系统中的学生进行排序后显示学生信息(包括学生姓名和成绩)
-
删除学生信息:按照姓名进行删除学生信息
-
删除老师信息:按照姓名进行删除老师信息
-
修改学生信息:按照姓名重新修改指定学生信息
-
查找学生信息:按照姓名查看指定学生信息
-
查找老师信息:按照姓名查看指定老师信息
-
清空联系人:清空通讯录中所有信息
-
退出通讯录:退出当前使用的通讯录
头文件函数:
#define MAX 1000 //最大人数
#include<iostream>
#include<string>
using namespace std;
struct Student
{
//成员列表
string name; //姓名
int score; //分数
int flag;
};
struct Teacher
{
//成员列表
string name; //姓名
struct Student stu[3];
};
struct Studentbooks
{
struct Teacher teacher[MAX]; //通讯录中保存的联系人数组
int m_Size; //通讯录中人员个数
};
void showMenu();
void inputstudent(Studentbooks *studentbooks);
void sortoutput(Studentbooks studentbooks);
void deletestudent(Studentbooks *studentbooks);
void deleteteacher(Studentbooks *studentbooks);
void findstudent(Studentbooks *studentbooks);
void findteacher(Studentbooks *studentbooks);
void modifystudent(Studentbooks *studentbooks);
void clean(Studentbooks *studentbooks);
main函数:
#include "struct1.h"
int main()
{
int select = 0;
//创建通讯录
Studentbooks studentbooks;
//初始化通讯录中人数
studentbooks.m_Size = 0;
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //添加学生信息
inputstudent( &studentbooks );
break;
case 2: //显示排序后学生信息
sortoutput( studentbooks );
break;
case 3: //删除学生信息
deletestudent( &studentbooks );
break;
case 4: //删除老师信息
deleteteacher( &studentbooks );
break;
case 5: //修改学生信息
modifystudent(&studentbooks);
break;
case 6: //查询学生信息
findstudent( &studentbooks );
break;
case 7: //查询老师信息
findteacher( &studentbooks );
break;
case 8: //清空学生管理系统
cleanPerson( &studentbooks );
break;
case 0: //退出学生管理系统
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
showMenu.cpp
#include "struct1.h"
void showMenu()
{
cout << "***********************************" << endl;
cout << "***** 1、添加学生信息 *****" << endl;
cout << "***** 2、显示排序后学生信息 *****" << endl;
cout << "***** 3、删除学生信息 *****" << endl;
cout << "***** 4、删除老师信息 *****" << endl;
cout << "***** 5、修改学生信息 *****" << endl;
cout << "***** 6、查询学生信息 *****" << endl;
cout << "***** 7、查询老师信息 *****" << endl;
cout << "***** 8、清空学生管理系统 *****" << endl;
cout << "***** 0、退出学生信息管理系统*****" << endl;
cout << "***********************************" << endl;
}
inputstudent.cpp
注:用地址传递可以在子函数中更改结构体数组的内容。
#include "struct1.h"
void inputstudent(Studentbooks *studentbooks)
{
if (studentbooks->m_Size == MAX)
{
cout << "通讯录已满,无法添加" << endl;
return;
}
else
{
cout <<"请输入老师的名字"<<endl;
cin >> studentbooks->teacher[studentbooks->m_Size].name;
for(int j = 0; j < 2; j++)
{
cout <<"请输入该老师所带第" << j+1 <<"个学生的名字"<<endl;
cin >> studentbooks->teacher[studentbooks->m_Size].stu[j].name;
cout <<"请输入该老师所带第"<< j+1 <<"个学生的分数"<<endl;
cin >> studentbooks->teacher[studentbooks->m_Size].stu[j].score;
studentbooks->teacher[studentbooks->m_Size].stu[j].flag =1;
//标志位,用于后面控制删除该学生的操作,为1时输出,为0是不输出(即删除)
}
studentbooks->m_Size++;
}
system("pause");
system("cls");
}
sortoutput.cpp
注:调用函数使用值传递,是为了避免在对学生成绩进行排序时更改原先结构体数组中的信息。
#include "struct1.h"
void sortoutput(Studentbooks studentbooks)
{
//二维数组排序嵌套两层循环
for(int i=0; i<studentbooks.m_Size; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<studentbooks.m_Size; k++)
{
for(int l=0; l<2; l++)
{
if(studentbooks.teacher[i].stu[j].score > studentbooks.teacher[k].stu[l].score)
{
int temp = 0; //交换分数
temp = studentbooks.teacher[i].stu[j].score;
studentbooks.teacher[i].stu[j].score = studentbooks.teacher[k].stu[l].score;
studentbooks.teacher[k].stu[l].score = temp;
string sname; //交换名字
sname = studentbooks.teacher[i].stu[j].name;
studentbooks.teacher[i].stu[j].name = studentbooks.teacher[k].stu[l].name;
studentbooks.teacher[k].stu[l].name = sname;
int temp1 = 0; //交换标志位用于控制输出
temp1 = studentbooks.teacher[i].stu[j].flag;
studentbooks.teacher[i].stu[j].flag = studentbooks.teacher[k].stu[l].flag;
studentbooks.teacher[k].stu[l].flag = temp1;
}
}
}
}
}
for(int i = 0; i < studentbooks.m_Size ; i++)
{
for(int j = 0; j < 2; j++)
{
if(studentbooks.teacher[i].stu[j].flag == 1) //0表示该学生已删除不输出
{
cout << " \t学生姓名:" <<studentbooks.teacher[i].stu[j].name << "\t学生分数"<<studentbooks.teacher[i].stu[j].score << endl;
}
}
}
system("pause");
system("cls");
}
deletestudent.cpp
#include "struct1.h"
void deletestudent(Studentbooks *studentbooks)
{
cout << "请输入您要删除的学生" << endl;
string name;
cin >> name;
int deleteflag1 = 1;
for (int i = 0; i < studentbooks->m_Size; i++)
{
for(int j = 0; j <2; j++)
{
if (studentbooks->teacher[i].stu[j].name == name)
{
if(j==0) //同一个老师的第一个学生,则把后一个学生的值赋值给第一个学生
//第一个学生被覆盖(即删除)
{
studentbooks->teacher[i].stu[j] = studentbooks->teacher[i].stu[j+1];
cout << "删除成功" << endl;
studentbooks->teacher[i].stu[j+1].flag = 0;
//更改标志位为0,则在排序输出函数中不输出,表示该学生已删除
deleteflag1 = 0;
}
if(j==1) //同一个老师的第二个学生,直接把该学生的标志位置0(不输出)表删除
{
studentbooks->teacher[i].stu[j].flag = 0;
//删除标志位置0,用于表示已经找到学生并删除
cout << "删除成功" << endl;
deleteflag1 = 0;
}
break;
}
}
}
if (deleteflag1 ==1) //删除标志位不变仍为1,表示未找到学生并删除
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
deleteteacher.cpp
#include "struct1.h"
void deleteteacher(Studentbooks *studentbooks)
{
cout << "请输入您要删除的老师" << endl;
string name;
cin >> name;
int deleteflag1 = 1;
for (int i = 0; i < studentbooks->m_Size; i++)
{
if (studentbooks->teacher[i].name == name)
//找到对应的老师姓名后,直接用后一位的信息进行覆盖(即删除当前老师信息)
{
for(int j = i; j <studentbooks->m_Size; j++)
{
studentbooks->teacher[i].name = studentbooks->teacher[i+1].name;
//覆盖老师姓名
for(int k = 0; k < 2; k++)
{
//覆盖学生信息
studentbooks->teacher[i].stu[k].name = studentbooks->teacher[i+1].stu[k].name;
studentbooks->teacher[i].stu[k].score = studentbooks->teacher[i+1].stu[k].score;
}
studentbooks->m_Size--;
cout << "删除成功" << endl;
deleteflag1 = 0;
}
}
}
if (deleteflag1 ==1) //删除标志位,同deletestudent.cpp
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
modifystudent.cpp
#include "struct1.h"
void modifystudent(Studentbooks *studentbooks)
{
cout << "请输入您要修改的学生" << endl;
string name;
cin >> name;
int modifyflag = 1;
for (int i = 0; i < studentbooks->m_Size; i++)
{
for(int j = 0; j <2; j++)
{
if (studentbooks->teacher[i].stu[j].name == name)
{
cout << "原来的学生信息"<< endl;
cout << " \t学生姓名:" <<studentbooks->teacher[i].stu[j].name << "\t学生分数"<<studentbooks->teacher[i].stu[j].score << "\t老师姓名"<<studentbooks->teacher[i].name << endl;
cout <<"请输入修改后的学生姓名"<<endl;
cin >> studentbooks->teacher[i].stu[j].name;
cout <<"请输入修改后的学生分数"<<endl;
cin >> studentbooks->teacher[i].stu[j].score;
cout << "修改成功" << endl;
modifyflag = 0;
}
}
}
if (modifyflag ==1)
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
findstudent.cpp
#include "struct1.h"
void findstudent(Studentbooks *studentbooks)
{
cout << "请输入您要查询的学生" << endl;
string name;
cin >> name;
int findflag = 1;
for (int i = 0; i < studentbooks->m_Size; i++)
{
for(int j = 0; j <2; j++)
{
if (studentbooks->teacher[i].stu[j].name == name)
{
cout << " \t学生姓名:" <<studentbooks->teacher[i].stu[j].name << "\t学生分数"<<studentbooks->teacher[i].stu[j].score << "\t老师姓名"<<studentbooks->teacher[i].name << endl;
findflag = 0;
}
}
}
if (findflag ==1)
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
findteacher.cpp
#include "struct1.h"
void findteacher(Studentbooks *studentbooks)
{
cout << "请输入您要查询的老师" << endl;
string name;
cin >> name;
int findflag = 1;
for (int i = 0; i < studentbooks->m_Size; i++)
{
if (studentbooks->teacher[i].name == name)
{
for(int j = 0 ;j <2 ; j++)
{
if(studentbooks->teacher[i].stu[j].flag == 1)
{
cout << "\t老师姓名" << studentbooks->teacher[i].name << " \t学生姓名:" <<studentbooks->teacher[i].stu[j].name << "\t学生分数"<<studentbooks->teacher[i].stu[j].score << endl;
findflag = 0;
}
}
}
}
if ( findflag ==1)
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
clean.cpp
#include "struct1.h"
void clean(Studentbooks *studentbooks)
{
studentbooks->m_Size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}