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

📄 xsglxt.cpp

📁 vC++下的学生成绩管理系统。可以保存学生数据》
💻 CPP
字号:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<iomanip.h>
class Student                                        //学生类//
{
public:	
	void sr()                                        //输入数据//
	{
		cout<<"共有多少位学生:";
		cin>>n1;
		for(int i=0;i<n1;i++)
		{
			cout<<setw(20)<<"学号:"<<i+1<<"\n";
			cout<<setw(20)<<"姓名:"; 
			cin>>xm[i]; 
			cout<<setw(20)<<"英语:";
			cin>>yy[i]; 
			cout<<setw(20)<<"数学:";
			cin>>sx[i];
			cout<<setw(19)<<"C++:"; 
			cin>>c[i]; 
			zf[i]=(yy[i]+sx[i]+c[i])/3;
			cout<<setw(20)<<"总分:"; 
			cout<<zf[i]*3<<"\n";
		}
		save();
	}
	void ave()                                 //计算各科和总分的平均值//
	{
		yya=sxa=ca=zfa=0;
		cout<<setw(20)<<"英语平均成绩:"; 
		for(int i=0;i<n1;i++) 
			yya+=int(yy[i]);
		cout<<yya/n1<<"\t";
		cout<<"\n"<<setw(20)<<"数学平均成绩:"; 
		for(i=0;i<n1;i++) 
			sxa+=int(sx[i]);
		cout<<sxa/n1<<"\t";
		cout<<"\n"<<setw(19)<<"c++平均成绩:"; 
		for(i=0;i<n1;i++) 
			ca+=int(c[i]);
		cout<<ca/n1<<"\t";
		cout<<"\n"<<setw(20)<<"总分平均成绩:"; 
		for(i=0;i<n1;i++) 
			zfa+=int(zf[i]);
		cout<<zfa/n1<<"\t";
	}
	void tj(double *p,int n,double pp[])                //计算优良中差的比例//
	{
		for(int i=0;i<5;i++)pp[i]=0; 
		for(i=0;i<n;i++)
		{ 
			if(p[i]>=90&&p[i]<=100) pp[0]+=1;
			else if(p[i]>=80&&p[i]<90) pp[1]+=1; 
			else if(p[i]>=70&&p[i]<80) pp[2]+=1;
			else if(p[i]>=60&&p[i]<70) pp[3]+=1;
			else pp[4]+=1;
		}
	}
	void xktj(double t[])
	{
		double temp=0;
		for(int i=0;i<5;i++) temp+=t[i]; 
		cout<<"优秀:"<<100*t[0]/temp<<"良好:"<<100*t[1]/temp<<"中等:"<<100*t[2]/temp<<"及格:"<<100*t[3]/temp<<"不及格:"<<100*t[4]/temp<<endl;
	}
	void yybl(){cout<<"英语:(百分之)"; xktj(yyt);}
	void sxbl(){cout<<"数学:(百分之)"; xktj(sxt);}
	void cbl(){cout<<"c++:  (百分之)"; xktj(ct);}
	void zfbl(){cout<<"总分:(百分之)";xktj((zft));}
	void qk()                                      //清空所有数据//
	{
		fstream  qkl;
		qkl.open("xm.txt",ios::trunc);
		qkl.close();
		sr();
	}
	void pm()//总分排名
	{	
		double s[100];
		for(int k=0;k<n1;k++)	s[k]=zf[k]; 
		for(int i=1;i<n1;i++)
		for(int j=0;j<n1-i;j++)
			if(s[j]<s[j+1])
			{
				double temp;
			    temp=s[j];
				s[j]=s[j+1];
				s[j+1]=temp;
			}
			for(int q=0;q<100;q++){
				for(int w=0;w<100;w++)
					if(zf[q]==s[w])
						p[q]=w+1;
			}
			for(int op=0;op<n1-1;op++)
				for(int po=op+1;po<n1;po++)
					if(p[op]==p[po])
						p[po]--;
	}
	int htoi(char *tt)                           //把字符转成十进制数字//
	{
		int temp2=0;
		for(int o=0;tt[o]!='\0'&&o<3;o++)
	{
	 temp2=(tt[0]-48)*10+(tt[1]-48);
	}
	return temp2;
	}
	void read()                               //读取数据//
	{   
		char xmmm[400];int count=0;
		fstream infiles;
		infiles.open("xm.txt",ios::in);
		if(!infiles)
		{
			cout<<"wrong";
			abort();
		}
		while(!infiles.eof()){
			while(infiles.getline(xmmm,6))
			{
				if(xmmm[0]=='$')
					count++;
			}
		}
		infiles.close();
		fstream infile;
		infile.open("xm.txt",ios::in);
		if(!infile)
		{
			cout<<"wrong";
			abort();
		}
		n1=count;
		char xmm[400][7];
		int k=0;
		while(!infile.eof())
		{   
			for (int i=0;i<4;i++)
			{
				infile.getline(xmm[i],6);
				if (xmm[i][0]=='\0')
					break;
				switch(i)	
				{
				case 0:{infile.getline(xm[k],6);};break;
				case 1:yy[k]=htoi(xmm[i]);break;
				case 2:sx[k]=htoi(xmm[i]);break;
				case 3:c[k]=htoi(xmm[i]);break;
				default:break;
				}
			}
			zf[k]=(yy[k]+sx[k]+c[k])/3;
			k++;
		}
		infile.close();
	}
	void save()                                    //保存数据//
	{	
		fstream outfile;
		outfile.open("xm.txt",ios::out);
		if (!outfile)
		{
			cout<<"wrong";
			abort();
		}
		for (int o=0;o<n1;o++)
		{
			outfile<<"$\n";
			outfile<<xm[o]<<"\n";
			outfile<<yy[o]<<"\n";
			outfile<<sx[o]<<"\n";
			outfile<<c[o]<<"\n";
		}
		outfile.close();
	}
	void del()                                    //删除学生数据//
	{	
		int tempx=0;
		cout<<setw(30)<<"你想删除哪个学号的成绩?"<<endl;
		cin>>tempx;
		if(tempx<1||tempx>n1) {goto end;}
		else if(tempx==n1){n1--;goto end;}
		else if(tempx>=1&&tempx<n1){
		for(int h=0;h<n1-1;h++){
		yy[tempx-1+h]=yy[tempx+h];
		sx[tempx-1+h]=sx[tempx+h];
		c[tempx-1+h]=c[tempx+h];
        zf[tempx-1+h]=(yy[tempx-1+h]+sx[tempx-1+h]+c[tempx-1+h])/3;
		for(int cc=0;cc<7;cc++)
			xm[tempx-1+h][cc]=xm[tempx+h][cc];
		}
		n1--;
		}
        end:;
		{
		fstream  clg;
		clg.open("xm.txt",ios::trunc);
		clg.close();
		}
    save();
	read();
	pm();
    }
	void add()                                  //添加学生数据//
	{
		int tempz=0;
		cout<<"你想添加几个学生的数据?";
		cin>>tempz;
		for(int j=0;j<tempz;j++){
			cout<<setw(28)<<"学号:"<<n1+1+j<<"\n";
			cout<<setw(28)<<"姓名:"; cin>>xm[n1+j]; cout<<"\n";
			cout<<setw(28)<<"英语:"; cin>>yy[n1+j]; cout<<"\n";
			cout<<setw(28)<<"数学:"; cin>>sx[n1+j]; cout<<"\n";
			cout<<setw(27)<<"C++:"; cin>>c[n1+j]; cout<<"\n";
			zf[n1+j]=(yy[n1+j]+sx[n1+j]+c[n1+j])/3;
			cout<<setw(28)<<"总分:"; 
			cout<<zf[n1+j]*3<<"\n";
		}	
		{
			fstream outfilez;
			outfilez.open("xm.txt",ios::app);
			if (!outfilez)
			{
				cout<<"wrong";
				abort();
			}
			for (int oo=0;oo<tempz;oo++)
			{
				outfilez<<"$\n";
				outfilez<<xm[n1+oo]<<"\n";
				outfilez<<yy[n1+oo]<<"\n";
				outfilez<<sx[n1+oo]<<"\n";
				outfilez<<c[n1+oo]<<"\n";
			}
			outfilez.close();
		}
		n1+=tempz;
		read();
		pm();
	}
	void modify()                                 //修改学生的记录//
	{	
		cout<<"你想修改哪个学号的成绩?";
		int temp;
		cin>>temp;
		cout<<setw(28)<<"学号:"<<temp<<endl;
		cout<<setw(28)<<"姓名:";
		cin>>xm[temp-1]; 
		cout<<setw(28)<<"英语:"; 
		cin>>yy[temp-1]; 
		cout<<setw(28)<<"数学:"; 
		cin>>sx[temp-1]; 
		cout<<setw(27)<<"C++:";
		cin>>c[temp-1]; 
		zf[temp-1]=(yy[temp-1]+sx[temp-1]+c[temp-1])/3;
		cout<<setw(28)<<"总分:";
		cout<<zf[temp-1]*3<<endl;
		save();
		read();
		pm();
	}
	void type()                                           //打印数据表//
	{	
		pm();
		cout<<"┌──┬──┬──┬──┬──┬──┬──┬──┐"<<endl;
		cout<<"│学号│姓名│英语│数学│编程│总分│平均│排名│"<<endl;
		cout<<"├───────────────────────┤"<<endl;
		for(int a=0;a<n1;a++)
		{
			cout<<"│"<<setw(4)<<a+1<<"│"<<setw(4)<<xm[a]<<"│"<<setw(4)<<yy[a]<<"│"<<setw(4)<<sx[a]<<"│"<<setw(4)<<c[a]<<"│"<<setw(4)<<zf[a]*3<<"│"<<setw(4)<<int(zf[a])<<"│"<<setw(4)<<p[a]<<"│"<<endl;
			cout<<"├───────────────────────┤"<<endl;
		}
		cout<<"└───────────────────────┘"<<endl;
	}
	void menu()                                            //菜单选项//
	{
begin:
	cout<<"\n"<<setw(40)<<"主菜单:"<<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<<"输入9.添加数据!"<<endl;
    cout<<"输入10.清空所有数据!"<<endl;
	cout<<"请选择:";
	int tem; 
    cin>>tem;
	if (tem==1) {sr();goto begin;}
	if(tem<1||tem>10)  {cout<<"你所输入的数字不符合,请重新输入!"<<endl;goto begin;}
	if(n1>0)
	{
		if(tem==2){ave();goto begin;}
		else if(tem==3)
		{
			tj(yy,n1,yyt);tj(sx,n1,sxt);tj(c,n1,ct);tj(zf,n1,zft);
			yybl();sxbl();cbl();zfbl();goto begin;
		}
		else if (tem==4){modify();goto begin;}
		else if(tem==5){type();goto begin;}
		else if(tem==6){del();goto begin;}	
		else if(tem==7){save();goto begin;}
		else if(tem==8){read();goto begin;}
		else if(tem==9){add();goto begin ;}
		else if(tem==10){qk();goto begin;}
	}
	else 
	{
		cout<<"出错,请先选择1输入数据,再进行其他程序!"<<endl;goto begin;}
	}
private:
	typedef int array[100];
	char xm[100][7];
	int yya,sxa,ca,zfa,n1,p[100];
	double zf[100],zft[5],xh[100],yy[100],yyt[5],sx[100],sxt[5],c[100],ct[5];
};
Student student;                                           //定义对象//
void main()
{
	student.menu();
}






⌨️ 快捷键说明

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