第1题:设计一个学生学籍管理系统

 学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。

 从键盘输入数据,建立数据文件student.dat。
 实现对学生或学号查询,显示信息。
 对所有学生,按照班级计算平均成绩。
 分别按照英语、数学、程序设计和体育成绩排序生成结果文件。

直接上代码

student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;

class Student {
public:
	Student() {};
	Student(string name, string no, char gender, int english, int math, int program, int pe);
	int getenglish() const;
	int getmath() const;
	int getprogram() const;
	string getname() const;
	string getno() const;
	char getgender() const;
	int getpe() const;
	void print() const;
	string printout() const;
private:
	string name;
	string no;
	char gender;
	int english;
	int math;
	int program;
	int pe;
};

#endif
student.cpp
#include "Student.h"
#include <iostream>
#include <string>

Student::Student(string name, string no, char gender, int english, int math, int program, int pe)
	:name(name), no(no), gender(gender), english(english), math(math), program(program), pe(pe)
{}

void Student::print() const {
	cout << "学生姓名:" << getname() << endl;
	cout << "性别:" << getgender() << endl;
	cout << "英语成绩:" << getenglish() << endl;
	cout << "数学成绩:" << getmath() << endl;
	cout << "程序设计成绩:" << getprogram() << endl;
	cout << "体育成绩:" << getpe() << endl;
}

string Student::printout() const {
	string result;
	result += getname();
	result += " ";
	result += getgender();
	result += " ";
	result += getno();
	result += " ";
	result += to_string(getenglish());
	result += " ";
	result += to_string(getmath());
	result += " ";
	result += to_string(getprogram());
	result += " ";
	result += to_string(getpe());
	result += " ";
	return result;
}
int Student::getenglish()const {
	return english;
}

int Student::getmath() const {
	return math;
}

int Student::getprogram() const {
	return program;
}

string Student::getname() const {
	return name;
}
string Student::getno() const {
	return no;
}
char Student::getgender() const {
	return gender;
}

int Student::getpe() const {
	return pe;
}
ClassStudent.h
#ifndef CLASSSTUDENT
#define CLASSSTUDENT
#include "Student.h"
#include <vector>

using namespace std;
class School;
class ClassStudent {
	friend class School;
public:
	ClassStudent(string class_no);
	bool pushback(Student s);
	int getEnglishAve();
	int getMathAve();
	int getProgramAve();
	int getPeAve();
	string get_no() const;
	void getinformation(string name) const;
	void getinformationbyno(string no) const;
private:
	void computerscores();
	ClassStudent& operator=(const ClassStudent&) = delete;
	vector<Student> students;
	int English_ave;
	int Math_ave;
	int Program_ave;
	int Pe_ave;
	bool check;				//监视平均值是否为当前正确值。
	string class_no;
};

#endif
ClassStudent.cpp
#include "ClassStudents.h"
#include "Student.h"
#include <vector>
#include <iostream>
using namespace std;

ClassStudent::ClassStudent(string classno) 
: students(vector<Student>(0)), Math_ave(0), English_ave(0), Program_ave(0), check(0), class_no(classno){}

bool ClassStudent::pushback(Student s) {
	students.push_back(s);
	check = 0;
	return 1;
}

void ClassStudent::getinformation(string name) const{
	for (auto it = students.cbegin(); it != students.cend(); it++) {
		if (name == it->getname()) {
			it->print();
		}
	}
}

void ClassStudent::getinformationbyno(string no) const{
	for (auto it = students.cbegin(); it != students.cend(); it++) {
		if (no == it->getno()) {
			it->print();
		}
	}
}

int ClassStudent::getEnglishAve(){
	if (check)
		return English_ave;
	computerscores();
	return English_ave;
}
int ClassStudent::getMathAve(){
	if (check)
		return Math_ave;
	computerscores();
	return Math_ave;
}

int ClassStudent::getProgramAve(){
	if (check)
		return Pe_ave;
	computerscores();
	return Pe_ave;
}

int ClassStudent::getPeAve(){
	if (check)
		return Program_ave;
	computerscores();
	return Program_ave;
}

void ClassStudent::computerscores(){
	int nums = students.size();
	int totalen = 0;
	int totalma = 0;
	int totalpro = 0;
	int totalpe = 0;
	check = 1;
	for (auto it = students.cbegin(); it != students.cend(); it++)
	{
		totalen += it->getenglish();
		totalma += it->getmath();
		totalpro += it->getprogram();
		totalpe += it->getpe();
	}
	English_ave = totalen / nums;
	Math_ave = totalma / nums;
	Program_ave = totalpro / nums;
	Pe_ave = totalpe / nums;
}

string ClassStudent::get_no() const {
	return class_no;
}
School.h
#ifndef SCHOOL_H
#define SCHOOL_H
#include <vector>
#include "ClassStudents.h"
#include <string>

using namespace std;

class School {
public:
	School();
	bool push_student();
	void getinformation_byname(string name) const;
	void getinformation_byno(string no) const;
	void getallaverage();
	void printandsave();
	void Setfromfile();
private:
	vector<ClassStudent> schoolclass;
};

#endif
School.cpp
#include "School.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

School::School() : schoolclass(vector<ClassStudent>()) {};

