⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xueshengxinxiguanli.cpp

📁 学生成绩管理系统c++ 实现键盘录入文件添加数据
💻 CPP
字号:
#include <fstream.h>
#include <string.h>


class Student{

public:
	int Class,num;
	char name[8];
	float elec,cpp,math,eng,sport,polity,ave;
	int order;
	Student *next;

public:
	Student() {}
    Student(int c1,int n1,char*n,float e1,float c2,float m,float e2,float s,float p,float a,
		int o,Student *next=NULL)
	{
		Class=c1;num=n1;
		strcpy(name,n);
		elec=e1;cpp=c2;math=m;eng=e2;sport=s;polity=p;ave=a;
		order=o;
		this->next=next;
	}
    
	
	
	friend int Valid(float score)
	{ 
		return (score<0||score>100) ?0:1;
	}
	
	
	friend void SetOrder(Student*head)
	{ 
		int order=1;
		while(head)
		{head->order=order++;head=head->next;}
	}
	
	
	friend Student* Insert(Student *head,Student *p)  //在head所指的链表中降序插入结点p
	{ 
		Student*p1,*p2;
		if(head==0)
		{
			head=p;p->next=0;
		}
		else if(head->ave<=p->ave)
		{
			p->next=head;head=p;
		}
		else
		{
			p2=p1=head;
			while(p2->next&&p2->ave>p->ave)
			{
				p1=p2;p2=p2->next;
			}
			if(p2->ave>p->ave)
			{
				p2->next=p;p->next=0;
			}
			else {
				p->next=p2;p1->next=p;
			}
		}
		return head;
	}
	
	
	
	friend Student *Create(Student *head,istream& in)
	{
		Student *p;
		int Class,num;
		char name[8];
		float elec,cpp,math,eng,sport,polity;
		if(&in==&cin)
			cout<<"\n\n请输入学生数据(输入成绩非法,则结束),数据输入格式为:\n"
			<<"班级 姓名 学号 电子 C++ 数学 英语 政治 体育\n";
		in>>Class>>name>>num>>elec>>cpp>>math>>eng>>polity>>sport;
		while(Valid(elec)&&Valid(cpp)&&Valid(math)&&Valid(eng)&&Valid(sport)&&Valid(polity))
		{p=new Student;
		p->Class=Class;p->num=num;strcpy(p->name,name);
		p->elec=elec;p->cpp=cpp;p->math=math;
		p->eng=eng;p->polity=polity;p->sport=sport;
		p->ave=(elec+cpp+math+eng+sport+polity)/6;
		head=Insert(head,p);
		in>>Class>>name>>num>>elec>>cpp>>math>>eng>>polity>>sport;
		}
		SetOrder(head);   //设置排名
		return head;
	}
	
	
	
	friend const Student * Lookup(const Student *head,int num)  //查找指定学号为num的结点
	{ 
		while(head && head->num!=num)
			head=head->next;
		return head;
	}
	
	
	friend void OutputOne(const Student* head)  //输出一个学生数据
	{
		cout<<head->Class<<'\t'<<head->name<<'\t'<<head->num<<'\t'
			<<head->elec<<'\t'<<head->cpp<<'\t'<<head->math<<'\t'
			<<head->eng<<'\t'<<head->polity<<'\t'<<head->sport<<'\t'
			<<head->order<<endl;
	}
	
	
	friend void OutputAll(const Student*head)  //输出所有学生的数据
	{
		if(!head) {cout<<"\n\n\t\t没有任何学生数据!\n\n"; return;}
		cout<<"\n\n\t\t学生成绩表\n\n";
		cout<<"班级\t姓名\t学号\t电子\tC++\t数学\t英语\t政治\t体育\t名次\n";
		while(head)
		{
			OutputOne(head);head=head->next;
		}
	}
	
	
	friend Student *Modify(Student *head,int num)  //修改学号为学生的数据
	{
		Student *p1=head,*p2=p1;
		while(p2&&p2->num!=num)  //寻找待修改的结点
			p1=p2,p2=p2->next;
		if(p2)  //修改指定结点数据
		{
			cout<<"\n\n请输入新数据,格式为:\n"
				<<"班级 姓名 学号 电子 C++ 数学 英语 政治 体育\n";
			cin>>p2->Class>>p2->name>>p2->num>>p2->elec>>p2->cpp>>p2->math
				>>p2->eng>>p2->polity>>p2->sport;
			while(!Valid(p2->elec)||!Valid(p2->cpp)||!Valid(p2->math)||!Valid(p2->eng)
				||!Valid(p2->sport)||!Valid(p2->polity))
			{
				cout<<"\n\n成绩数据非法!请重新输入,格式为:\n"
					<<"班级 姓名 学号 电子 C++ 数学 英语 政治 体育\n";
				cin>>p2->Class>>p2->name>>p2->num>>p2->elec>>p2->cpp>>p2->math
					>>p2->eng>>p2->polity>>p2->sport;
			}
			p2->ave=(p2->elec+p2->cpp+p2->math+p2->eng+p2->polity+p2->sport)/6;
			//将修改的指定结点从原链表上修改下来,并重新降序插入原链表
			if(p2==p1)
				head=Insert(p2->next,p2);
			else
			{
				p1->next=p2->next;  
				head=Insert(head,p2);
			}
			SetOrder(head);
		}
		else cout<<"没找到指定学生!\n";
		return head;
	}
	
	
	
