C++:学生成绩管理系统

学生成绩管理系统

大一下册课程设计,瞎勾八画的流程图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

学习参考文章:https://blog.csdn.net/LYK13469998283/article/details/114712238使用容器存储一步到位

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<Windows.h>
using namespace std;
class Student		//学生类
{
public:
	Student( string name, int id, string sex)
	{
        this->name = name;
		this->id = id;
		this->sex = sex;
	}
	string name;
	int id;
	string sex;
};

class Grade			//课程类
{
public:
	string English;
	string Math;
	string C_plus;
	string Analog_circuit;
	string Sports;
	string Matlab;
	string Data_structure;
	string Physical;
};

class Score : public Grade,virtual public Student	//成绩类
{
public:
	Score(string name, int id, string sex, float English_score, float Math_score, float C_plus_score, float Analog_circuit_score, float Sports_score, float Matlab_score, float Data_structure_score, float Physical_score) :Student(name, id, sex)
	{
		this->English_score = English_score;
		this->Math_score = Math_score;
		this->C_plus_score = C_plus_score;
		this->Analog_circuit_score = Analog_circuit_score;
		this->Sports_score = Sports_score;
		this->Matlab_score = Matlab_score;
		this->Data_structure_score = Data_structure_score;
		this->Physical_score = Physical_score;
		GPA = (English_score + Math_score + C_plus_score + Analog_circuit_score + Sports_score + Matlab_score + Data_structure_score + Physical_score) / 8.0;
	}
	float English_score;
	float Math_score;
	float C_plus_score;
	float Analog_circuit_score;
	float Sports_score;
	float Matlab_score;
	float Data_structure_score;
	float Physical_score;
	float GPA;


};
class stusystem
{
public:
	stusystem()    //打开文件
	{
		ifstream ifs("学生成绩.txt", ios::in);
		if (ifs.is_open() != NULL)
		{
			string name, sex;
			int id;
			float English_score;
			float Math_score;
			float C_plus_score;
			float Analog_circuit_score;
			float Sports_score;
			float Matlab_score;
			float Data_structure_score;
			float Physical_score;
			float GPA;    //说实话你设的这变量名真的长
			while (ifs >> name >> id >> sex >> English_score >> Math_score >> C_plus_score >> Analog_circuit_score >> Sports_score >> Matlab_score >> Data_structure_score >> Physical_score)
			{
				Score s(name , id , sex , English_score , Math_score , C_plus_score , Analog_circuit_score , Sports_score , Matlab_score , Data_structure_score , Physical_score);
				a.push_back(s);
			}
		}
		else
		{
			cout << "不存在该文件" << endl;

		}
	}
	vector<Score>a;
	void Save();       //保存
	void Add();        //添加
	void AddAgain();   //再度添加
	void Del();        //删除
	void Find();       //查找菜单
	void Findname();   //查找名字
	void Findid();     //查找学号
	void Sort();       //排序
	void Remake();     //重新编辑
	void Print();      //打印
	void mainmenu()    //主菜单
	{
		system("cls");
		cout << "____________________________________________" << endl;
		cout << "|                                          |" << endl;
		cout << "|    添加学生信息                     1    |" << endl;
		cout << "|    查找学生信息                     2    |" << endl;
		cout << "|    GPA排名                          3    |" << endl;
		cout << "|    修改学生信息                     4    |" << endl;
		cout << "|    删除学生信息                     5    |" << endl;
		cout << "|    打印学生信息                     6    |" << endl;
		cout << "|    退出系统                         0    |" << endl;
		cout << "|                                          |" << endl;
		cout << "--------------------------------------------" << endl;
		cout << "请输入你的选择:";
		int x;
		cin >> x;
		switch (x)
		{
		case 1:Add(); break;
		case 2:Find(); break;
		case 3:Sort(); break;
		case 4:Remake(); break;
		case 5:Del(); break;
		case 6:Print(); break;
		case 0:Save(); break;
		default:cout << "输入错误,请重新输入\n"; Sleep(1000); mainmenu(); break;
		}
	}
};

