本科生和研究生成绩管理系统
某大学班级有本科生CStudent、留学生FStudent,他们的绩点计算方法如下:
本科生绩点计算方法是: 英语*0.03+数学*0.04+计算机*0.03。
留学生绩点计算方法: 中文*0.03+数学*0.04+计算机*0.03。
成绩均为百分制,绩点总计10。
每类学生都有学号、姓名、性别、年龄、类别等数据,类别分为本科生和留学生,各类人员使用统一接口getPoint()计算各类人员的绩点,重载<<运算符实现学生信息的输出。其次,设计一个统计并输出该班级每个人员绩点信息的报表类Report,该类提供insert接口向Report类的容器中添加学生信息,并提供print接口用于输出所有学生的学号、姓名、性别、年龄和绩点。为了方便实现查找功能,为Report类重载[]运算符的功能,下标值为学生类别,能根据类别查找出所有该类别的学生,并重载print接口,输出指定类别的学生信息。初始成绩表里可以先固定添加4个本科生和4个留学生信息。各个指令和对应功能如下:
指令1:输入学号,删除相应记录;
指令2:删除所有学生的记录;
指令3:分类别显示所有学生绩点记录表;
指令4:输入类别,显示该类别学生绩点记录表;
指令-1:退出。
//class.h
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED
#include<windows.h>
#include<iostream>
#include<string>
#include<list>
#include<vector>
#include<map>
#include"date.h"
using namespace std;
class Person
{
protected:
string ID;//学号
string name;//姓名
string sex;//性别
string type;//学生类型
int age;
double score;
public:
Person(string ID, string name, string sex, int age,string type, double score);
string gettype() { return type; }
string getname() { return name; }
string getID() { return ID; }
string getsex() { return sex; }
int getage() { return age; }
double getscore()
{
return score;
}
virtual double getp(int m,int i,int j) = 0;
//friend ostream & operator<<(ostream & os,const Person & p);
};
class undergraduate :public Person
{
/*private:
map<int>score;
*/
private:
map<int,int> grade1;
public:
undergraduate(string ID, string name, string sex, int age, string type, double score, map<int,int> grade1) :Person(ID, name, sex, age, type, score) { this->grade1 = grade1; }
//double getscore(int m);
double getp(int m,int i,int j);
};
class graduate :public Person
{
private:
map<int,int> grade;
public:
graduate(string ID, string name, string sex, int age, string type, double score, map<int,int> grade) :Person(ID, name, sex, age, type, score) { this->grade = grade; }
//double getscore(int m);
double getp(int m,int i,int j);
};
class Report
{
private:
list<Person*> members;
list<Person*> operator[](string type);
double min_score(list<Person*> emp_list);
double max_score(list<Person*> emp_list);
void print(list<Person*> emp_list);
public:
//~Report();
void insert(Person* p);
void print(string type);
void delete_allstudent();//删除所有人的信息
void delete_student();//根据学号删除信息
};
#endif
//functions.cpp
#include"class.h"
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
Person::Person(string ID, string name, string sex, int age, string type, double score)
{
this->ID = ID;
this->name = name;
this->sex = sex;
this->age = age;
this->score = score;
this->type = type;
}
list<Person*> Report::operator[](string type)
{
list<Person*> cp;
list<Person*>::iterator it;
for (it = members.begin(); it != members.end(); it++)
{
if ((*it)->gettype() == type)
{
cp.push_back(*it);
}
}
return cp;
}
void Report::print(string t)
{
cout << "类别:" << t << endl;
cout << "学号\t" << "姓名\t" << "性别\t" << "年龄\t"<< "绩点" << endl;
list<Person*> emp_ls;
emp_ls = (*this)[t];
print(emp_ls);
cout << "最低绩点:" << min_score(emp_ls) << endl;
cout << "最高绩点:" << max_score(emp_ls) << endl;
cout << "--------------------------------" << endl;
}
double Report::min_score(list<Person*>emp_list)
{
vector<double>score;
list<Person*>::iterator it;
for (it = emp_list.begin(); it != emp_list.end(); it++)
{
score.push_back((*it)->getp(1,2,3));
}
return *min_element(score.begin(), score.end());
}
double Report::max_score(list<Person*>emp_list)
{
vector<double> score;
list<Person*>::iterator it;
for (it = emp_list.begin(); it != emp_list.end(); it++)
{
score.push_back((*it)->getp(1,2,3));
}
return *max_element(score.begin(), score.end());
}
void Report::print(list<Person*> emp_list)
{
list<Person*>::iterator it;
for (it = emp_list.begin(); it != emp_list.end(); it++)
{
cout << (*it)->getID() << "\t" << (*it)->getname() << "\t";
cout << (*it)->getsex() << "\t";
cout << (*it)->getage() << "\t";
cout << (*it)->getp(1,2,3) << "\t";
cout << endl;
//cout << (*it)->getscore(type) << endl;
}
}
void Report::insert(Person* p)
{
members.push_back(p);
}
void Report::delete_allstudent()
{
list<Person*>::iterator it;
for (it = members.begin(); it != members.end(); it++)
{
delete* it;
}
cout << "所有人的信息删除成功!" << endl;
}
void Report::delete_student()
{
string id;
string type;
cout << "请输入学生的ID:";
cin >> id;
cout << "请输入学生类别(本科生or研究生):";
cin >> type;
list<Person*>::iterator it;
//for (it = members.begin(); it != members.end(); it++)
//{
if (type == "本科生") {
for (it = members.begin(); it != members.end(); it++)
{
if (strcmp("it->getID()", "id")) {
cout << "已成功找到该生信息!" << endl;
char ch;
cout << "是否确定删除(y or n):";
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
members.erase(it++);
cout << "删除成功!" << endl;
system("pause");
return;
}
}
else {
it++;
}
}
}
else if (type == "研究生") {
for (it = members.begin(); it != members.end(); it++)
{
if (strcmp("it->getID()", "id")){
cout << "已成功找到该生信息!" << endl;
char ch;
cout << "是否确定删除(y or n):";
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
members.erase(it++);
cout << "删除成功!" << endl;
system("pause");
return;
}
}
else {
it++;
}
}
}
else {
cout << "该生类别不存在,请核实后输入..." << endl;
system("pause");
return;
}
}
double undergraduate::getp(int m,int i,int j) {
return score + grade1[m] * 0.03 + grade1[i] * 0.04 + grade1[j] * 0.03;
}
double graduate::getp(int m,int i,int j) {
return score + grade[m] * 0.03+grade[i]*0.04+grade[j]*0.03;
}
//main.cpp
#include"class.h"
//#include"date.h"
#include"interface.h"
#include<map>
#include<iostream>
using namespace std;
int main()
{
//Interface inter;
Report re;
map<int, int> grade1;
grade1[1] = 98;
grade1[2] = 97;
grade1[3] = 96;
re.insert(new undergraduate("001", "张三", "男", 19, "本科生", 0,grade1));
map<int, int> grade;
grade1[1] = 99;
grade1[2] = 96;
grade1[3] = 95;
re.insert(new undergraduate("002", "李四", "女", 17, "本科生", 0, grade1));
grade1[1] = 94;
grade1[2] = 92;
grade1[3] = 91;
re.insert(new undergraduate("003", "祝雪", "女", 16, "本科生", 0, grade1));
grade1[1] = 89;
grade1[2] = 96;
grade1[3] = 91;
re.insert(new undergraduate("004", "祝贺", "男", 18, "本科生", 0, grade1));
grade[1] = 90;
grade[2] = 91;
grade[3] = 98;
re.insert(new graduate("101", "Tom", "男", 20, "研究生", 0, grade));
grade[1] = 92;
grade[2] = 91;
grade[3] = 90;
re.insert(new graduate("102", "Jack", "男", 19, "研究生", 0, grade));
grade[1] = 99;
grade[2] = 93;
grade[3] = 88;
re.insert(new graduate("103", "Lusy", "女", 18, "研究生", 0, grade));
grade[1] = 90;
grade[2] = 86;
grade[3] = 85;
re.insert(new graduate("104", "Mike", "男", 19, "研究生", 0, grade));
int i;
cout << "-------------------------------------" << endl;
cout << "操作菜单项" << endl;
cout << "输入1,输入学号,删除相应记录" << endl;
cout << "输入2,删除所有学生记录" << endl;
cout << "输入3,分类别显示所有学生绩点记录表" << endl;
cout << "输入4,输入类别,显示该类别学生绩点记录表" << endl;
cout << "输入-1,退出" << endl;
cout << "-------------------------------------" << endl;
while (1) {
cout << "输入您的选择:" << "i=" << endl;
cin >> i;
if (i == -1) {
cout << "退出成绩管理系统" << endl;
break;
}
if (i == 1) {
re.delete_student();//根据学号删除学生信息
}
if (i == 2) {
re.delete_allstudent();//删除所有人的信息
}
if (i == 3) {
cout << "分别显示绩点记录表" << endl;
cout << "---------------------------" << endl;
re.print("本科生");
re.print("研究生");
}
if (i == 4) {
cout << "按类别显示绩点记录表" << endl;
cout << "请输入类别(本科生or研究生):" << endl;;
string type;
cin >> type;
if (type == "本科生") {
re.print("本科生");
}
else if (type == "研究生") {
re.print("研究生");
}
else {
cout << "您输入有误,请重新输入" << endl;
}
}
}
return 0;
}
运行结果展示
1、显示所有学生的信息(功能三)
2、分类别显示学生的信息(按照输入类别显示学生信息)
(1)输入本科生
(2)输入研究生
3、根据学号删除学生信息(例删除本科生学号001)
4、删除所有人的信息