一个学期过去了,网课划水一时爽,一直划水一直爽,话不多说,来写写这学期的课程设计吧!
问题描述
研究生考试课程为 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
#include
#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 <
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 //处理文件
#include
#include //包含格式化I/O操作算子
#include
#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;
out << "\t";
out << math;
out << "\t";
out << english;
out << "\t";
out << curscore << setw(9);//设置域宽
out.close();
}
//将类成员写入文件2
void student::intofile2() {
ofstream out;//文件输出流对象,用于写文件
out.open("text2.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;
out << "\t";
out << math;
out << "\t";
out << english;
out << "\t";
out << curscore << setw(9);//设置域宽
out.close();
}
//将类成员写入文件2
void student::intofile3() {
ofstream out;//文件输出流对象,用于写文件
out.open("text3.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;
out << "\t";
out << math;
out << "\t";
out << english;
out << "\t";
out << curscore << setw(9);//设置域宽
out.close();
}
//按总成绩排序
void Rank1(student arr[], int m) {
int i = 0, j = 0;
student tmp;
//由高到低排序
for (i = 0; i < m - 1; i++) {
for (j = i + 1; j < m; j++) {
if (arr[i].Sum() < arr[m].Sum()) {
tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
}
}
//按政治成绩排序
void Rank2(student arr[], int m) {
int i = 0, j = 0;
student tmp;
//由高到低排序
for (i = 0; i < m - 1; i++) {
for (j = i + 1; j < m; j++) {
if (arr[i].Getp() < arr[m].Getp()) {
tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
}
}
//按数学成绩排序
void Rank3(student arr[], int m) {
int i = 0, j = 0;
student tmp;
//由高到低排序
for (i = 0; i < m - 1; i++) {
for (j = i + 1; j < m; j++) {
if (arr[i].Getm() < arr[m].Getm()) {
tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
}
}
//按英语成绩排序
void Rank4(student arr[], int m) {
int i = 0, j = 0;
student tmp;
//由高到低排序
for (i = 0; i < m - 1; i++) {
for (j = i + 1; j < m; j++) {
if (arr[i].Gete() < arr[m].Gete()) {
tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
}
}
//按专业课成绩排序
void Rank5(student arr[], int m) {
int i = 0, j = 0;
student tmp;
//由高到低排序
for (i = 0; i < m - 1; i++) {
for (j = i + 1; j < m; j++) {
if (arr[i].Getc() < arr[m].Getc()) {
tmp = arr[i];
arr[i] = arr[m];
arr[m] = tmp;
}
}
}
}
// standard.h
#pragma once
#include
using namespace std;
//录取总分及各科最低分数标准
class standard {
private:
//总分100的试卷最低应达到40分,总分150的试卷最低应达到65
int sum_line;//总分线
int polity_line;//政治线
int math_line;//数学线
int english_line;//英语线
int curscore_line;//专业课线
public:
//带默认参数的构造函数
standard(int su1 = 310, int po1 = 40, int ma1 = 65, int en1 = 40, int cu1 = 65) {
sum_line = su1;
polity_line = po1;
math_line = ma1;
english_line = en1;
curscore_line = cu1;
}
//输入函数
void input(int su1, int po1, int ma1, int en1, int cu1) {
sum_line = su1;
polity_line = po1;
math_line = ma1;
english_line = en1;
curscore_line = cu1;
}
friend class student;//定义student类为standard类的友元
};
// main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include "standard.h"
#include "students.h"
using namespace std;
void Inputting1() {
student* network;
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
int choice = 0;
int i = 0;
//申请内存空间
network = new student[20];
while (1) {
cout << "您正在输入第 " << i + 1 << " 位学生信息\n" << endl;
cout << " 姓名:"; cin >> name; cout << endl;
cout << " 性别:"; cin >> sex; cout << endl;
cout << " 年龄:"; cin >> age; cout << endl;
cout << " 报考专业:"; cin >> major; cout << endl;
cout << " 准考证号:"; cin >> id; cout << endl;
cout << " 是否应届生:"; cin >> current; cout << endl;
cout << " 政治成绩:"; cin >> polity; cout << endl;
cout << " 数学成绩:"; cin >> math; cout << endl;
cout << " 英语成绩:"; cin >> english; cout << endl;
cout << " 专业基础课成绩:"; cin >> curscore; cout << endl;
//输入数据
network[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
//写入文件
network[i].intofile1();
//控制循环是否继续
cout << "是否继续? 0/1 "; cin >> choice;
i++;
system("cls");//清屏
if (choice == 0) {
break;
}
}
}
void Inputting2() {
//用于输入分数要求
standard R;
int sum;
int polity;
int math;
int english;
int curscore;
string name;
string sex;
int age;
string major;
string id;
string current;
int polity1;
int math1;
int english1;
int curscore1;
student aginst;
cout << "请输入:总成绩,政治,数学,英语,专业基础课的标准:\n";
cin >> sum >> polity >> math >> english >> curscore;
ofstream de1("text2.txt", ios_base::trunc);//如果文件存在,清空文件内容
de1.close();
ofstream de2("text3.txt", ios_base::trunc);//如果文件存在,清空文件内容
de2.close();
R.input(sum, polity, math, english, curscore);
ifstream is("text1.txt", ios_base::in | ios_base::binary);
while (!is.eof()) {
is >> name;
is >> sex;
is >> age;
is >> major;
is >> id;
is >> current;
is >> polity1;
is >> math1;
is >> english1;
is >> curscore1;
aginst.input(name, sex, age, major, id, current, polity1, math1, english1, curscore1);
if (aginst.IsOk(R) == 1) {//将过线学生保存到“text2.txt”
//aginst.output();
aginst.intofile2();
}
else {
aginst.intofile3();
}
}
is.close();
}
void See1() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text2.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank1(aginst, i-1);
cout << endl;
for (int k = 0; k < i; k++) {
cout << aginst[k] << endl;
}
in.close();
}
void See2() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text2.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank2(aginst, i - 1);
cout << endl;
for (int k = 0; k < i - 1; k++) {
cout << aginst[k] << endl;
}
in.close();
}
void See3() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text2.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank3(aginst, i - 1);
cout << endl;
for (int k = 0; k < i - 1; k++) {
cout << aginst[k] << endl;
}
in.close();
}
void See4() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text2.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank4(aginst, i - 1);
cout << endl;
for (int k = 0; k < i - 1; k++) {
cout << aginst[k] << endl;
}
in.close();
}
void See5() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text2.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank5(aginst, i - 1);
cout << endl;
for (int k = 0; k < i - 1; k++) {
cout << aginst[k] << endl;
}
in.close();
}
void Seefile2() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text2.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank1(aginst, i - 1);
cout << endl;
for (int k = 0; k < i; k++) {
cout << aginst[k] << endl;
}
in.close();
}
void Seefile3() {
string name;
string sex;
int age;
string major;
string id;
string current;
int polity;
int math;
int english;
int curscore;
student* aginst;
int i = 0;
aginst = new student[20];
ifstream in("text3.txt", ios_base::in);//为读打开
while (!in.eof()) {
in >> name;
in >> sex;
in >> age;
in >> major;
in >> id;
in >> current;
in >> polity;
in >> math;
in >> english;
in >> curscore;
aginst[i].input(name, sex, age, major, id, current, polity, math, english, curscore);
i += 1;
/* if(out.eof()==0){
break;
}
*/
}
Rank1(aginst, i-1);
cout << endl;
for (int k = 0; k < i; k++) {
cout << aginst[k] << endl;
}
in.close();
}
int main() {
breakhere1:
breakhere2:
int choice1, choice2;//控制选项
do {
cout << endl;
cout << "****欢迎使用研究生初试录取系统****" << endl;
cout << endl;
cout << "\t请选择所需功能:" << endl;
cout << endl;
cout << "\t1.输入学生初始信息" << endl;
cout << endl;
cout << "\t2.输入当年分数要求" << endl;
cout << endl;
cout << "\t3.排序考生信息" << endl;
cout << endl;
cout << "\t4.输出考生信息" << endl;
cout << endl;
cout << "**********************************" << endl;
cout << "\n请您输入:";
cin >> choice1;
switch (choice1) {
case 1:
system("cls");
Inputting1();//输入学生初始信息
break;
case 2:
system("cls");
Inputting2();//输入分数线
break;
case 3:
system("cls");
int num1,num2;
do {
cout << "********************" << endl;
cout << endl;
cout << "1.按总成绩排序查看" << endl;
cout << endl;
cout << "2.按政治成绩排序查看" << endl;
cout << endl;
cout << "3.按数学成绩排序查看" << endl;
cout << endl;
cout << "4.按英语成绩排序查看" << endl;
cout << endl;
cout << "5.按专业课成绩排序查看" << endl;
cout << endl;
cout << "6.返回" << endl;
cout << "********************" << endl;
cout << "\n请您输入:";
cin >> num1;
switch (num1) {
case 1:
system("cls");
See1();
break;
case 2:
system("cls");
See2();
break;
case 3:
system("cls");
See3();
break;
case 4:
system("cls");
See4();
break;
case 5:
system("cls");
See5();
break;
case 6:
system("cls");
goto breakhere1;
default:
break;
}
cout << endl;
cout << "***返回上级菜单请按 0 !***" << endl;
cout << "\n请您输入:";
cin >> num2;
} while (num2 == 0);
break;
case 4:
system("cls");
int num3, num4;
do {
system("cls");
cout << "********************" << endl;
cout << endl;
cout << "1.输出过线考生" << endl;
cout << endl;
cout << "2.输出未过线考生" << endl;
cout << endl;
cout << "3.返回" << endl;
cout << "********************" << endl;
cout << "\n请您输入:";
cin >> num3;
switch (num3) {
case 1:
system("cls");
Seefile2();
break;
case 2:
system("cls");
Seefile3();
break;
case 3:
system("cls");
goto breakhere2;
default:
break;
}
cout << endl;
cout << "***返回上级菜单请按 0 !***" << endl;
cout << "\t请您输入:";
cin >> num4;
} while (num4 == 0);
default:
break;
}
cout << endl;
cout << "***返回上级菜单请按 0 !***" << endl;
cout << "\n请您输入:";
cin >> choice2;
system("cls");
} while (choice2 == 0);
return 0;
}
总结
好啦,程序就介绍到这里,给大家看看运行的截图!
That’s all.