void stusystem::Save()  //保存文件
{
	ofstream ofs("学生成绩.txt", ios::out);
	for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
	{
		ofs << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t" << (*i).English_score << "\t" <<
			(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
			<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t" << (*i).GPA;
	}
	ofs.close();
}

void stusystem::Add()  //添加
{
	system("cls");
	int flag;
	
	cout << "请输入学生信息:" << endl;
	string name, sex;
	int id;
	float eng, math, cplus, mndl, ty, matlab, sjjg, dw;
	while (1)
	{
		cout << "姓名:";
		cin >> name;
		
		cout << "学号:";
		cin >> id;
		for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
		{
			if ((*i).id == id)
			{
				cout << "此同学信息已经存在,请重新输入";
				Sleep(700);
				Add();
			}
		}
		cout << "性别:";
		cin >> sex;
		do {
			flag = 0;
			cout << "英语学分:";
			cin >> eng;
			if (eng > 5.0 || eng < 0) {
				cout << " 对不起,请输入0-5.0之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "高数学分:";
			cin >> math;
			if (math > 5.00 || math < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "C++学分:";
			cin >> cplus;
			if (cplus > 5.00 || cplus < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "模拟电路学分:";
			cin >> mndl;
			if (mndl > 5.00 || mndl < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "体育学分:";
			cin >> ty;
			if (ty > 5.00 || ty < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "Matlab学分:";
			cin >> matlab;
			if (matlab > 5.00 || matlab < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "数据结构学分:";
			cin >> sjjg;
			if (sjjg > 5.00 || sjjg < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		do {
			flag = 0;
			cout << "大学物理学分:";
			cin >> dw;
			if (dw > 5.00 || dw < 0) {
				cout << " 对不起,请输入0-5.00之间的数字!!\n";
			}
			else {
				flag = 1;
			}
		} while (flag == 0);
		Score s(name, id, sex, eng, math, cplus, mndl, ty, matlab, sjjg, dw);
		a.push_back(s);  //vector头文件中的push_back操作符,意思是插入到表尾
		Save();
		cout << "添加成功" << endl;
		AddAgain();	
	}
}
void stusystem::AddAgain()
{
        cout << "是否继续添加学生成绩信息?(y/n)" << endl;
        char sign = '0';
		cin >> sign;
		switch (sign) {
		case'y':
		case'Y':
			Add(); break;
		case'n':
		case'N':
			mainmenu(); break;
		default:cout << "输入错误,请重新输入\n"; Sleep(1000); AddAgain(); break;
		}
}
void stusystem::Find()
{
	system("cls");
	cout << "查找学生成绩:" << endl;
	cout << "请选择查询方式:" << endl;
	cout << "1-姓名查询" << endl << "2-学号查询" << endl;
	int x = 0;	
	cin >> x;
	switch (x)
	{
	case 1:
		Findname(); break;
	case 2:
		Findid(); break;
	}
}
void stusystem::Findname()
{
	
	string s; 
	bool flag = false;         //异常解决方案
	cout << "请输入需查找的学生姓名:";
	cin >> s;
	for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
	{
		if ((*i).name == s)
		{
			flag = true;
			cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
				<< "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
				<< "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
			cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
				(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
				<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
			cout << "\n按任意键返回主菜单"; char ch;
			cin >> ch; mainmenu();
		}
		if (flag == false)
		{
			cout << "查无此人!" << endl;
			Findname();
		}
	}
}
void stusystem::Findid()
{
	int y=0;
	bool flag = false;         //异常解决方案
	cout << "请输入需查找的学生学号:";
	cin >> y;
	for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
	{
		if ((*i).id == y)
		{
			flag = true;
			cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
				<< "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
				<< "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
			cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
				(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
				<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
			cout << "\n按任意键返回主菜单"; char ch;
			cin >> ch; mainmenu();
		}
		if (flag == false)
		{
			cout << "查无此人!" << endl;
			Findid();
		}
	}
}

void stusystem::Del()   //删除
{
	cout << "请输入学生的学号" << endl;
	int id;
	cin >> id;
	for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
	{
		if ((*i).id == id)
		{
			int select;
			cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
				<< "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
				<< "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
			cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
				(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
				<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
			cout << "是否删除?" << endl;
			cout << "1.是" << "   " << "2.否" << endl;
			cin >> select;
			switch (select)
			{
			case 1:
				a.erase(i);  //以string.h为头文件的删除函数
				cout << "删除成功" << endl; Sleep(100); mainmenu(); break;
			case 2:
				cout << "您未删除" << endl; Sleep(100); mainmenu(); break;
			default:cout << "输入错误,将返回主菜单\n"; Sleep(1000); mainmenu(); break;
			}
			if ((*i).id != id)
			{
				cout << "查无此人" << endl;
			}

		}
	}
	Save();
	cout << "\n按任意键返回主菜单"; char ch;
	cin >> ch; Sleep(500); return;
}
void stusystem::Remake()
{
	system("cls");
	cout << "修改学生成绩:" << endl;
	cout << "请输入要修改的学生学号:";
	int id;
	cin >> id;
	for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
	{
		if ((*i).id == id)
		{
			cout << "已找到该学生,请重新输入该学生的信息" << endl;
			int ID;
			string name, sex;
			float eng, math, cplus, mndl, ty, matlab, sjjg, dw;
			cout << "姓名:"; cin >> name;
			cout << "学号:"; cin >> id;
			cout << "性别:"; cin >> sex;
			cout << "英语:"; cin >>eng;
			cout << "高数:"; cin >> math;
			cout << "C++:"; cin >>cplus;
			cout << "模拟电路:"; cin >> mndl;
			cout << "体育:"; cin >> ty;
			cout << "Matlab:"; cin >> matlab;
			cout << "数据结构:"; cin >> sjjg;
			cout << "大学物理:"; cin >> dw;
			char c;
			cout << "是否保存修改?(y/n)";
			cin >> c;
			switch (c) {
			case'y':
			case'Y':
				(*i).name = name; (*i).id = id; (*i).sex = sex; (*i).English_score = eng; (*i).Math_score = math;
				(*i).C_plus_score = cplus; (*i).Analog_circuit_score = mndl; (*i).Sports_score = ty; (*i).Physical_score = dw; break;
			case'n':
			case'N':
				 break;
			default:cout << "输入错误,将返回主菜单\n"; Sleep(1000); mainmenu(); break;

			}
		}
	}
	Save();
	cout << "\n按任意键返回主菜单"; char ch;
	cin >> ch; Sleep(500); return;
}
bool mysort(Score a,Score b)//自定义排序规则
{
	if (a.GPA == b.GPA) {
		return a.id > b.id;         //若gpa相同则按学号从小到大排列
	}
	else {
		return a.GPA > b.GPA;       //gpa从大到小排
	}
}
void stusystem::Sort()
{
	system("cls");
	if (a.size() == 0)
		cout << "文件不存在" << endl;
	else {
		sort(a.begin(), a.end(), mysort);   //排序函数从向量表头至表尾使用自定义mysort规则
		cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
			<< "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
			<< "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
		for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
		{
			cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
				(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
				<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
		}
		cout << "\n按任意键返回主菜单"; char ch;
		cin >> ch; Sleep(500); return;
	}
}
void stusystem::Print()
{
	system("cls");
	if (a.size() == 0)
		cout << "文件不存在" << endl;
	else {
		cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
			<< "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
			<< "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
		for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
		{
			cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
				(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
				<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
		}
	}
	cout << "\n按任意键返回主菜单"; char ch;
	cin >> ch; Sleep(500); return;
}
int main()
{
	stusystem stu;
	while (1)
	{
		stu.mainmenu();
	}
	return 0;
}
相当不错的一个成绩管理系统 #include #include #include #include using namespace std; enum {SUBJECT=5};//一共五门 typedef struct { char subject[10];//科目名称 int score;//科目成绩 }markinfo; typedef struct studentnode { markinfo mark[SUBJECT]; int totalmark; char name[10];//学生姓名 studentnode * next; }studentnode; class student { studentnode * head; public: student(); int addstudent(); ~student(); int countmark(); int sortbymark(); int save(); int show(); int display(); int readfiletolist(); int searchbyname(); }; student::student() //用构造函数来初始化。 { head=new studentnode; head->next=NULL; } //1.输入学生姓名、成绩等数据,并保存在链表中。 int student::addstudent() { studentnode * p; int i; char check; system("cls"); cout<<"**********************"<<endl; cout<<"请输入学生信息:"<<endl; do { p=new studentnode; cin.ignore(); cout<name); i=0; p->totalmark=0; do { cout<mark[i].subject); cout<>p->mark[i].score; } while(p->mark[i].score>100||p->mark[i].scoretotalmark=p->totalmark+p->mark[i].score; getchar(); } while(++i!=SUBJECT); if(head->next==NULL) { head->next=p;p->next=NULL; } else { p->next=head->next; head->next=p; } cout<next; if(p==NULL) { cout<<"没有学生,请重新输入"<<endl;system("pause");return 0; } else { cout<<"***************"<<endl; cout<<"学生成绩汇总:"<<endl; while(p) { cout<<"姓名:"<name<<" 总成绩:"<totalmark<next; } } system("pause"); return 0; } //4.输出所有学生成绩到一个文件中。 int student::save() { char address[35]; int i; studentnode * p=head->next; cout<<"请输入保存的地址"<<endl; cin.ignore(); gets(address); ofstream fout; fout.open(address,ios::app|ios::out); while(p) { fout<<"*"; fout<name<<"*"; i=0; while(i!=SUBJECT) { fout<mark[i].subject<<"*"; fout<mark[i].score; i++; } //fout<next; } fout.flush(); fout.close(); cout<next; while(p) { s=p->next; delete p; p=s; } delete head; } //3.按照总成绩大小对记录进行排序 int student::sortbymark() { studentnode *move1=head->next; studentnode *move2,*max,*pre1,*pre2,*maxpre,*s=move1; if(head->next==NULL) { cout<<"没有记录,请添加"<next!=NULL;pre1=move1,maxpre=pre1,move1=move1->next,max=move1) { for(pre2=move1,move2=move1->next;move2!=NULL;pre2=move2,move2=move2->next) if(move2->totalmark>max->totalmark) { maxpre=pre2; max=move2; } if(move1->next==max) //交换max和move1。 { pre1->next=max; move1->next=max->next; max->next=move1; move1=max; } else { s=move1->next; move1->next=max->next; max->next=s; maxpre->next=move1; pre1->next=max; move1=max; } } cout<<"已经按照从大到小排序"<next; int i; if(head->next==NULL){cout<<"没有学生记录,请添加"<<endl;system("pause"); return 0;} else { while(p) { cout<<"姓名:"<name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<mark[i-1].subject; cout<<" 成绩:"<mark[i-1].score; i++; } cout<next; } } system("pause"); return 0; } //6:从文件按读取记录 int student::display() { ifstream fin; char buf[100]; char str[25]; cout<<"请输入路径及文件名:"<<endl; cin.ignore(); gets(str); fin.open(str); if(!fin) { cout<<"没有此文件"<<endl; system("pause"); return 0; } while(fin) { fin.getline(buf,sizeof(buf)); cout<<buf<<endl; } system("pause"); return 0; } //8从文件中读取数据,并将数据保存在链表中 int student::readfiletolist() { ifstream fin; int i; char str[25]; cout<<"请输入路径及文件名:"<<endl; cin.ignore(); gets(str); fin.open(str); if(!fin) { cout<<"没有此文件"<totalmark=0; fin.getline(p->name,100,'*'); i=0; while(i!=SUBJECT) { fin.getline(p->mark[i].subject,100,'*'); fin>>p->mark[i].score; p->totalmark+=p->mark[i].score; i++; } if(head->next==NULL) { head->next=p; p->next=NULL; } else { p=head->next; head->next=p; } } cout<<"信息已经保存在链表中"<next==NULL) { cout<<"没有学生,请添加或者从文件中读取"<next; char findname[10]; int i; cout<name,findname)) { cout<<"经查找,找到该生信息如下:"<<endl<<endl; cout<<"姓名:"<name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<mark[i-1].subject; cout<<" 成绩:"<mark[i-1].score; i++; } cout<next; } cout<<"没有此学生,请添加或者从文件中读取"<<endl; system("pause"); return 0; } int showmenu() { int choice; char * menu[9]={ "1:输入学生成绩保存到链表\n", "2:计算每位学生总成绩\n", "3:按照总成绩大小对记录进行排序\n", "4:输出所有学生成绩到一个文件中\n", "5:显示新输入的学生信息\n", "6:从文件中读取信息\n", "7:将文件信息保存在链表中\n", "8:根据姓名查找学生记录\n", "9:结束程序\n" }; cout<<" "<<"*****************************************************"<<endl; cout<<" *"<<" "<<"学生成绩管理系统"<<" *"<<endl; cout<<" "<<"*****************************************************"<<endl; for(choice=0;choice<9;choice++) cout<<" "<<menu[choice]; cout<<" "<<"*****************************************************"<<endl; cout<<"please choose to continue"<>choice; } while(choice>9||choice<1); return choice; } int main() { int menuitem,flag=1; student stu; while(flag) { system("cls"); menuitem=showmenu(); switch(menuitem) { case 1:{stu.addstudent();break;} case 2:{stu.countmark();break;} case 3:{stu.sortbymark();break;} case 4:{stu.save();break;} case 5:{stu.show();break;} case 6:{stu.display();break;} case 7:{stu.readfiletolist();break;} case 8:{stu.searchbyname();break;} case 9:{flag=0;break;} } } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SHkko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
>