学生成绩管理系统(C++)

学生成绩管理系统

一、问题描述
设计一个学生成绩管理程序,实现按班级完成对学生成绩信息的录入和修改,并用文件保存。实现按班级输出学生的成绩单;实现按学号和姓名进行查询,按平均成绩进行排序功能。
二、基本要求
(1)使用面向对象编程思想编写开发过程中需要用到的类,比如:Person 类,学生类继
承Person类,管理类实现对成绩信息的操作。
(2)输入和输出可以使用文本文件重定向输入(保存数据为磁盘文件);也可以使用标
准输入输出进行(提交时需要提交TXT格式输入数据)。比如 score.txt 的文件(包含 20 个 以上记录),包含数据信息有:学号(长度为10位,不允许重复)、姓名、班级(至少有 2 个)、 课程成绩(至少 3 门);在运行程序时自动载入,若文件不存则创建一个空文件。平均成绩 不作为类成员,只是在程序运行时产生,也不须保存。
平均成绩设置独立的计算函数,对文件的所有数据进行遍历,将每个人三门课程的成绩累加起来然后除以课程数最终得到每个人的平均成绩,在每次输出时,将平均成绩同时输出出来,但在文件保存时对平均成绩不保存。
(3)程序运行时显示菜单功能,按班录入成绩,查询个人成绩,修改成绩,按班输出成绩
单,按平均分排序显示。
按平均分排序显示需要对所有同学的平均成绩进行比较,然后排序,初步考虑在遍历的过程中进行比较,然后借助中间变量进行排序,后来查找资料发现头文件下有sort专门的排序函数,不过默认的是从小到大的排序,因此需要对排序方式进行定义。
(4)基本功能要求具有增、删、改、查。

基本流程图
在这里插入图片描述

1)score.h
#include <iostream>
#include <string>//为了使用string类 
#include <vector>//vector数组的头文件 
#include <fstream>//文件操作的头文件 
#include<conio.h>//使用getch()函数
#include<algorithm>//使用库函数sort进行排序 
using namespace std; 
class person
{
	public:
		string Number;//学号 
		string Name;//姓名 
		string Class;//班级 
		person(){} //缺省构造函数 
		~person(){}//析构函数 
} ;
class student:public person//学生类,继承person类 
{
	public:
		double Lesson_1;//课程1 
		double Lesson_2;//课程2 
		double Lesson_3; //课程3 
	friend istream& operator>>(istream& in, student & t)//重载>>操作符
	{
		in>>t.Number>>t.Name>>t.Class>>t.Lesson_1>>t.Lesson_2>>t.Lesson_3;
	 } 
	friend ostream& operator<<(ostream& out, const student& t)//重载<<操作符 
	{
		out<<t.Number<<"\t"<<t.Name<<"\t"<<t.Class<<"\t"<<t.Lesson_1<<"\t"<<t.Lesson_2<<"\t"<<t.Lesson_3<<endl; 
	}
};
class Admin//管理类 
{
	public:
		vector<student>stu;//vector数组 
		void AddScore();//添加成绩  
		void ShowScore();//显示成绩 
		void ChangeScore();//修改成绩 
		void ShowClass();//按班输出成绩单
		void DelScore();//删除学生成绩 
		void SearchScore();//查找学生成绩 
		void ShowSort();//按平均分排序 
		int GetScore();//获取文件中的学生成绩 
		void Keep();//保存为文件数据 
};
(2)main.cpp
#include"score.h"
void Admin::AddScore()//添加 成绩 
{
	student s; //定义student类的对象 
	cout<<"Input Number: "; cin>>s.Number; 
	cout<<"Input Name: "; cin>>s.Name; 
	cout<<"Input Class: "; cin>>s.Class; 
	cout<<"Input学生课程1的成绩: "; cin>>s.Lesson_1; 
	cout<<"Input学生课程2的成绩: "; cin>>s.Lesson_2; 
	cout<<"Input学生课程3的成绩: "; cin>>s.Lesson_3; 
	stu.push_back(s);
	cout<<"学生成绩添加成功!"<<endl; 
}
void Admin::ShowScore()//显示所有学生的成绩 
{
	vector<student>::iterator iter; //定义学生Vector数组的迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();iter++)//对vector数组进行遍历,调用begin()和end()函数进行 
	{
		cout<<"Number: "<<iter->Number<<"   Name:  "<<iter->Name<<"  Class:  "<<iter->Class<<"  课程1:  "<<iter->Lesson_1
		<<"  课程2:  "<<iter->Lesson_2<<"  课程3:  "<<iter->Lesson_3<<"  平均分:  "<<(iter->Lesson_1+iter->Lesson_2+iter->Lesson_3)/3<<endl;
		}	
}
void Admin::ChangeScore()//修改学生成绩信息 
{
	if(stu.empty())//判断数组是否有学生成绩信息, 
	{
		cout<<"暂无学生成绩信息,无法修改"<<endl;
		return;  //返回主函数 
	}
	string _name;
	cout<<"Input Name: ";cin>>_name;
	vector<student>::iterator iter; //定义学生Vector数组的迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();)//对vector数组进行遍历,调用begin()和end()函数进行 
	{
		if(iter->Name==_name)    //根据输入的Name找到成绩信息 
		{
			cout<<"Input Number: ";cin>>iter->Number;
			cout<<"Input Class: ";cin>>iter->Class; 
			cout<<"Input 课程1的成绩: ";cin>>iter->Lesson_1; 
			cout<<"Input 课程2的成绩: ";cin>>iter->Lesson_2; 
			cout<<"Input 课程3的成绩: ";cin>>iter->Lesson_3; 
			this->Keep();  //调用keep函数将修改后的数据保存 
			cout<<"修改成功!"<<endl; 
		}
		else
			iter++; 
	}
}
void Admin::ShowClass()//按Class输出成绩单 
{
	string _Class;
	cout<<"Input  Class: "; cin>>_Class;
	if(Class!="网络1901"||Class!="网络1902") 
	{
		cout<<"请在网络1901和网络1902两个班之间选择"<<endl'
		return;' 
	}
	vector<student>::iterator iter; //定义学生Vector数组的迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();iter++)//对vector数组进行遍历,调用begin()和end()函数进行 
	{
		if(iter->Class==_Class)
		{
		cout<<"Number: "<<iter->Number<<"   Name:  "<<iter->Name<<"  Class:  "<<iter->Class<<"  课程1:  "<<iter->Lesson_1
		<<"  课程2:  "<<iter->Lesson_2<<"  课程3:  "<<iter->Lesson_3<<"  平均分:  "<<(iter->Lesson_1+iter->Lesson_2+iter->Lesson_3)/3<<endl;}
	}
} 
void Admin::DelScore()//删除学生成绩 
{
	if(stu.empty())//判断vector数组是否有学生成绩信息, 
	{
		cout<<"暂无学生成绩信息,无法修改"<<endl;
		return;  //返回主函数 
	}
	string _name;
	cout<<"Input  Name:  " ;cin>>_name;
	vector<student>::iterator iter; //定义学生Vector数组的迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();)//对vector数组进行遍历,调用begin()和end()函数进行 
	{
		if(iter->Name==_name){
			stu.erase(iter);//从vector数组中删除这条学生信息 
			this->Keep();//保存学生信息 
		}
	}
} 
void Admin::SearchScore()//查询学生成绩信息 
{
	cout<<"     1、按 姓 名 查 找"<<endl;
	cout<<"     2、按 学 号 查 找  "<<endl; 
	int n;
	cout<<"请选择-> "; cin>>n;
	switch(n)
	{
		case 1:
			{
				string _name;
				cout<<"Input  Name: "; cin>>_name;
				vector<student>::iterator iter; //定义学生Vector数组的迭代器 
				for(iter=this->stu.begin();iter!=this->stu.end();iter++)//对vector数组进行遍历,调用begin()和end()函数进行 
				{
					if(iter->Name==_name)
					{
					cout<<"Number: "<<iter->Number<<"   Name:  "<<iter->Name<<"  Class:  "<<iter->Class<<"  课程1:  "<<iter->Lesson_1
					<<"  课程2:  "<<iter->Lesson_2<<"  课程3:  "<<iter->Lesson_3<<"  平均分:  "<<(iter->Lesson_1+iter->Lesson_2+iter->Lesson_3)/3<<endl;}
				}
			}break;
		case 2:
			{
				string _number;
				cout<<"Input  Number: "; cin>>_number;
				vector<student>::iterator iter; //定义学生Vector数组的迭代器 
				for(iter=this->stu.begin();iter!=this->stu.end();iter++)//对vector数组进行遍历,调用begin()和end()函数进行 
				{
					if(iter->Number==_number)
					{
					cout<<"Number: "<<iter->Number<<"   Name:  "<<iter->Name<<"  Class:  "<<iter->Class<<"  课程1:  "<<iter->Lesson_1
					<<"  课程2:  "<<iter->Lesson_2<<"  课程3:  "<<iter->Lesson_3<<"  平均分:  "<<(iter->Lesson_1+iter->Lesson_2+iter->Lesson_3)/3<<endl;}
				}	
			}
	} 
 }
