C++实现学生成绩管理系统

学生成绩管理系统

在下小白一枚,给各位大神奉上源码!
拒绝一切花里胡哨,所以我没有美化菜单和背景。
包含读写文件操作和vector容器操作。
希望各位点个赞!

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<fstream>
class Student   //学生类
{
public:
	Student(int id, string name, int math, int eng,int phy)
	{
		this->id = id;
		this->name = name;
		this->math = math;
		this->eng = eng;
		this->phy = phy;
		this->sum = math + eng + phy;
	}
	
public:
	int id;
	string name;
	int math,eng,phy;
	int sum;
};   

bool mySort(Student s1, Student s2)   //根据条件判断排序
{
	if (s1.sum != s2.sum)
	{
		return s1.sum < s2.sum;
	}
	else
	{
		if (s1.math != s2.math)
		{
			return s1.math < s2.math;
		}
		else
		{
			if (s1.eng != s2.eng)
			{
				return s1.eng < s2.eng;
			}
			else
			{
				if (s1.phy != s2.phy)
				{
					return s1.phy < s2.phy;
				}
				else
				{
					return s1.id < s2.id;
				}
			}
		}
	}
}

class Management   //管理类
{
public:
	Management()   //读文件
	{
		ifstream ifs;
		ifs.open("text.txt", ios::in);
		if (ifs.is_open() != NULL)
		{
			int id, math, eng, phy, sum;
			string name;
			while (ifs >> id >> name >> math >> eng >> phy >> sum)
			{
				Student s(id, name, math, eng, phy);
				v.push_back(s);
			}
		}
		ifs.close();
	}
	vector<Student>v;
	void Keep()   //保存文件
	{
		ofstream ofs;
		ofs.open("text.txt", ios::out);
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			ofs << (*it).id << "\t" << (*it).name << "\t" << (*it).math << "\t" << (*it).eng << "\t" << (*it).phy << "\t" << (*it).sum << endl;
		}