	friend Student *DeleteStudent(Student *head,int num)
	{
		Student *p1=head,*p2=p1;
		while(p2&&p2->num!=num)
			p1=p2,p2=p2->next;
		if(p2)
		{
			if(p2==p1)
			{
				head=head->next;delete p1;
			}
			else 
			{
				p1->next=p2->next;delete p2;
			}
			cout<<"已删除"<<num<<"号学生数据\n";
			SetOrder(head);
		}else cout<<"没找到指定学生!\n";
		return head;
	}
	
	
	
	friend void Statistic(const Student *head)
	{
		int i=0;
		float ave_elec=0,
			ave_cpp=0,
			ave_math=0,
			ave_eng=0,
			ave_polity=0,
			ave_sport=0;
		while(head)
		{
			ave_elec+=head->elec;
			ave_cpp+=head->cpp;
			ave_math+=head->math;
			ave_eng+=head->eng;
			ave_polity+=head->polity;
			ave_sport+=head->sport;
			i++;head=head->next;
		}
		if(!i)
		{
			cout<<"\n\n没有任何学生数据!\n";return;}
		cout<<"\n\n\t\t各门课程平均成绩表\n\n";
		cout<<"电子\tC++\t数学\t英语\t政治\t体育\n";
		cout<<ave_elec/i<<'\t'<<ave_cpp/i<<'\t'<<ave_math/i<<'\t'
			<<ave_eng/i<<'\t'<<ave_polity/i<<'\t'<<ave_sport/i<<endl;
	}
	
	
	
	
	friend void DeleteChain(Student *head)
	{Student *p;
	while(head)
	{
		p=head;head=head->next;delete p;
	}
	}
	
	
	
	friend void SaveAll(Student *head,char*fname)
	{ofstream out(fname);
	if(!out)
	{
		cout<<"\n不能打开"<<fname<<"文件!\n";return;
	}
	while(head)
	{
		out<<head->Class<<'\t'<<head->name<<'\t'<<head->num<<'\t'
			<<head->elec<<'\t'<<head->cpp<<'\t'<<head->math<<'\t'<<head->eng<<'\t'
			<<head->polity<<'\t'<<head->sport<<'\t';
		head=head->next;
	}
	out<<-1<<'\t'<<-1<<'\t'<<-1<<'\t'<<-1<<'\t'<<-1<<'\t'
		<<-1<<'\t'<<-1<<'\t'<<-1<<'\t'<<-1<<endl;
	}
	
	
	
	friend void ShowMenu(void)
	{
		cout<<"\n\n";
		cout<<"\t\t******欢迎使用学生成绩管理系统******\n"
			<<"\t\t*                                  *\n"
			<<"\t\t*    1.从键盘录入与添加数据        *\n"
			<<"\t\t*    2.从文件录入与添加数据        *\n"
			<<"\t\t*    3.修改数据                    *\n"
			<<"\t\t*    4.查询数据                    *\n"
			<<"\t\t*    5.删除数据                    *\n"
			<<"\t\t*    6.显示数据                    *\n"
			<<"\t\t*    7.平均数据                    *\n"
			<<"\t\t*    8.保存数据                    *\n"
			<<"\t\t*                                  *\n"
			<<"\t\t*    0.退出系统                    *\n"
			<<"\t\t**********************************\n\n";
	}
};



void main (void)
{  
	Student *head=0;
	int select;
	while(1)
	{
		ShowMenu();
		cout<<"\t\t请输入你的选择(0~8):";cin>>select;
		switch(select)
		{
		case 0:
			
			DeleteChain(head);
			cout<<"\n\n谢谢您使用本系统!\n\n";
			return;
		case 1:
			head=Create(head,cin);
			break;
		case 2:
			{
				char fname[256];
				cout<<"请输入文件名:";
				cin.get();
				cin.getline(fname,256);
				ifstream in(fname);
				if(!in)
				{
					cout<<"\n不能打开"<<fname<<"文件!\n";break;
				}
				head=Create(head,in);
				break;
			}
		case 3:
			{
				int num;
				cout<<"请输入学号:";
				cin>>num;
				head=Modify(head,num);
			}
			break;
		case 4:
			{
				int num;
				cout<<"请输入学号:";
				cin>>num;
				const Student *t=Lookup(head,num);
				if(t)
				{
					cout<<"\t\t\t\t"<<t->name<<"同学的成绩表\n";
					cout<<"班级\t姓名\t学号\t电子\tC++\t数学\t英语\t政治\t体育\t名次\n";
					OutputOne(t);
				}else cout<<"没有找到指定学生!\n";
				break;
			}
		case 5:
			{
				int num;
				cout<<"请输入学号:";
				cin>>num;
				head=DeleteStudent(head,num);
			}break;
		case 6:
			OutputAll(head);break;
		case 7:
			Statistic(head);break;
		case 8:
			if(head)
			{
				char fname[256];
				cout<<"请输入文件名:";
				cin.get();
				cin.getline(fname,256);
				SaveAll(head,fname);
			}else cout<<"\n\n尚无数据可保存!\n\n";
			break;
		default:
			cout<<"\n\n非法操作!\n\n";
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -