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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM