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

📄 hong.cpp

📁 关于学校的学生学籍管理系统,能实现学生成绩和学生资料的录入和修改
💻 CPP
字号:
// Hong.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <string>
 FILE *fp;
struct Stu
{   char name[30];
    char sex[5];
	char num[10];
  float math;
  float eng;
  float pro;
  float mfc;
  float ed;
  float all;
  float ave;
};
class Stud
{int m;
 public:
	 Stud(int i ){m=i;};
   void Hunt1();             //用学号查找个人信息
   void Hunt2();             //用姓名查找个人信息
   void Show();              //显示所有信息
};

void Stud::Hunt1()                                         
{struct Stu st[100],tem;
char ch1[10];
int i;

 cout<<"\n请输入要查找的学号:";
 cin>>ch1;
  fp=fopen("student.txt","r");
  while((fp=fopen("student.txt","r"))==NULL)
	{ cout<<"文件打开失败,无法显示!"<<endl;break;}
  for(i=0;fread(&st[i],sizeof(struct Stu),1,fp)!=0;i++)
  {   while(strcmp(st[i].num,ch1)==0) 
  {    tem=st[i];
	  cout<<"学号\t"<<"姓名\t"<<"性别\t"<<"数学\t"<<"英语\t"
	 <<"程序\t"<<"可视化\t"<<"体育\t"<<"总分\t"<<"平均分\n\n";
   cout<<tem.num<<"\t"<<tem.name<<"\t"<<tem.sex<<"\t"<<tem.math
       <<"\t"<<tem.eng<<"\t"<<tem.pro<<"\t"<<tem.mfc<<"\t"
	   <<tem.ed<<"\t"<<tem.all<<"\t"<<tem.ave<<endl; 
   break;
  }
  }
  while(strcmp(tem.num,ch1)!=0)
  { cout<<"没有相关信息!\n";break;}
  fclose(fp);
}

void Stud::Hunt2()
{struct Stu st[100],tem;
char ch2[30];
int i;
  cout<<"\n请输入要查找的姓名:";
  cin>>ch2;
  fp=fopen("student.txt","r");
  while((fp=fopen("student.txt","r"))==NULL)
	{ cout<<"文件打开失败,无法显示!"<<endl;break;}
  for(i=0;fread(&st[i],sizeof(struct Stu),1,fp)!=0;i++)
  {   while(strcmp(st[i].name,ch2)==0) 
  {    tem=st[i];
	  cout<<"学号\t"<<"姓名\t"<<"性别\t"<<"数学\t"<<"英语\t"
	 <<"程序\t"<<"可视化\t"<<"体育\t"<<"总分\t"<<"平均分\n\n";
   cout<<tem.num<<"\t"<<tem.name<<"\t"<<tem.sex<<"\t"<<tem.math
       <<"\t"<<tem.eng<<"\t"<<tem.pro<<"\t"<<tem.mfc<<"\t"
	   <<tem.ed<<"\t"<<tem.all<<"\t"<<tem.ave<<endl; 
    break;
  }
  }
  while(strcmp(tem.name,ch2)!=0)
  { cout<<"没有相关信息!\n";break;}
  fclose(fp);
}