		ofs.close();
	}
	void Add()   //增加
	{
		cout << "请依次输入学生的学号,姓名,数学成绩,英语成绩,物理成绩" << endl;
		int id;
		string name;
		int math;
		int eng;
		int phy;
		
		cin >> id;
		cin >> name;
		cin >> math;
		cin >> eng;
		cin >> phy;
		Student s(id, name, math, eng, phy);
		v.push_back(s);
		Keep();
		cout << "添加成功" << endl;
		system("pause");
		system("cls");
	
	}

	void Del()   //删除
	{
		cout << "请输入学生的学号" << endl;
		int id;
		cin >> id;
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			if ((*it).id == id)
			{
				int select;
				cout << "是否删除?" << endl;
				cout << "1.是" << "   " << "2.否" << endl;
				cin >> select;
				switch (select)
				{
				case 1:
					v.erase(it);
					cout << "删除成功" << endl;
					break;
				case 2:
					cout << "您未删除" << endl;
					break;
				}
				if ((*it).id != id)
				{
					cout << "查无此人" << endl;
				}

			}
		}
		Keep();
		system("pause");
		system("cls");
	}
	
	void Search()   //查找
	{
		cout << "请输入学生的学号" << endl;
		int id;
		cin >> id;
		bool flag = false;
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			if ((*it).id == id)
			{
				flag = true;
				cout << "学号" << "\t" << "姓名" << "\t" << "数学" << "\t" << "英语" << "\t" << "物理" << endl;
				cout  << (*it).id << "\t"  << (*it).name << "\t"  << (*it).math
					<< "\t"  << (*it).eng << "\t"  << (*it).phy << endl;
			}
		}
		if (flag == false)
			cout << "查无此人!" << endl;
		system("pause");
		system("cls");
	}

	void Modify()   //修改
	{
		cout << "请输入学生的学号" << endl;
		int id;
		cin >> id;
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			if ((*it).id == id)
			{
				cout << "已找到该学生,请重新输入该学生的信息(学号、姓名、数学成绩、英语成绩、物理成绩)" << endl;
				int id;
				string name;
				int math;
				int eng;
				int phy;
				cin >> id;
				cin >> name;
				cin >> math;
				cin >> eng;
				cin >> phy;
				(*it).id = id;
				(*it).name = name;
				(*it).math = math;
				(*it).eng = eng;
				(*it).phy = phy;
				cout << "修改成功!" << endl;
			}


		}
		Keep();
		system("pause");
		system("cls");
	}

	void Statistic()   //统计
	{
		double sumZ=0, sumM=0,aveM=0, sumE=0,aveE=0,sumP=0,aveP=0
			,BM=0,bM=0,BE=0,bE=0,BP=0,bP=0;   //这些你们应该看得懂
		double HM = 0,hM=0, HE = 0,hE=0 ,HP=0 ,hP= 0;
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			sumZ += (*it).sum;
			sumM += (*it).math;
			sumE += (*it).eng;
			sumP += (*it).phy;
			bM += pow(((*it).math - aveM), 2);
			bE += pow(((*it).eng - aveE), 2);
			bP += pow(((*it).phy - aveP), 2);
		}
		aveM = sumM / (v.size());
		aveE = sumE / (v.size());
		aveP = sumP / (v.size());
		BM= sqrt(bM / v.size());
		BE= sqrt(bE / v.size());
		BP = sqrt(bP / v.size());
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			if ((*it).math >= 60)
			{
				hM++;
			}
			if ((*it).eng >= 60)
			{
				hE++;
			}
			if ((*it).phy >= 60)
			{
				hP++;
			}
				
		}
		HM = hM / v.size();
		HE = hE / v.size();
		HP = hP / v.size();
		cout << "平均分:" << sumZ / (v.size()) << endl;
		cout << "数学平均分:" << aveM << endl;
		cout << "英语平均分:" << aveE << endl;
		cout << "物理平均分:" << aveP << endl;
		cout << "数学标准差:" << BM << endl;
		cout << "英语标准差:" << BE << endl;
		cout << "物理标准差:" << BP << endl;
		cout << "数学合格率:" << HM << endl;
		cout << "英语合格率:" << HE << endl;
		cout << "物理合格率:" << HP << endl;
		system("pause");
		system("cls");
	}

	void Sort()   //打印排序结果
	{
		sort(v.begin(), v.end(), mySort);
		cout << "学号" << "\t" << "姓名" << "\t" << "数学" << "\t" << "英语" << "\t" << "物理\t" << "总分" << endl;
		
		for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
		{
			cout << (*it).id << "\t" << (*it).name << "\t" << (*it).math << "\t" << (*it).eng << "\t" << (*it).phy << "\t" << (*it).sum << endl;
		}
		Keep();
		system("pause");
		system("cls");
	}

	void Show()   //显示
	{
		if (v.size() == 0)
			cout << "文件不存在" << endl;
		else
		{
			cout << "学号" << "\t" << "姓名" << "\t" << "数学" << "\t" << "英语" << "\t" << "物理" << "\t"<<"总分" << endl;
			for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
			{
				cout << (*it).id << "\t" << (*it).name << "\t" << (*it).math << "\t" << (*it).eng << "\t" << (*it).phy << "\t" << (*it).sum << endl;

			}
		}
		system("pause");
		system("cls");

	}
};
void Menushow()   //菜单
{
	cout << "学生成绩管理系统" << endl;
	cout << "1.增加" << "\t" << "2.删除" << endl;
	cout << "3.查找" << "\t" << "4.修改" << endl;
	cout << "5.统计" << "\t" << "6.排序" << endl;
	cout << "7.显示" << "\t" << "8.保存" << endl;
	cout << "0.退出" << endl;
}

int main()
{
	Management m;
	int select;
	while (1)
	{
		Menushow();
		cin >> select;
		switch (select)
		{
		case 1:
			m.Add();
			break;
		case 2:
			m.Del();
			break;
		case 3:
			m.Search();
			break;
		case 4:
			m.Modify();
			break;
		case 5:
			m.Statistic();
			break;
		case 6:
			m.Sort();
			break;
		case 7:
			m.Show();
			break;
		case 8:
			m.Keep();
			break;
		case 0:
			exit(0);
		}

	}
	return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页