资源描述
综合实验要求
编写一个小型学生信息管理系统,可以对中学生、大学生和研究生的信息进行简单管理.每一类学生都包含有学生的学生名、成绩1、成绩2、成绩3和平均成绩,其中平均成绩=(成绩1+成绩2+成绩3)/3。每一类学生还有区别与其他类学生的特殊信息,中学生有家长,大学生有专业,研究生有导师。实现以下功能:
(1) 输入学生的基本信息;
(2) 根据学生名查询某个学生的信息;
(3) 计算并显示某个学生的平均成绩;
一、系统分析
1、基本信息类的属性和操作
1) 属性
学生类别编号、学生名、成绩1、成绩2、成绩3、平均成绩
(为了方便信息的读取,程序中给每类学生设置了一个学生类别编号,以便区别各类学生)
2)操作
数据输入:输入学生名、成绩1、成绩2和成绩3;
数据输出:输出学生类别编号、姓名、成绩1、成绩2和成绩3;
计算平均成绩:平均成绩 =(成绩1+成绩2+成绩3)/3。
2、中学生类的属性和操作
1)属性
继承基本信息类的属性,并增加中学生类区别于其他学生类的特殊属性,即家长.
2)操作
数据输入:继承基本信息类的数据输入操作,并增加输入“家长”、信息的功能。
数据输出:继承基本信息类的数据输出操作,并增加输出“家长”、信息的功能。
3、大学生类的属性和操作
1)属性
继承基本信息类的属性,并增加大学生类区别于其他学生类的特殊属性,即专业。
2)操作
数据输入:继承基本信息类的数据输入操作,并增加输入“专业”、信息的功能。
数据输出:继承基本信息类的数据输出操作,并增加输出“专业"、信息的功能。
4、研究生属性和操作
1)属性
继承基本信息类的属性,并增加研究生类区别于其他学生类的特殊属性,即导师。
2)操作
数据输入:继承基本信息类的数据输入操作,并增加输入“导师”、信息的功能。
数据输出:继承基本信息类的数据输出操作,并增加输出“导师”、信息的功能。
5、系统管理类操作
系统管理类自成一个类:系统管理类。主要操作有:
1) 输入学生基本信息;
2) 根据学生姓名查询某个学生的信息;
3) 计算并显示某个学生的平均成绩。
二、系统设计
1、基类和派生类的设计
基类Record(基本信息类):
num(学生类别编号)、name(学生名)、score1(成绩1)、
score2(成绩2)、score3(成绩3)、average(平均成绩).
Student(中学生类):
从基类继承来的属性、patriarch(家长)。
U_student(大学生类):
从基类继承来的属性、major(专业)。
Graduate(研究生类):
从基类继承来的属性、mentor(导师)。
System(系统管理类):
成员函数In_information负责输入学生信息,成员函数Search查询学生信息,成员函数Out_average 计算并显示平均成绩,成员函数Interface负责界面输出.
2、系统管理类设计
(1)将数据文件信息读入内存对象数组
程序一启动,由System的构造函数自动调用函数readFile完成.
(2)信息的输入
成员函数In_information根据要输入学生的类别分别调用对应的学生信息输入功能函数完成输入.
(3)成员函数Search接收从键盘输入的学生类别和学生名,在对应的对象
数组中查找,找到后调用对象的成员函数Output显示学生信息。
(4)平均成绩计算和显示
接收从键盘输入的学生类别和姓名,然后查找对象数组,找到后调用对象的计算平均成绩函数计算平均成绩,然后显示。
三、系统实现
头文件:Record.h 、Student。h 、U_student 。h 、Graudate。h 、System。h
源文件:Record。cpp 、Student。 cpp 、U_student . cpp 、Graudate. cpp 、
System. cpp、main。cpp .
程序运行的主界面如图:
实验代码:
头文件部分:
//Record。h
#ifndef _RECORD_H_
#define_RECORD_H_
#include〈string>
#include〈vector>
usingnamespace std;
classSystem;
classRecord{
public:
Record(){}
Record(stringn,floats1,floats2,floats3,floatavg):name(n),score1(s1),score2(s2),score3(s3),average(avg){average=0;}
virtualvoid set_inf()=0;
virtualvoid display_inf()=0;
float caculate_avg();
friendclassSystem;
protected:
string name;
float score1;
float score2;
float score3;
float average;
};
#endif
//Student。h
#ifndef _STUDENT_H_
#define_STUDENT_H_
#include〈string〉
#include”Record。h"
usingnamespace std;
classSystem;
classStudent:publicRecord{
public:
Student() {}// { //set_inf(); }
Student(stringn,floats1,floats2,floats3,floatavg,stringpa):Record(n,s1,s2,s3,avg),patriarch(pa){}
virtualvoid set_inf();
virtualvoid display_inf();
friendclassSystem;
friendifstream& operator〉>(ifstream& i, Student& s);
friendofstream& operator〈〈(ofstream&i, Student& s);
protected:
string patriarch;
};
#endif
//U_student。h
#ifndef _U_STUDENT_H_
#define_U_STUDENT_H_
#include<string〉
#include”Record.h”
usingnamespace std;
classSystem;
classU_student:publicRecord{
public:
U_student(){}//{ //set_inf(); }
U_student(stringn,floats1,floats2,floats3,floatavg,stringma):Record(n,s1,s2,s3,avg),major(ma){}
virtualvoid set_inf();
virtualvoid display_inf();
friendclassSystem;
friendifstream& operator〉>(ifstream& i, U_student& s);
friendofstream& operator〈<(ofstream&i, U_student& s);
protected:
string major;
};
#endif
//Graduate.h
#ifndef _GRADUATE_H_
#define_GRADUATE_H_
#include〈string>
#include”Record.h”
usingnamespace std;
classSystem;
classGraduate:publicRecord{
public:
Graduate(){}//{ //set_inf(); }
Graduate(stringn,floats1,floats2,floats3,floatavg,stringme):Record(n,s1,s2,s3,avg),mentor(me){}
virtualvoid set_inf();
virtualvoid display_inf();
friendclassSystem;
friendifstream& operator〉>(ifstream& i, Graduate& s);
friendofstream& operator<〈(ofstream&i, Graduate& s);
protected:
string mentor;
};
#endif
//System.h
#ifndef _SYSTEM_H_
#define_SYSTEM_H_
classSystem{
public:
System();
void In_information();
void Search();
void Out_average();
void Interface();
void readfile();
void delete_inf();
void writefile();
};
#endif
//源文件部分
//Record.cpp
#include”Record。h”
#include〈iostream〉
usingnamespace std;
float Record::caculate_avg(){
return average=(score1+score2+score3)/3;
}
//Student。cpp
#include"Student.h"
#include<iostream〉
#include<fstream〉
usingnamespace std;
voidStudent::set_inf(){
cout〈<”姓名:"<〈endl;
cin>>name;
cout〈〈”成绩一:”〈〈endl;
cin〉〉score1;
cout<<”成绩二:"<〈endl;
cin〉〉score2;
cout〈〈”成绩三:”<<endl;
cin>>score3;
cout<<”家长:"<〈endl;
cin〉〉patriarch;
this-〉caculate_avg();
}
voidStudent::display_inf(){
cout〈<”姓名:"〈〈name〈〈endl;
cout〈〈”成绩一:”<〈score1〈〈endl;
cout〈〈”成绩二:"〈〈score2〈〈endl;
cout<<”成绩三:"〈〈score3〈〈endl;
cout〈<”家长:”〈<patriarch〈〈endl;
}
ifstream& operator〉〉(ifstream& infile, Student& s)
{
infile〉〉s。name〉〉s。score1 〉〉s。score2 〉〉s.score3>〉s。average>>s.patriarch;
returninfile;
}
ofstream& operator<〈(ofstream& outfile, Student& s)
{
outfile<〈s.name 〈<" "〈〈s。score1 <〈" ”〈〈s。score2 〈<" ”
〈〈s。score3 <〈" ”〈〈s。average 〈<” ”<<s.patriarch <〈 endl;
returnoutfile;
}
//U_student。cpp
#include”U_student.h”
#include〈iostream〉
#include〈fstream〉
usingnamespace std;
void U_student::set_inf(){
cout〈〈”姓名:"<〈endl;
cin>〉name;
cout〈〈”成绩一:”<〈endl;
cin〉>score1;
cout<〈"成绩二:”〈<endl;
cin〉〉score2;
cout<〈"成绩三:”<〈endl;
cin〉〉score3;
cout〈<"专业:”〈〈endl;
cin〉>major;
this—>caculate_avg();
}
void U_student::display_inf(){
cout〈〈"姓名:"〈〈name〈<endl;
cout〈〈”成绩一:”<<score1〈〈endl;
cout<<”成绩二:"〈<score2〈〈endl;
cout〈<"成绩三:”<〈score3〈<endl;
cout〈<”专业:"〈〈major〈〈endl;
}
ifstream& operator〉〉(ifstream& infile, U_student& s)
{
infile 〉〉 s.name >〉 s。score1 〉〉 s.score2 〉> s.score3 〉〉 s。average >〉 s。major;
return infile;
}
ofstream& operator〈〈(ofstream& outfile, U_student& s)
{
outfile 〈< s。name 〈〈 ” " 〈< s.score1 <〈 ” ” 〈〈 s。score2 <〈 " ”
〈〈 s。score3 〈〈 " ” 〈〈 s。average << ” ” 〈< s。major <〈 endl;
return outfile;
}
//Graduate.cpp
#include"Graduate.h”
#include<iostream〉
#include〈fstream>
usingnamespace std;
void Graduate::set_inf(){
cout〈〈"姓名:”<〈endl;
cin>〉name;
cout<〈"成绩一:"<〈endl;
cin>〉score1;
cout〈〈”成绩二:”〈<endl;
cin〉〉score2;
cout〈〈”成绩三:”<〈endl;
cin〉>score3;
cout〈<”导师:”〈〈endl;
cin〉〉mentor;
this->caculate_avg();
}
void Graduate::display_inf(){
cout〈〈”姓名:"<〈name<〈endl;
cout〈<"成绩一:"〈<score1〈<endl;
cout<〈”成绩二:”<<score2<<endl;
cout〈<”成绩三:”〈〈score3<<endl;
cout〈〈"导师:”<<mentor〈〈endl;
}
ifstream& operator>〉(ifstream& infile, Graduate& s)
{
infile >〉 s。name 〉> s.score1 〉> s。score2 >> s。score3 >> s。average >〉 s.mentor;
return infile;
}
ofstream& operator〈〈(ofstream& outfile, Graduate& s)
{
outfile <〈 s.name <〈 " ” <〈 s。score1 <〈 " " 〈〈 s。score2 << " ”
<〈 s.score3 <〈 " ” <〈 s.average 〈< ” ” 〈〈 s.mentor 〈〈 endl;
return outfile;
}
//System。cpp
#include”System。h"
#include"Record。h”
#include”Student.h"
#include”U_student.h”
#include”Graduate。h"
#include〈iostream>
#include〈fstream>
#include〈conio.h〉
#include〈iterator>
#include"shlwapi。h"
#pragmacomment(lib,”shlwapi。lib”)
#include<iomanip>
usingnamespace std;
vector〈Student> stu_m;
vector<U_student〉 stu_u;
vector<Graduate〉 stu_g;
System::System() {
system(”title 小型学生信息管理");
system(”color 0b");
this-〉readfile();
this—〉Interface();
}
void System::readfile() {
/*HANDLE hFILE1 = CreateFileA(”中学生信息。txt”, FILE_ATTRIBUTE_READONLY, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);*/
//*****
Student temp_stu_m;
if (!PathFileExistsA("中学生信息。txt”)) { CreateFileA(".//中学生信息.txt”, NULL, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); }
ifstream iofile1(”中学生信息.txt", ios::in | ios::out);
while (!iofile1。eof())
{
iofile1 >〉 temp_stu_m;
stu_m.push_back(temp_stu_m);
}
stu_m。pop_back();
iofile1.close();
//*****
U_student temp_stu_u;
/*HANDLE hFILE2 = CreateFileA("大学生信息。txt”, FILE_ATTRIBUTE_READONLY, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);*/
if (!PathFileExistsA(”大学生信息。txt”)) { CreateFileA("。//大学生信息.txt", NULL, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); }
ifstream iofile2(”大学生信息.txt”, ios::in | ios::out);
while (!iofile2。eof())
{
iofile2 〉〉 temp_stu_u;
stu_u。push_back(temp_stu_u);
}
stu_u。pop_back();
iofile2。close();
//******
Graduate temp_stu_g;
/*HANDLE hFILE3 = CreateFileA(”研究生信息。txt”, FILE_ATTRIBUTE_READONLY, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);*/
if (!PathFileExistsA(”研究生信息。txt")) { CreateFileA(”。//研究生信息。txt", NULL, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); }
ifstream iofile3("研究生信息。txt”, ios::in | ios::out);
while (!iofile3。eof())
{
iofile3 >〉 temp_stu_g;
stu_g。push_back(temp_stu_g);
}
stu_g。pop_back();
iofile3。close();
}
void System::writefile() {
//*****
ofstream iofile4(”中学生信息.txt”, ios::in | ios::out|ios::trunc);
if (!iofile4) {
cerr 〈< "open error!” 〈< endl;
system(”pause");
exit(0);
}
for (vector〈Student〉::iterator it = stu_m.begin(); it !=stu_m。end(); it++)
{
iofile4<<*it;
}
iofile4。close();
//*****
ofstream iofile5(”大学生信息。txt”, ios::in | ios::out |ios::trunc);
if (!iofile5) {
cerr 〈< ”open error!" 〈〈 endl;
system("pause”);
exit(0);
}
for (vector<U_student〉::iterator it = stu_u。begin(); it != stu_u。end(); it++)
{
iofile5 <〈 *it;
}
iofile5。close();
//******
ofstream iofile6("研究生信息。txt”, ios::in | ios::out | ios::trunc);
if (!iofile6) {
cerr << ”open error!" 〈< endl;
system(”pause");
exit(0);
}
for (vector<Graduate>::iterator it = stu_g。begin(); it != stu_g。end(); it++)
{
iofile6 〈< *it;
}
iofile6。close();
}
void System::In_information(){
int n;
cout<〈”输入要创建的学生类别:"<<endl;
cout<〈”1。中学生”〈<"2.大学生”〈<”3。研究生”〈<endl;
cin>>n;
if(n==1){
Student* p_s = new Student;
p_s—>set_inf();
stu_m。push_back(*(p_s));
}
elseif (n == 2) {
U_student* p_s = new U_student;
p_s—>set_inf();
stu_u.push_back(*(p_s));
}
elseif (n == 3) {
Graduate* p_s = new Graduate;
p_s-〉set_inf();
stu_g。push_back(*(p_s));
}
cout 〈< endl 〈< ”任意键返回” 〈〈 endl;
_getch();
}
void System::Search() {
string na;
int n;
cout 〈〈 ”请输入学生类别:” 〈〈 endl;
cout << ”1。中学生” <〈 "2.大学生" << "3。研究生" <〈 endl;
cin >〉 n;
cout <〈 "请输入学生的名字:" 〈〈 endl;
cin 〉〉 na;
if (n == 1) {
int i = 0;
for (vector〈Student〉::iterator it = stu_m.begin(); it 〈stu_m.end(); it++) {
if (it—〉name == na) { i = 1; it—>display_inf(); }
if (it == stu_m。end() — 1 && i== 0) { cout 〈〈 "没有信息!” 〈〈 endl; break; }
}
}
elseif (n == 2) {
int i = 0;
for (vector<U_student〉::iterator it = stu_u。begin(); it 〈stu_u。end(); it++) {
if (it—〉name == na) { i = 1; it-〉display_inf(); }
if (it == stu_u。end() — 1 && i==0) { cout <〈 ”没有信息!” 〈〈 endl; break; }
}
}
elseif (n == 3) {
int i = 0;
for (vector〈Graduate>::iterator it = stu_g.begin(); it 〈stu_g。end(); it++) {
if (it-〉name == na) { i = 1; it—〉display_inf(); }
if (it == stu_g。end()—1&&i==0){ cout 〈〈 ”没有信息!” 〈< endl; break; }
}
}
cout <〈 endl <〈 ”任意键返回" 〈< endl;
_getch();
}
void System::Out_average(){
string na;
int n;
cout 〈〈 ”请输入学生类别:” 〈〈 endl;
cout <〈 "1.中学生" 〈< "2.大学生” 〈< ”3.研究生” <〈 endl;
cin 〉〉 n;
cout << ”请输入学生的名字:” 〈〈 endl;
cin 〉〉 na;
if (n == 1) {
for (vector〈Student〉::iterator it = stu_m.begin(); it < stu_m。end(); it++) {
if (it-〉name == na)
{
cout 〈〈 ”姓名:” 〈〈 it-〉name <〈 endl;
cout <〈 ”平均成绩为”<〈it—〉caculate_avg() <〈 endl;
}
}
}
elseif (n == 2) {
for (vector<U_student>::iterator it = stu_u。begin(); it 〈 stu_u.end(); it++) {
if (it—〉name == na) {
cout <〈 ”姓名:” << it->name << endl;
cout 〈〈 ”平均成绩为” 〈< it—>caculate_avg() 〈< endl;
}
}
}
elseif (n == 3) {
for (vector<Graduate〉::iterator it = stu_g。begin(); it 〈 stu_g。end(); it++) {
if (it—>name == na) {
cout <〈 ”姓名:” << it-〉name << endl;
cout <〈 ”平均成绩为” 〈< it—>caculate_avg() 〈< endl;
}
}
}
cout 〈〈 endl 〈< "任意键返回” 〈< endl;
_getch();
}
void System::delete_inf() {
string na;
int n;
cout 〈〈 "请输入学生类别:” <〈 endl;
cout 〈〈 "1。中学生" 〈〈 ”2.大学生” 〈< ”3.研究生" << endl;
cin 〉〉 n;
cout 〈< "请输入学生的名字:" 〈〈 endl;
cin >〉 na;
if (n == 1) {
for (vector〈Student>::iterator it = stu_m.begin(); it < stu_m.end();) {
if (it—>name == na) { it = stu_m.erase(it); cout 〈< "删除成功!”<<endl; }
else { it++; }
}
}
elseif (n == 2) {
for (vector<U_student>::iterator it = stu_u.begin(); it 〈 stu_u。end();) {
if (it—〉name == na) { it = stu_u。erase(it); cout 〈< ”删除成功!” 〈〈 endl; }
else { it++; }
}
}
elseif (n == 3) {
for (vector〈Graduate〉::iterator it = stu_g。begin(); it 〈 stu_g。end();) {
if (it->name == na) { it = stu_g。erase(it); cout <〈 ”删除成功!” 〈〈 endl; }
else { it++; }
}
}
cout 〈< endl 〈〈 "任意键返回” 〈〈 endl;
_getch();
}
void System::Interface() {
system("cls");
int n;
cout <〈 endl 〈〈 endl 〈〈 endl 〈〈 endl 〈〈 endl <〈 endl;
cout <〈 setfill(’ ’) 〈〈 setw(45) 〈< ”欢迎使用” 〈〈 endl;
cout 〈〈 setfill(’ ’) 〈< setw(53) << "*************************” <〈 endl;
cout <〈 endl;
cout 〈< setfill(' ’) <〈 setw(32) 〈〈 ”*1。” 〈< "输入学生信息" 〈〈 endl;
cout 〈〈 setfill(' ’) << setw(32) 〈〈 ”*2.” 〈〈 "查询学生信息并显示" 〈〈 endl;
cout <〈 setfill(' ’) 〈〈 setw(32) <〈 ”*3。” 〈〈 ”计算平均成绩并显示" 〈〈 endl;
cout << setfill(’ ’) 〈〈 setw(32) 〈〈 "*4。" 〈〈 ”删除学生信息” <〈 endl;
cout <〈 setfill(’ ’) 〈〈 setw(32) << ”*5." 〈〈 "退出系统” 〈< endl;
cout 〈< endl;
cout << setfill(' ’) 〈〈 setw(53) <〈 ”*************************” 〈< endl;
cout 〈〈 setfill(’ ’) 〈< setw(45) 〈< ”请输入编号(1-5):";
do {
cin 〉〉 n;
switch (n) {
case 1:system(”cls”); this—〉In_information(); Interface();
case 2:system(”cls”); this—〉Search(); Interface();
case 3:system("cls"); this—〉Out_average(); Interface();
case 4:system("cls"); this—>delete_inf(); Interface();
case 5:this-〉writefile();exit(0);
default:cout 〈< ”请输入正确的数字:" <〈 endl;
}
} while (n<1 || n>5);
}
//main.cpp
#include<iostream〉
#include〈fstream>
#include"System。h”
usingnamespace std;
int main() {
System s1;
system(”pause”);
return 0;
}
实验结果
实验截图:
图1 显示菜单
图2 添加信息
图3 退出系统查看文本文件
图4 二次进入查询信息
图5 二次进入删除信息
-—不懂的可以问我qq2136440859
展开阅读全文