bool School::push_student() {
	cout << "请输入学生姓名:" << endl;
	string name;
	cin >> name;
	cout << "请输入学生学号:" << endl;
	string no;
	cin >> no;
	cout << "请输入学生性别:" << endl;
	char gender;
	cin >> gender;
	cout << "请输入学生班级:" << endl;
	string class_no;
	cin >> class_no;
	cout << "请输入学生英语成绩:" << endl;
	int enscore;
	cin >> enscore;
	cout << "请输入学生数学成绩:" << endl;
	int mathscore;
	cin >> mathscore;
	cout << "请输入学生程序设计成绩:" << endl;
	int programscore;
	cin >> programscore;
	cout << "请输入学生体育成绩:" << endl;
	int pescore;
	cin >> pescore;
	
	for (auto it = schoolclass.begin(); it != schoolclass.end(); it++) {
		if (it->get_no() == class_no) {
			it->pushback(Student(name, no, gender, enscore, mathscore, programscore, pescore));
			return 1;
		}
	}
	schoolclass.push_back(ClassStudent(class_no));
	auto it = schoolclass.end() - 1;
	it->pushback(Student(name, no, gender, enscore, mathscore, programscore, pescore));
}

void School::getinformation_byname(string name) const{
	for (auto it = schoolclass.begin(); it != schoolclass.end(); it++) {
		it->getinformation(name);
	}
}

void School::getinformation_byno(string no) const{
	for (auto it = schoolclass.begin(); it != schoolclass.end(); it++) {
		it->getinformationbyno(no);
	}
}

void School::getallaverage(){
	for (auto it = schoolclass.begin(); it != schoolclass.end(); it++) {
		cout << "班级号" << it->get_no() << endl;
		cout << "数学平均成绩" << it->getMathAve() << endl;
		cout << "英语平均成绩" << it->getEnglishAve() << endl;
		cout << "程序设计平均成绩" << it->getProgramAve() << endl;
		cout << "体育平均成绩" << it->getPeAve() << endl;
		cout << endl;
	}
}

void School::printandsave() {
	ofstream fout;
	fout.open("Student.dat");
	vector<Student> together;
	for (auto it = schoolclass.cbegin(); it != schoolclass.cend(); it++) {
		for (auto sit = (it->students).begin(); sit != (it->students).end(); sit++) {
			fout << sit->printout() << it->get_no() << endl;
		}
		together.insert(together.end(), it->students.begin(), it->students.end());
	}
	fout.close();
	fout.open("english.txt");
	sort(together.begin(), together.end(),
		[](Student& a, Student& b)
		{return a.getenglish() > b.getenglish(); }
	);
	for (auto it = together.begin(); it != together.end(); it++) {
		fout << it->getname() << "\t" << it->getenglish() << endl;
	}
	fout.close();

	fout.open("math.txt");
	sort(together.begin(), together.end(),
		[](Student& a, Student& b)
		{return a.getmath() > b.getmath(); }
	);
	for (auto it = together.begin(); it != together.end(); it++) {
		fout << it->getname() << "\t" << it->getmath() << endl;
	}
	fout.close();

	fout.open("program.txt");
	sort(together.begin(), together.end(),
		[](Student& a, Student& b)
		{return a.getprogram() > b.getprogram(); }
	);
	for (auto it = together.begin(); it != together.end(); it++) {
		fout << it->getname() << "\t" << it->getprogram() << endl;
	}
	fout.close();

	fout.open("pe.txt");
	sort(together.begin(), together.end(),
		[](Student& a, Student& b)
		{return a.getpe() > b.getpe(); }
	);
	for (auto it = together.begin(); it != together.end(); it++) {
		fout << it->getname() << "\t" << it->getpe() << endl;
	}
	fout.close();

}

void School::Setfromfile() {
	string name;
	string no;
	int english;
	int math;
	int pe;
	int program;
	char gender;
	string c;
	ifstream fin;
	fin.open("Student.dat");
	string temp;
	if (!fin.is_open()) {
		return;
	}
	do {
		if (!getline(fin, temp)) {
			break;
		}
		stringstream s(temp);
		getline(s, name, ' ');
		s >> gender;
		getline(s, no, ' ');
		getline(s, no, ' ');
		s >> english >> math >> program >> pe;
		getline(s, c, ' ');
		getline(s, c, ' ');

		bool check = false;
		for (auto it = schoolclass.begin(); it != schoolclass.end(); it++) {
			if (it->get_no() == c) {
				it->pushback(Student(name, no, gender, english, math, program, pe));
				check = true;
				break;
			}
		}
		if (!check) {
			schoolclass.push_back(ClassStudent(c));
			auto it = schoolclass.end() - 1;
			it->pushback(Student(name, no, gender, english, math, program, pe));
		}
	} while (1);
	fin.close();
}
main
#include <iostream>
#include "School.h"

using namespace std;

int main()
{
	School university;
	university.Setfromfile();
	string name;
	string no;
	while (1) {
		cout << "1:输入学生数据" << endl;
		cout << "2:按照学生姓名查找" << endl;
		cout << "3:按照学生学号查找" << endl;
		cout << "4:计算班级平均分" << endl;
		cout << "5:按各成绩排名保存并退出" << endl;
		cout << "请选择:" << endl;
		char check;
		cin >> check;
		switch (check) {
		case '1':
			university.push_student();
			break;
		case '2':
			cout << "请输入名字:" << endl;
			cin >> name;
			university.getinformation_byname(name);
			break;
		case '3':
			cout << "请输入学号:" << endl;
			cin >> no;
			university.getinformation_byno(no);
			break;
		case '4':
			university.getallaverage();
			break;
		case '5':
			university.printandsave();
			return 1;
			break;
		default:
			cout << "请输入正确的选项" << endl;
			break;
		}
		cin.clear();
		cout << "继续" << endl;
	}
}
  • 13
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值