要求:
请编写一个高校学生信息查询系统,学生类别包括研究生和本科生两类,要求用继承的方法构造两个类,学生信息主要包括姓名,学号,专业,校园卡号等,要求通过输入姓名,学号或校园卡号可以查询到相关学生的简单信息。
C++代码:
#include <iostream>
#include <fstream>
#include "stdlib.h"
#define MAX 10000
using namespace std;
class person //基本类
{
public:
int Xnum; //学号
char name[20]; //姓名
char sex[4]; //性别
int Unum; //校园卡号
};
/********** 本科生类 *************/
class Bstudent :virtual public person
{
public:
int credit; //已修学分
char special[20]; //专业
void Input()
{
cout << "请输入学号:"; cin >> Xnum;
cout << "请输入姓名:"; cin >> name;
cout << "请输入性别:"; cin >> sex;
cout << "请输入校园卡号:"; cin >> Unum;
cout << "请输入已修学分:"; cin >> credit;
cout << "请输入专业:"; cin >> special;
}
void Output()
{
cout << "学号: " << Xnum << " " << "姓名: " << name << " " << "性别: " << sex << " " << "校园卡号: " << Unum <<
" " << "已修学分" << credit << " " << "专业: " << special << endl;
}
};
Bstudent Bstu[MAX];
static int Bstutop;
/********** 本科生功能类 *************/
class BstuManager
{
public:
int Add(); //添加
int Search(); //查询
void Show(); //显示
void Edit(); //编辑
int Delete(); //删除
void Save(); //保存
void Read(); //读取
};
int BstuManager::Add() //添加
{
Bstudent t;
char choose;
choose = 'y';
do {
if (Bstutop == MAX)
{
cout << "人数已满" << endl;
return 0;
}
t.Input(); Bstu[Bstutop] = t; Bstutop++;
cout << "添加成功!" << endl;
cout << "是否继续添加?(y/n)";
cin >> choose;
} while (choose == 'y' || choose == 'Y');
return 1;
}
int BstuManager::Search() // 查询
{
char s[20];
int j, n, method;
char choose;
choose = 'y';
do {
method = 0;
cout << "请输入查询方式:1.学号,2.校园卡号,3.姓名" << endl;
cin >> method;
if (method == 1) {
cout << "请输入学号:"; cin >> n;
for (j = 0; j < Bstutop; j++)
{
if (n == Bstu[j].Xnum) break;
}
if (j == Bstutop)
cout << "没有此人!" << endl;
else
Bstu[j].Output();
}
if (method == 2) {
cout << "请输入校园卡号:"; cin >> n;
for (j = 0; j < Bstutop; j++)
{
if (n == Bstu[j].Unum) break;
}
if (j == Bstutop)
cout << "没有此人!" << endl;
else
Bstu[j].Output();
}
if (method == 3) {
cout << "请输入姓名:"; cin >> s;
for (j = 0; j < Bstutop; j++) {
if (!strcmp(s, Bstu[j].name))
Bstu[j].Output();
}//姓名存在重复,故循环输出
}
cout << "是否继续查询?(y/n)";
cin >> choose;
} while (choose == 'y' || choose == 'Y');
return 1;
}
void BstuManager::Show() //显示
{
int i;
if (Bstutop == 0)
{
cout << "记录为空!" << endl; return;
}
for (i = 0; i < Bstutop; i++)
Bstu[i].Output();
}
void BstuManager::Edit() //编辑
{
Bstudent t1;
int j, n;
char choose;
choose = 'y';
do {
cout << "请输入要编辑的人的学号:"; cin >> n;
for (j = 0; j < Bstutop; j++)
{
if (n == Bstu[j].Xnum) break;
}
if (j == Bstutop)
{
cout << "没有此人!" << endl; return;
};
cout << "输入修改后的信息:" << endl;
t1.Input();
Bstu[j] = t1;
cout << "编辑成功!" << endl;
cout << "是否继续编辑?(y/n)";
cin >> choose;
} while (choose == 'y' || choose == 'Y');
}
int BstuManager::Delete() //删除
{
int j, n;
cout << "请输入要删除的人的学号:"; cin >> n;
for (j = 0; j < Bstutop; j++)
{
if (n == Bstu[j].Xnum) break;
}
if (j == Bstutop)
{
cout << "没有此人!" << endl; return 0;
};
for (j; j < Bstutop; j++)
{
Bstu[j] = Bstu[j + 1];
}
Bstutop--;
cout << "删除成功!" << endl;
return 1;
}
void BstuManager::Save() //保存
{
int i;
ofstream outfile, outfile1;
outfile1.open("Bstutop.txt", ios::out);
outfile1 << Bstutop;
outfile.open("Bstu_data.txt", ios::binary);
if (!outfile)
{
cerr << "open error!" << endl; return;
}
for (i = 0; i < Bstutop; i++)
outfile.write((char*)&Bstu[i], sizeof(Bstu[i]));
outfile.close();
cout << "保存成功!" << endl;
}
void BstuManager::Read() //读取
{
int i;
ifstream infile, infile1;
infile1.open("Bstutop.txt", ios::in);
infile1 >> Bstutop;
infile.open("Bstu_data.txt", ios::binary);
if (!infile)
{
cerr << "open error!" << endl; return;
}
for (i = 0; i < Bstutop; i++)
infile.read((char*)&Bstu[i], sizeof(Bstu[i]));
infile.close();
cout << "读取成功!" << endl;
}
void Bstu_mune(BstuManager BM) //本科生菜单
{
int b; b = 0;
do {
cout << "本科生管理" << endl;
cout << "==========================================================================================" << endl;
cout << "| 1.添加 ";
cout << "2.查询 ";
cout << "3.显示 ";
cout << "4.编辑 ";
cout << "5.删除 ";
cout << "6.统计 ";
cout << "7.保存 ";
cout << "8.读取 ";
cout << "0.退出 |" << endl;
cout << "==========================================================================================" << endl;
cout << "请选择:"; cin >> b;
switch (b)
{
case 1:BM.Add(); break;
case 2:BM.Search(); break;
case 3:BM.Show(); break;
case 4:BM.Edit(); break;
case 5:BM.Delete(); break;
case 6:cout << "共有本科生人数:" << Bstutop << endl; break;
case 7:BM.Save(); break;
case 8:BM.Read(); break;
default:cout << "\n error" << endl; break;
case 0:break;
}
system("pause");
system("cls");
} while (b != 0);
}
/********** 研究生类 *************/
class Gstudent :virtual public person
{
public:
int credit; //已修学分
char testroom[40]; //所在实验室
char special[20]; //专业
void Input()
{
cout << "请输入学号:"; cin >> Xnum;
cout << "请输入姓名:"; cin >> name;
cout << "请输入性别:"; cin >> sex;
cout << "请输入校园卡号:"; cin >> Unum;
cout << "请输入已修学分:"; cin >> credit;
cout << "请输入所在实验室:"; cin >> testroom;
cout << "请输入专业:"; cin >> special;
}
void Output()
{
cout << "学号: " << Xnum << " " << "姓名: " << name << " " << "性别: " << sex << " " << "校园卡号: " << Unum <<
" " << "已修学分" << credit << " " << "所在实验室: " << testroom << " " << "专业: " << special << endl;
}
};
Gstudent Gstu[MAX];
static int Gstutop;
/********** 研究生功能类 *************/
class GstuManager
{
public:
int Add();
int Search();
void Show();
void Edit();
int Delete();
void Save();
void Read();
};
int GstuManager::Add() //添加
{
Gstudent t;
char s;
s = 'y';
do {
if (Gstutop == MAX)
{
cout << "人数已满" << endl;
return 0;
}
t.Input();
Gstu[Gstutop] = t;
Gstutop++;
cout << "添加成功!" << endl;
cout << "是否继续添加?(y/n)";
cin >> s;
} while (s == 'y' || s == 'Y');
return 1;
}
int GstuManager::Search() //查询
{
char s[20];
int j, n, method;
char choose;
choose = 'y';
do {
method = 0;
cout << "请输入查询方式:1.学号,2.校园卡号,3.姓名" << endl;
cin >> method;
if (method == 1) {
cout << "请输入学号:"; cin >> n;
for (j = 0; j < Gstutop; j++)
{
if (n == Gstu[j].Xnum) break;
}
if (j == Gstutop)
cout << "没有此人!" << endl;
else
Gstu[j].Output();
}
if (method == 2) {
cout << "请输入校园卡号:"; cin >> n;
for (j = 0; j < Gstutop; j++)
{
if (n == Gstu[j].Unum) break;
}
if (j == Gstutop)
cout << "没有此人!" << endl;
else
Gstu[j].Output();
}
if (method == 3) {
cout << "请输入姓名:"; cin >> s;
for (j = 0; j < Gstutop; j++) {
if (!strcmp(s, Gstu[j].name))
Gstu[j].Output();
}
}
cout << "是否继续查询?(y/n)";
cin >> choose;
} while (choose == 'y' || choose == 'Y');
return 1;
}
void GstuManager::Show() //显示
{
int i;
if (Gstutop == 0)
{
cout << "记录为空!" << endl; return;
}
for (i = 0; i < Gstutop; i++)
Gstu[i].Output();
}
void GstuManager::Edit() //编辑
{
Gstudent t1;
int j, n;
char choose;
choose = 'y';
do {
cout << "请输入要编辑的人的学号:"; cin >> n;
for (j = 0; j < Gstutop; j++)
{
if (n == Gstu[j].Xnum) break;
}
if (j == Gstutop)
{
cout << "没有此人!" << endl; return;
};
cout << "输入修改后的信息:" << endl;
t1.Input();
Gstu[j] = t1;
cout << "编辑成功!" << endl;
cout << "是否继续编辑?(y/n)";
cin >> choose;
} while (choose == 'y' || choose == 'Y');
}
int GstuManager::Delete() //删除
{
int j, n;
cout << "请输入要删除的人的学号:"; cin >> n;
for (j = 0; j < Gstutop; j++)
{
if (n == Gstu[j].Xnum) break;
}
if (j == Gstutop)
{
cout << "没有此人!" << endl; return 0;
};
for (j; j < Gstutop; j++)
{
Gstu[j] = Gstu[j + 1];
}
Gstutop--;
cout << "删除成功!" << endl;
return 1;
}
void GstuManager::Save() //保存
{
int i;
ofstream outfile, outfile1;
outfile1.open("Gstutop.dat", ios::out);
outfile1 << Gstutop;
outfile.open("Gstu_data.dat", ios::binary);
if (!outfile)
{
cerr << "open error!" << endl; return;
}
for (i = 0; i < Gstutop; i++)
outfile.write((char*)&Gstu[i], sizeof(Gstu[i]));
outfile.close();
cout << "保存成功!" << endl;
}
void GstuManager::Read() //读取
{
int i;
ifstream infile, infile1;
infile1.open("Testop.dat", ios::in);
infile1 >> Gstutop;
infile.open("Test_data.dat", ios::binary);
if (!infile)
{
cerr << "open error!" << endl; return;
}
for (i = 0; i < Gstutop; i++)
infile.read((char*)&Gstu[i], sizeof(Gstu[i]));
infile.close();
cout << "读取成功!" << endl;
}
void Gstu_mune(GstuManager GM) //研究生菜单
{
int b; b = 0;
do {
cout << "研究生管理" << endl;
cout << "==========================================================================================" << endl;
cout << "| 1.添加 ";
cout << "2.查询 ";
cout << "3.显示 ";
cout << "4.编辑 ";
cout << "5.删除 ";
cout << "6.统计 ";
cout << "7.保存 ";
cout << "8.读取 ";
cout << "0.退出 |" << endl;
cout << "==========================================================================================" << endl;
cout << "请选择:"; cin >> b;
switch (b)
{
case 1:GM.Add(); break;
case 2:GM.Search(); break;
case 3:GM.Show(); break;
case 4:GM.Edit(); break;
case 5:GM.Delete(); break;
case 6:cout << "共有研究生人数:" << Gstutop << endl; break;
case 7:GM.Save(); break;
case 8:GM.Read(); break;
default:cout << "\n error" << endl; break;
case 0:break;
}
system("pause");
system("cls");
} while (b != 0);
}
int main()
{
BstuManager Tmer1;
GstuManager Tetmer;
int a = 1, m = 0;
cout << " *************************************";
cout << endl << " $ 高校学生管理系统 $" << endl;
cout << " *************************************";
cout << endl << "请输入密码:"; cin >> m;
if (m != 123456)
{
cout << "密码错误!" << endl;
system("pause");
return 0;
}
while (a)
{
system("cls");
cout << endl;
cout << " ****欢迎使用高校学生信息管理系统****" << endl;
cout << " =================================" << endl;
cout << " | 1.本科生管理 |" << endl;
cout << " | 2.研究生管理 |" << endl;
cout << " | 0.退出 |" << endl;
cout << " =================================" << endl;
cout << "请选择:"; cin >> a;
switch (a)
{
case 1:Bstu_mune(Tmer1); break;
case 2:Gstu_mune(Tetmer); break;
case 0:break;
default:cout << "\n error" << endl;
system("pause");
break;
}
}
cout << endl << "谢谢使用" << endl;
system("pause");
return 0;
}