一个学期过去了,网课划水一时爽,一直划水一直爽,话不多说,来写写这学期的课程设计吧!
问题描述
研究生考试课程为 4 门,其中数学、外语、政 治为统一命题,而专业基础课则 根据不同的专业由招生学校自行命题。国家对初试录取分数有总分要求(如某一年 要求 4 门课总分应达到 310 分),另外还有对每门课的最低分数要求(如总分为 100 的试卷最低应达到 40 分,总分为 150 的试卷最低应达到 65 分)。编程统计初试合 格的人数,并按总分由高到低的顺序输出合格考生的信息。
基本要求
程序运行时首先要求输入:考生姓名,准考证号,报考专业,是否应届生,4 门课程(政治、数 学、外语、专业基础课)成绩。这些原始数据应保存到一个文件中。 然后输入:录取的总分要求,各课程的最低分数要求。 输出要求:过线考生的姓名,准考证号,报考专业,是否应届生,4 门课程(政治、数学、外语、专 业基础课)成绩及总分,这些信息应存放到另一个文件中。
测试数据
程序应输入不少于 10 名考生的信息,其中应届生和历届生分别有若干名,并且都有合格和不合 格的情况。
方案思路
person类为基类,拥有姓名、性别、年龄三个保护数据成员,student类公有继承person类,且增加了报考专业、考号、是否应届及政治成绩、数学成绩、英语成绩、专业课成绩七个私有数据成员。standard类拥有各科及总成绩分数线要求作为私有成员,student类作为standard类的友元类,可访问standard的数据,便于完成考生成绩与分数线的比较。文件1保存所有输入的考生信息,文件2保存成绩合格的考生信息,文件3保存成绩不合格的考生信息。主菜单下除了基本的输入考生和分数线外,还包含两个二级菜单,分别为排序和输出,二级菜单排序里分5种规则排序,输出分两种,一个是过线考生,另一个是未过线考生且程序可以在各个菜单之间自由切换。
设计代码
// students.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include "standard.h"
using namespace std;
//基类,保存person的姓名,性别,年龄
class person {
protected:
string name;//姓名
string sex;//性别
int age;//年龄
public:
//带默认参数构造函数
person(string str1 = " ", string str2 = " ", int num = 0) {
name = str1;
sex = str2;
age = num;
}
};
//派生类,公有继承person类,保存student相关信息
class student :public person {
private:
string major;//专业
string id;//考号
string current;//是否应届
int polity;//政治
int math;//数学
int english;//英语
int curscore;//专业课
public:
//带默认参数的构造函数
student(string name1 = " ", string sex1 = " ", int age1 = 0, string major1 = " ", string id1 = " ", string current1 = " ", int polity1 = 0, int math1 = 0, int english1 = 0, int curscore1 = 0) :person(name1, sex1, age1) {
major = major1;
id = id1;
current = current1;
polity = polity1;
math = math1;
english = english1;
curscore = curscore1;
}
//输入类成员信息
void input(string name1, string sex1, int age1, string major1, string id1, string current1, int polity1, int math1, int english1, int curscore1);
//输出类成员信息
void output();
//计算总成绩
int Sum() {
int result = polity + math + english + curscore;
return result;
}
//判断是否达标,达标返回1,否则返回0
int IsOk(standard& obj);
//写入文件1,保存所有学生的信息,成绩
void intofile1();
//写入文件2
void intofile2();
//写入文件2
void intofile3();
//重载输出运算符
friend ostream& operator << (ostream& os, student& p) {
os << "姓名:" << p.name << "\t性别:" << p.sex << "\t年龄:" << p.age << "\t专业:" << p.major << "\t考号:" << p.id << "\t是否应届:" << p.current << "\n政治:" << p.polity << "\t数学:" << p.math << "\t英语:" << p.english << "专业课:" << p.curscore <<"\t总成绩:" <<p.Sum()<<endl;
return os;
}
//获取私有成员
int Getp() {
return polity;
}
int Getm() {
return math;
}
int Gete() {
return english;
}
int Getc() {
return curscore;
}
//排序函数
friend void Rank1(student arr[], int m);
friend void Rank2(student arr[], int m);
friend void Rank3(student arr[], int m);
friend void Rank4(student arr[], int m);
friend void Rank5(student arr[], int m);
};
// students.cpp
#include <fstream>//处理文件
#include <iostream>
#include <iomanip>//包含格式化I/O操作算子
#include <string>
#include "students.h"
using namespace std;
//student类成员
//输入类成员信息
void student::input(string name1, string sex1, int age1, string major1, string id1, string current1, int polity1, int math1, int english1, int curscore1) {
name = name1;
sex = sex1;
age = age1;
major = major1;
id = id1;
current = current1;
polity = polity1;
math = math1;
english = english1;
curscore = curscore1;
}
//输出类成员信息
void student::output() {
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "年龄:" << age << endl;
cout << "报考专业:" << major << endl;
cout << "准考证号:" << id << endl;
cout << "是否应届生:" << current << endl;
cout << "政治成绩:" << polity << endl;
cout << "数学成绩:" << math << endl;
cout << "英语成绩:" << english << endl;
cout << "专业基础课成绩:" << curscore << endl;
cout << "总成绩:" << Sum() << endl;
}
//判断是否达标,达标返回1,否则返回0
int student::IsOk(standard& obj) {
int result = 0;
//判断是否合格
if (Sum() >= obj.sum_line
&& polity >= obj.polity_line
&& math >= obj.math_line
&& english >= obj.english_line
&& curscore >= obj.curscore_line) {
result = 1;
}
return result;
}
//将类成员写入文件1
void student::intofile1() {
ofstream out;//文件输出流对象,用于写文件
out.open("text1.txt", ios_base::app);//打开文件,每次写入前寻位到流结尾
out << name;
out << "\t";//水平制表符
out << sex;
out << "\t";
out << age;
out << "\t";
out << major;
out << "\t";
out << id;
out << "\t";
out << current;
out << "\t";
out << polity