void Stud::Show()	 
{ struct Stu t[100],temp;
  int i=0,n=0,j,k,fail1,fail2,fail3,fail4,fail5;
  int zb1,zb2,zb3,zb4,zb5;
   fail1=fail2=fail3=fail4=fail5=0;
   zb1=zb2=zb3=zb4=zb5=0;
  
   fp=fopen("student.txt","r");
    while((fp=fopen("student.txt","r"))==NULL)
	{ cout<<"文件打开失败,无法显示!"<<endl;break;}
	while(fread(&t,sizeof(struct Stu),1,fp)==0)
	{cout<<"\n信息库里没有信息可供查看!!\n";break;}
    fclose(fp);
   fp=fopen("student.txt","r");
   for(i=0;fread(&t[i],sizeof(struct Stu),1,fp)!=0;i++)
  {
   if(t[i].math>=0 &&t[i].math<60)fail1++;if(t[i].math==-1)zb1++;
   if(t[i].eng>=0 &&t[i].eng<60)fail2++; if(t[i].eng==-1)zb2++;
   if(t[i].pro>=0 &&t[i].pro<60)fail3++; if(t[i].pro==-1)zb3++;
   if(t[i].mfc>=0 &&t[i].mfc<60)fail4++; if(t[i].mfc==-1)zb4++;
   if(t[i].ed>=0 &&t[i].ed<60)fail5++;  if(t[i].ed==-1)zb5++;
  }
   n=i;
  fclose(fp);
  for(i=0;i<n-1;i++)
  {k=i;
  for(j=i+1;j<n;j++)
	  if(t[j].num<t[k].num)k=j;
	  if(k!=i)
	  {temp=t[i];t[i]=t[k];t[k]=temp;}
  }
  /*for (j=1;j<=n-1;j++)
   for (i=0;i<n-j;i++)
	 if(t[i].num>t[i+1].num) 
	 {temp=t[i];t[i]=t[i+1];t[i+1]=temp;}*/
 
  cout<<"\n\n为了方便查看,下列信息均按学号顺序排列!\n\n"
	 <<"学号\t"<<"姓名\t"<<"性别\t"<<"数学\t"<<"英语\t"
	 <<"程序\t"<<"可视化\t"<<"体育\t"<<"总分\t"<<"平均分\n\n";
  for (i=0;i<n;i++)
  {cout<<t[i].num<<"\t"<<t[i].name<<"\t"<<t[i].sex<<"\t"<<t[i].math
       <<"\t"<<t[i].eng<<"\t"<<t[i].pro<<"\t"<<t[i].mfc<<"\t"
	   <<t[i].ed<<"\t"<<t[i].all<<"\t"<<t[i].ave<<endl;
  };
  cout<<"\n\t一共统计了"<<n<<"个学生信息,结果如下:\n"
      <<"\t不及格人数"<<"\t作弊人数(成绩为-1的)"<<endl;
  cout<<"\n数学:\t"<<fail1<<"\t\t"<<zb1
	  <<"\n英语:\t"<<fail2<<"\t\t"<<zb2
	  <<"\n程序:\t"<<fail3<<"\t\t"<<zb3
	  <<"\n可视化:\t"<<fail4<<"\t\t"<<zb4
	  <<"\n体育:\t"<<fail5<<"\t\t"<<zb5<<endl;
	  
}

class Teach:public Stud      //类Teach继承类Stud
{public:
 Teach(int i);
  void Add();              //添加学生信息
  void Del();              //删除单个学生信息  
  void Delall();           //删除所有学生信息
  void Edit();             //修改单个学生信息
  void Sort();              //修改用户密码
};

Teach::Teach(int i):Stud( i ){};
void Teach::Add()
{ struct Stu st[50],t[100];
  int i=0,j;
  char ch;

  
  cout<<"\n请按以下顺序输入学生信息\n:"
	  <<"学号  姓名  性别  数学  英语  程序  可视化  体育\n";
  fp=fopen("student.txt","r");
  do
  {cout<<"请输入:\n";
	  cin>>st[i].num>>st[i].name>>st[i].sex>>st[i].math>>
		 st[i].eng>>st[i].pro>>st[i].mfc>>st[i].ed;
      st[i].all=(st[i].math+st[i].eng+st[i].pro+st[i].mfc+st[i].ed);
      st[i].ave=st[i].all/5;
    for(j=0;fread(&t[j],sizeof(struct Stu),1,fp)!=0;j++)
	{ while(strcmp(st[i].num,t[j].num)==0)
	   {cout<<"您要添加信息的学号已经存在,若其信息有误,"
	        <<"\n可返回上一层选择 <修改学生信息> 对其修改!";
			break;}
	}fclose(fp);
   fp=fopen("student.txt","a");
      fwrite(&st[i],sizeof(struct Stu),1,fp);
      i++;
     cout<<"\n继续输入?(Y/N)";
     cin>>ch;
  }while(ch=='y'||ch=='Y');
  
  cout<<"你已经成功添加"<<i<<"个学生信息!"<<endl;
  fclose(fp);
}

