C++学生成绩管理系统(有写文件操作)

C++学生成绩管理系统

本人是计算机系大二学生,写的程序比较粗糙,博客只是我用来记录自己的编程史的地方,望各位大佬看看就好。

#include<iostream>
#include<cstring>
#include<fstream>

using namespace std;

class Student {
public:
	void input();
	void sum();
	void sort();
	void average();
	void pass();
	void proportion();
private:
	int sumscore;
	int score1;
	int score2;
	int flag = 0;
	string name;
}s[5];

void Student::input() {
	cout << "请输入五名学生的姓名及两次成绩:(以空格隔开)" << endl;
	int i;
	ofstream fout("student.txt");
	for (i = 0; i < 5; i++) {
		cin >> s[i].name >> s[i].score1 >> s[i].score2;
		s[i].sumscore = s[i].score1 + s[i].score2;
		s[i].flag = 1;
		//写入文件
		fout << s[i].name << " " << s[i].score1 << " " << s[i].score2 << endl;
	}
	fout.close();  //关闭文件
	cout << "输入结束!"<<endl;
}

void Student::sum() {
	int i = 0;
	if (s[i].flag == 0) {
		cout << "请先输入学生信息!" << endl;
		return;
	}
	for (i = 0; i < 5; i++) {
		cout << "第" << i << "位同学的总成绩为:" << s[i].sumscore << endl;
	}
}

void Student::sort() {
	int j, temp;
	string n;
	int i = 0;
	ofstream fout("sort.txt");
	if (s[i].flag == 0) {
		cout << "请先输入学生信息!" << endl;
		return;
	}
	for (i = 0; i < 4; i++) {          //排序
		for (j = i+1; j < 5; j++) {
			if (s[i].sumscore < s[j].sumscore) {
				temp = s[i].sumscore;
				s[i].sumscore = s[j].sumscore;
				s[j].sumscore = temp;
				n = s[i].name;
				s[i].name = s[j].name;
				s[j].name = n;
			}
		}
	}
	for (i = 0; i < 5; i++) {            //输出并写入文件
		cout << s[i].name << " " << "总成绩为:" << s[i].sumscore << endl;
		fout << s[i].name << " " << "总成绩为:" << s[i].sumscore << endl;
	}
	fout.close();
}

void Student::average() {
	int ave = 0, ave1 = 0, ave2 = 0;
	int i = 0;
	if (s[i].flag == 0) {
		cout << "请先输入学生信息!" << endl;
		return;
	}
	for (i = 0; i < 5; i++) {
		ave1 += s[i].score1;
		ave2 += s[i].score2;
		ave += s[i].sumscore;
	}
	cout << "平均成绩分别为" << ave1 / 5 << " " << ave2 / 5 << " " << ave / 5 << endl;
}


void Student::pass() {
	int num1 = 0, num2 = 0;
	int i = 0;
	if (s[i].flag == 0) {
		cout << "请先输入学生信息!" << endl;
		return;
	}
	for (i = 0; i < 5; i++) {
		if (s[i].score1 >= 60) {
			num1 += 1;
		}
		if (s[i].score2 >= 60) {
			num2 += 1;
		}
	}
	cout << "两门课及格比例分别为:" << num1 * 100 / 5 << "%" <<" "<< num2 * 100 / 5 << "%" << endl;
	cout << "两门课不及格比例分别为:" << (5 - num1) * 100 / 5 << "%" << " " << (5 - num2) * 100 / 5 << "%" << endl;
}

void Student::proportion() {
	int i = 0;
	if (s[i].flag == 0) {
		cout << "请先输入学生信息!" << endl;
		return;
	}
	cout << "60-74为中,75-89为良,90-100为优" << endl;
	int you1 = 0, you2 = 0, liang1 = 0, liang2 = 0, zhong1 = 0, zhong2 = 0;
	for (i = 0; i < 5; i++) {
		if (s[i].score1 >= 60 && s[i].score1 < 75) {
			zhong1 += 1;
		}
		if (s[i].score1 >= 75 && s[i].score1 < 90) {
			liang1 += 1;
		}
		if (s[i].score1 >= 90 && s[i].score1 <= 100) {
			you1 += 1;
		}
		if (s[i].score2 >= 60 && s[i].score2 < 75) {
			zhong2 += 1;
		}
		if (s[i].score2 >= 75 && s[i].score2 < 90) {
			liang2 += 1;
		}
		if (s[i].score2 >= 90 && s[i].score2 <= 100) {
			you2 += 1;
		}
	}
	cout << "第一门课的优,良,中比例为:" << 100 * you1 / 5 << "%" << " " << 100 * liang1 / 5 << "%"
		<< " " << 100 * zhong1 / 5 << "%" << endl;
	cout << "第二门课的优,良,中比例为:" << 100 * you2 / 5 << "%" << " " << 100 * liang2 / 5 << "%"
		<< " " << 100 * zhong2 / 5 << "%" << endl;
}



int main()
{
	int key;
	Student Stu;
	cout << "-------------------学生成绩管理系统----------------------" << endl;
	cout << "-------------------1、输入学生及成绩----------------------" << endl;
	cout << "-------------------2、学生两门成绩之和----------------------" << endl;
	cout << "-------------------3、排序并写入文件----------------------" << endl;
	cout << "-------------------4、平均成绩----------------------" << endl;
	cout << "-------------------5、及格情况----------------------" << endl;
	cout << "-------------------6、优良中比例----------------------" << endl;
	cout << "-------------------7、按任意键退出----------------------" << endl;
	cout << "请输入数字:" << endl;
	while (cin >> key) {
		if (key == 1) {
			Stu.input();
			cout << "请输入数字:" << endl;
		}
		else if (key == 2) {
			Stu.sum();
			cout << "请输入数字:" << endl;
		}
		else if (key == 3 ){
			Stu.sort();
			cout << "请输入数字:" << endl;
		}
		else if (key == 4) {
			Stu.average();
			cout << "请输入数字:" << endl;
		}
		else if (key == 5) {
			Stu.pass();
			cout << "请输入数字:" << endl;
		}
		else if (key == 6) {
			Stu.proportion();
			cout << "请输入数字:" << endl;
		}
		else
		{
			cout << "谢谢使用!" << endl;
			return 0;
		}
	}
	return 0;
}
实验题目1:班级学生学期成绩管理系统 (1)程序功能简介 灵活运用类的继承、对象成员等机制,设计一个能够实现班级学生学期成绩管理的程序。 (2)程序设计说明 ① 个人信息类CPerson的数据成员有姓名、性别、年龄、身份证号等数据成员,成员函数根据需要自行设计; ② 学生类CStudent从CPerson派生,并增加学号、CCourse对象成员数组(大小至少3个)等数据成员,并根据需要自行设计成员函数,包括能够求解所选修课程的总学分、显示某个学生基本信息和课程信息的成员函数; ③ 课程类CCourse包括课程名、学分、分数、任课老师等数据成员,成员函数根据需要自行设计; ④ 班级类CClass的数据成员有班级名称、班级人数、CStudent对象成员数组(大小由构造函数确定)等。本班级类CClass的对象成员数组需要在构造函数中用new动态分配内存空间,在析构函数中用delete释放。在CClass类中设计包括能够求解最高成绩、最低成绩和平均成绩以及通过学号查找并输出某个学生全部信息(例如Seek())等功能在内的成员函数; ⑤ 构造三十个学生的数据,每个学生都有三门课程成绩,输出并显示这些数据; ⑥ 根据类的需要添加适当的其它成员,编完整的程序并测试。 (3)程序调试运行 运行程序查看结果,并进行源代码调试和优化。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值