C++学生成绩管理系统

#include <map>
#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
class Courses
{
private:
	std::map<std::string, int> c;   //课程名字到编号的映射,编号从0开始
	std::vector<std::string> names; //课程编号到名字的映射
public:
	int getNum() { return c.size(); } //课程总数
	int addCourse(std::string name)   //返回新ID
	{
		int temp = c.size();
		c[name] = temp;
		names.push_back(name);
		return temp;
	}
	int findCourseId(std::string name) //课程名字寻找课程ID,找不到返回-1
	{
		if (c.find(name) != c.end())
			return c[name];
		else
			return -1;
	}
	std::string queryName(int id) //询问课程名称
	{
		if (id < 0 || id >= c.size())
			return std::string("");
		return names[id];
	}
	void display() //输出表头
	{
		printf("\t");
		for (auto i : names)
			printf("%s\t", i.c_str());
		printf("总分\t平均分\t排名\n");
	}
} courses;

class Student
{
private:
	std::string name;
	std::map<int, double> score;
	double totScore = 0;
public:
	bool operator<(const Student &x) const
	{ //输出时候除以课程总数就行了courses.getNum()
		return totScore > x.totScore;
	}
	friend class Students;
};

class Students
{
private:
	std::map<std::string, int> mp; //名字到下标的映射
	std::vector<Student> s;		   //下标到名字的映射
public:
	int getNum(){return s.size();}
	int findName(std::string name)
	{
		std::map<std::string, int>::iterator it = mp.find(name);
		if (it == mp.end()) return 0;
		return it->second;
	}
	void addStudent(std::string name) //录入学生成绩
	{
		Student temp;
		temp.name = name;
		int totCourse = courses.getNum();
		for(int i=0;i<totCourse;i++)
		{
			std::cout<<"请输入"<<courses.queryName(i)<<"成绩:"<<std::endl;
			double ss;
			std::cin>>ss;
			temp.score[i]=ss;
			temp.totScore+=ss;
		}
		s.push_back(temp);
		mp[name] = mp.size();
		return; //成功
	}
	double queScore(std::string name,int courseId)
	{
		std::map<std::string, int>::iterator it = mp.find(name);
		if (it == mp.end())
			return -1.0;
		int sid=it->second;
		return s[sid].score[courseId];
	}
	bool changeScore(std::string name, int courseId, double newscore) //返回是否成功
	{
		std::map<std::string, int>::iterator it = mp.find(name);
		if (it == mp.end())
			return false;
		int sid = it->second;
		int temp = s[sid].score[courseId];
		s[sid].score[courseId] = newscore;
		s[sid].totScore += newscore - temp;
		return true;
	}
	void displayStudent(std::string name) //输出一个学生的成绩,不输出表头
	{
		int sid = mp[name];
		displayStudent(sid);
	}
	void displayStudent(int sid)	 //输出一个学生的成绩,不输出表头
	{
		std::cout<<s[sid].name<<"\t";
		for (int i = 0; i < courses.getNum(); i++)
		{
			printf("%.1f\t", s[sid].score[i]);
		}
		printf("%.1f\t%.1f\t%d/%u\n", s[sid].totScore, s[sid].totScore / courses.getNum(), sid + 1, mp.size());
	}
	
	void sort()
	{
		std::sort(s.begin(), s.end());
		int temp = 0;
		for (auto i : s)
		{
			mp[i.name] = temp++; //重排mp
		}
	}
} students;

class FrontPage{
private:
	inline void read(int &x)
	{
		int s = 0, w = 1;
		char ch = getchar();
		while (ch < '0' || ch > '9')
		{
			if (ch == '-')
				w = -1;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9')
			s = s * 10 + ch - '0', ch = getchar();
		x = s * w;
	}
	int temp;
public:
	void run()
	{
		printf("启动中");
		for (int i = 1; i <= 5; i++)
			system("sleep 0.1"), printf(".");
		system("cls");
		printf("欢迎使用成绩管理系统!\n请输入科目数量:\n");
		read(temp);
		puts("请输入每个科目的名称,用回车键分隔");
		for(int i=0;i<temp;i++)
		{
			std::string name;
			std::cin>>name;
			courses.addCourse(name);
		}
		puts("请输入学生数量");
		read(temp);
		puts("开始录入成绩");
		for(int i=1;i<=temp;i++)
		{
			std::string name;
			puts("输入姓名:");
			std::cin>>name;
			students.addStudent(name);
			puts("添加成功");
		}
		students.sort();
		while (1)
		{
			puts("添加学生请输入1\n修改学生成绩请输入2\n查询某学生成绩请输入3\n按排名输出成绩单请输入4");
			int type = 0;
			read(type); //提高容错性
			if (type == 1)
			{
				std::string name;
				puts("输入姓名:");
				std::cin>>name;
				students.addStudent(name);
				puts("添加成功");
				students.sort();
				system("sleep 2");
				system("cls");
				continue;
			}
			if (type == 2)
			{
				std::string name;
				puts("姓名:");
				std::cin >> name;
				if(!students.findName(name))
				{
					puts("查无此人");
					system("sleep 2");
					system("cls");
					continue;
				}
				puts("要修改的科目名称:");
				std::string cour;
				int courid;
				while (1)
				{
					std::cin>>cour;
					if(cour==std::string("@$#")) goto cao;
					courid = courses.findCourseId(cour);
					if(courid==-1)
					{
						puts("查无此科目,请重新输入。返回上层请输入@$#");
					}
					else break;
				}
				std::cout<< "原先成绩为" << students.queScore(name, courid)<<",请输入新的成绩:" << std::endl;
				double newsc;
				std::cin>>newsc;
				students.changeScore(name,courid,newsc);
				puts("修改成功");
				students.sort();
				system("sleep 2");
				cao:
				system("cls");
				continue;
			}
			if(type==3)
			{
				std::string name;
				puts("输入姓名:");
				std::cin>>name;
				courses.display();
				students.displayStudent(name);
				continue;
			}
			if(type==4)
			{
				courses.display();
				for(int i=0,sz=students.getNum();i<sz;i++)
				{
					students.displayStudent(i);
				}
				continue;
			}
			puts("输入错误");
			system("sleep 2");
		}
	}
};

int main()
{
	FrontPage p;
	p.run();
	return 0;
}

posted @ 2019-12-15 17:57  wawcac  阅读(2891)  评论(0编辑  收藏  举报