void Teach::Del()
{struct Stu st[100],temp;
  int i=0,j=0,n;
  char ch7[10];
  cout<<"\n请输入你要删除信息的学号:";
   cin>>ch7;
   fp=fopen("student.txt","r");
    while((fp=fopen("student.txt","r"))==NULL)
	{ cout<<"文件打开失败,无法显示!"<<endl;break;}

  for(i=0;fread(&st[i],sizeof(struct Stu),1,fp)!=0;i++)//先查找一下是
      if(strcmp(st[i].num,ch7)==0)                     //否有此学号的
	  {temp=st[i];j=i; };                              //信息. 
   n=i;
  fclose(fp);
  while(strcmp(temp.num,ch7)!=0)                      //没有,则退出 
  { cout<<"没有相关信息,不用删除!\n";break; }         //删除程序
  fp=fopen("student.txt","w");                        //有,则打开文件,
for (i=0;i<j;i++)fwrite(&st[i],sizeof(struct Stu),1,fp);//进行删除工作. 
for (i=j+1;i<n;i++)fwrite(&st[i],sizeof(struct Stu),1,fp);
cout<<"信息已删除!\n";
 fclose(fp);
  
}
void Teach::Delall()
{Stu st;
	char ch8;
 cout<<"\n!!!你确定要删除所有信息吗!!!?(Y/N)\n";
 cin>>ch8;
  while(ch8=='y'||ch8=='Y')
  {  fp=fopen("student.txt","w");
     if((fp=fopen("student.txt","w"))==NULL)
	 { cout<<"文件打开失败,无法删除!"<<endl;exit(0);}
     else
	 {fwrite(&st,0,0,fp);
		 cout<<"所有信息都已删除!\n";break;
		 fclose(fp);}
  }
}

void Teach:: Edit()
{struct Stu st[100],tem,tem2;
char ch1[10];
int i,k,n=0;

 cout<<"\n请输入要修改信息的学生学号:";
 cin>>ch1;
  fp=fopen("student.txt","r");
  while((fp=fopen("student.txt","r"))==NULL)
	{ cout<<"文件打开失败,无法显示!"<<endl;break;}
  for(i=0;fread(&st[i],sizeof(struct Stu),1,fp)!=0;i++,n++)
      if(strcmp(st[i].num,ch1)==0) 
	  {tem2=st[i];k=i;
          
	   cout<<"请输入新的信息!\n"
           <<"学号\t"<<"姓名\t"<<"性别\t"<<"数学\t"<<"英语\t"
	       <<"程序\t"<<"可视化\t"<<"体育\n\n";
	  cin>>tem.num>>tem.name>>tem.sex>>tem.math>>
		 tem.eng>>tem.pro>>tem.mfc>>tem.ed;
      tem.all=(tem.math+tem.eng+tem.pro+tem.mfc+tem.ed);
      tem.ave=tem.all/5;
	    while(strcmp(tem.num,ch1)!=0)
		{cout<<"要修改信息的学号与新输入的学号不一致,修改不成功!\n";
	     break;}
	  }
  fclose(fp);
  while(strcmp(tem2.num,ch1)!=0)
  { cout<<"没有相关信息!\n";break;}
  st[k]=tem;
  fp=fopen("student.txt","w");
  for(i=0;i<n;i++)fwrite(&st[i],sizeof(struct Stu),1,fp);
			 
	  cout<<"修改成功,新的信息为:\n"
		      <<tem.num<<"\t"<<tem.name<<"\t"<<tem.sex<<"\t"<<tem.math
              <<"\t"<<tem.eng<<"\t"<<tem.pro<<"\t"<<tem.mfc<<"\t"
	          <<tem.ed<<"\t"<<endl;
   fclose(fp);
 }


void Teach::Sort()                    //按数学成绩排序
{
	int j,k,i=0;
	struct Stu temp;
	struct Stu *t;
	t=new struct Stu[100];
	int num;                                //写入操作文件的人数
 	fstream f,g;
	num=0;
 fp=fopen("student.txt","r");
   for(i=0;fread(&t[i],sizeof(struct Stu),1,fp)!=0;i++);
   num=i;
	fclose(fp);                              //选择排序
	for(i=0;i<num;i++)
		{
			k=i;
			for(j=i+1;j<num;j++)
				if(t[j].num>t[k].num)
					k=j;
				temp=t[i];
				t[i]=t[k];
				t[k]=temp;
		}
	
	while((fp=fopen("student.txt","w"))==NULL)
	{ cout<<"文件打开失败,无法显示!"<<endl;break;}
	for( i=0;i<num;i++)
	{
		fwrite((char*)&t[i],sizeof(struct Stu),1,fp);
	}
	fclose(fp);
	cout<<"成功按数学成绩排序"<<endl;
	cout<<"学号\t"<<"姓名\t"<<"性别\t"<<"数学\t"<<"英语\t"
	 <<"程序\t"<<"数据\t"<<"体育\t"<<"总分\t"<<"平均分\n\n";
  for (i=0;i<num;i++)
	{cout<<t[i].num<<"\t"<<t[i].name<<"\t"<<t[i].sex<<"\t"<<t[i].math
       <<"\t"<<t[i].eng<<"\t"<<t[i].pro<<"\t"<<t[i].mfc<<"\t"
	   <<t[i].ed<<"\t"<<t[i].all<<"\t"<<t[i].ave<<endl;
	};
}