bool operator < (student &A,student &B)//定义排序规则 
{
	return (A.Lesson_1+A.Lesson_2+A.Lesson_3)/3 > (B.Lesson_1+B.Lesson_2+B.Lesson_3)/3;
}
void Admin::ShowSort()//按平均分排序 
{
	sort(stu.begin(),stu.end());//调用库函数sort 
	vector<student>::iterator iter;//定义迭代器 
	for (iter = this->stu.begin(); iter != this->stu.end();iter++ )//对vector数组进行循环 
	{		
		cout<<"Number: "<<iter->Number<<"   Name:  "<<iter->Name<<"  Class:  "<<iter->Class<<"  课程1:  "<<iter->Lesson_1
		<<"  课程2:  "<<iter->Lesson_2<<"  课程3:  "<<iter->Lesson_3<<"  平均分:  "<<(iter->Lesson_1+iter->Lesson_2+iter->Lesson_3)/3<<endl;
	}
 } 
int Admin::GetScore()//从文件中获取学生成绩数据 
{
	fstream readfile("score.txt");//打开文件操作 
	int rows = 0;
	if (readfile)//先判断文件是否存在,如果文件存在
	{
		student s;
		if (!(readfile >>s.Number>>s.Name>>s.Class>>s.Lesson_1>>s.Lesson_2>>s.Lesson_3))//如果第一次读取为空
		{
			cout << "文件为空"<<endl;
			return 1;
		}
		stu.push_back(s); //push_back将文件中的数据存入Vector数组中 
		rows++;
		while(readfile >>s.Number>>s.Name>>s.Class>>s.Lesson_1>>s.Lesson_2>>s.Lesson_3)//读取后存入vector容器中
		{
			rows++;
			stu.push_back(s); //vector数组第要调用 push_back函数进行读入 
		}
		cout<<"数据读取成功!"<<endl; 
		readfile.close();//文件操作完成后,需要关闭文件 
		return rows;
	}
	else
	{
		ofstream outfile;//磁盘中不存在文件的话则创建
		outfile.open("score.txt");//创建通讯录文件 
		if (!outfile.is_open()) //未能打开新创建的文件 
		{
			cout << "文件没有创建!"<<endl;
			return -1;
		}
		else	cout<<"文件为空"<<endl; 
		outfile.close();//文件操作完成后,需要关闭文件 
	}
	return 2;
 }
void Admin::Keep()//保存 
 {
 	ofstream outfile("score.txt");//打开文件 
	vector<student>::iterator iter;//vector数组保存的磁盘文件 
	for (iter=this->stu.begin(); iter!=this->stu.end();iter++) //vector数组的头部要用begin(),尾部要用end(),从头到尾利用迭代器进行遍历
	{
		outfile<<iter->Number<<"\t"<<iter->Name<<"\t"<<iter->Class<<"\t"<<iter->Lesson_1<<"\t"<<iter->Lesson_2<<"\t"<<iter->Lesson_3<<endl;
	}
	outfile.close(); //关闭文件 
} 
int main()
{
	int n;
	Admin a;//定义对象 
	a.GetScore();//从文件中读取数据 
	do{
		cout<<endl<<" 学生成绩管理系统  "<<endl;
		cout<<"1、添加学生成绩   " <<endl;  
		cout<<"2、显示学生成绩   " <<endl; 
		cout<<"3、修改学生成绩   " <<endl; 
		cout<<"4、按班级显示学生成绩" <<endl; 
		cout<<"5、删除学生成绩   " <<endl; 
		cout<<"6、按平均分排序学生成绩" <<endl; 
		cout<<"7、查询学生个人成绩" <<endl; 
		cout<<"8、保存并退出   " <<endl; 
		cout<<"请选择 " ;cin>>n;
		switch(n)
		{
			case 1: a.AddScore();getch();break;
			case 2: a.ShowScore();getch();break;
			case 3: a.ChangeScore();getch();break;
			case 4: a.ShowClass();getch();break;
			case 5: a.DelScore();getch();break;
			case 6: a.ShowSort();getch();break;
			case 7: a.SearchScore(); getch();break;
			case 8: exit(0);
		 } 
	}while(1);
	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; }
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值