void menu1()
{  cout<<"\n\t******************************************\n\n";
   cout<<"\n\t**  计机054,林镇鸿 (学号:0515214416)  **\n\n";
   cout<<"\n\t--------欢迎您来到学生学籍管理系统------\n\n";
   cout<<"\n\t*******     1--- 使用需知           ******\n";
   cout<<"\n\t*******     2--- 学生               ******\n";
   cout<<"\n\t*******     3--- 教师(需要密码)     *****\n";
   cout<<"\n\t*******     4----退出               *****\n";
   cout<< "\n\n\t**请选择你的身份或需要的操作:";
};
void menu2()
{ cout<<"\n\t*****   1--- 用学号查找个人信息     ******\n";
  cout<<"\n\t*****   2--- 用姓名查找个人信息     ******\n";
  cout<<"\n\t*****   3--- 显示所有信息           ******\n";
  cout<<"\n\t*****   4----返回                   ******\n";
  cout<<"\n\t*****   5----退出                   ******\n"; 
  cout<< "\n\n\t**请选择你需要的操作:";
}
void menu3()
{ cout<<"\n\t**  1-- 用学号查找个人信息  2-- 用姓名查找个人信息**\n\n";
  
  cout<<"\t**  3-- 显示所有信息        4-- 添加学生信息      **\n\n";             
  
  cout<<"\t**  5-- 删除单个学生信息    6-- 删除所有学生信息  **\n\n";
  
  cout<<"\t**  7-- 修改学生信息        8--- paixu     **\n\n";
  
  cout<<"\t**  9-- 返回                0--- 退出             **\n\n";
  
  cout<< "\n\n\t**请选择你需要的操作:";
} ;

 void main()
{ char i,i1,i2,c[20],key[20],q[100],p;
  Stud s(0);
  Teach te(0);
 
 loop1:  
  	menu1(); cin>>i;
   
   switch(i)
   { case '1':
	{system("cls");
	 ifstream f("instruct.txt",ios::in);
      while(!f.eof())
	  { f.getline(q,100);
       cout<<q<<endl;}
      f.close();
     cout<<"\n返回上一层?(Y/N)";
     cin>>p;
	 while(p=='y'||p=='Y'){system("cls");goto loop1;}
     break;
	 }
     case '2':
	{ system("cls");
      loop2:
		   menu2();
		   cin>>i1;
		   if(i1=='1')
		   { s.Hunt1();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop2;}
		     break;}

		   else if(i1=='2')
		   { s.Hunt2();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop2;}
		     break;}

		   else if(i1=='3')
		   { s.Show();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop2;}
		      break;}
          else if(i1=='4'){system("cls");goto loop1;}
          else  break;
		 }
     case '3':
	{ system("cls");
	  
	{loop3:
		    menu3();
            cin>>i2;
		    if(i2=='1')
			{ te.Hunt1(); 
			  cout<<"\n返回上一层?(Y/N)";
		      cin>>p;
		      while(p=='y'||p=='Y'){system("cls");goto loop3;}
		      break;}
		   else if(i2=='2')
		   { te.Hunt2();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop3;}
		      break;} 
		   
		   else if(i2=='3')
		   { te.Show();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop3;}
		     break;}
		   else if(i2=='4')
		   { te.Add();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop3;}
		     break;}
		   else if(i2=='5')
		   { te.Del();
		     cout<<"\n返回上一层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop3;}
		     break;}

		  else if(i2=='6')
		  { te.Delall();
		    cout<<"\n返回上一层?(Y/N)";
		    cin>>p;
		    while(p=='y'||p=='Y'){system("cls");goto loop3;}
		    break;}

		  else if(i2=='7') 
		  { te.Edit();
		    cout<<"\n返回上一层?(Y/N)";
		    cin>>p;
		    while(p=='y'||p=='Y'){system("cls");goto loop3;}
		    break;}
		   else if(i2=='8')
		   { te.Sort();
		     cout<<"\n返回到最上层?(Y/N)";
		     cin>>p;
		     while(p=='y'||p=='Y'){system("cls");goto loop1;}
		     break;}
		   else if(i2=='9'){system("cls");goto loop1;}
		   else  break;
		 }
	} 
     case '4': cout<<"\n谢谢使用,再见!\n";break;
     default: {cout<<"\n输入错误,请重新输入!\n";goto loop1;}
   }
 }
  

⌨️ 快捷键说明

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