C++

C language version of student performance management system


In this paper, the example of C language version of the student performance management system to share the specific code, for your reference, the specific content is as follows

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#include<algorithm>
char buf[255];
char c=14;
char path[]="D:\\data";
char tmp[]="D:\\tmp";
struct Student
{
  char Name[20];
  char No[20];
  char Dept[20];
  char ClassNo[20];
  double Math,Chinese,English;
  double Ave;
  bool Pass;
}students[500];
bool cmp1(Student s1,Student s2)
{
  return s1.Chinese>s2.Chinese;
}
bool cmp2(Student s1,Student s2)
{
  return s1.Math>s2.Math;
}
bool cmp3(Student s1,Student s2)
{
  return s1.English>s2.English;
}
bool cmp4(Student s1,Student s2)
{
  return s1.Ave>s2.Ave;
}
//---------------------------------------------------------void Sort(int cmd)
/*
1 ChineseSort
2 MathSort
3 EnglishSort
4 AveSort
*/
int Sort(int cmd)
{
  Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=0;
  while((n=fread(&stu,sizeof(stu),1,fp)>0))
  {
    students[cnt++]=stu;
  }
  fclose(fp);
  switch(cmd)
  {
  case 1:
  std::sort(students,students+cnt,cmp1);
    break;
  case 2:
  std::sort(students,students+cnt,cmp2);
    break;
  case 3:
  std::sort(students,students+cnt,cmp3);
    break;
  case 4:
  std::sort(students,students+cnt,cmp4);
    break;
  default:
    exit(0);
  }
  return cnt;
}
//---------------------------------------------------------PrintTitle()
void PrintTitle()
{
  printf("%-8s%-12s%-12s%-8s%-7s%-7s%-7s%-7s%-s\n"," The name "," The class "," Student id "," faculties "," Chinese language and literature "," mathematics "," English "," average "," Pass the ");
}
//---------------------------------------------------------Print(Student stu)
void Print(Student stu)
{
  printf("%-8s%-12s%-12s%-8s%-7.2lf%-7.2lf%-7.2lf%-7.2lf%-s\n",stu.Name,stu.ClassNo,stu.No,stu.Dept,stu.Chinese,stu.Math,stu.English,stu.Ave,stu.Pass?" is ":" no ");
}
//---------------------------------------------------------PrintNo(char No[])
void PrintNo(char No[])
{
  Student stu;
  int n;
  bool find=0;
  FILE *fp=fopen(path,"ab+");// In order to ab+ Mode open prevent data An error occurred when the file did not exist
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    if(!strcmp(stu.No,No))
    {
      find=1;
      break;
    }
  }
  fclose(fp);
  if(find)
  {
    PrintTitle();
    Print(stu);
  }
  else
    puts(" Not found! ");
}
//---------------------------------------------------------PrintClass(char ClassNo[])
void PrintClass(char ClassNo[])
{
  Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=0;
  while((n=fread(&stu,sizeof(stu),1,fp)>0))
  {
    if(!strcmp(stu.ClassNo,ClassNo))
    {
      if(cnt==0)
        PrintTitle();
      Print(stu);
      cnt++;
    }
  }
  fclose(fp);
  if(cnt==0)
    puts(" No data ");
  else
    printf(" A total of %d article \n",cnt);
}
//---------------------------------------------------------PrintDept(char Dept[])
void PrintDept(char Dept[])
{
    Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=0;
  while((n=fread(&stu,sizeof(stu),1,fp)>0))
  {
    if(!strcmp(stu.Dept,Dept))
    {
      if(cnt==0)
        PrintTitle();
      Print(stu);
      cnt++;
    }
  }
  fclose(fp);
  if(cnt==0)
    puts(" No data ");
  else
    printf(" A total of %d article \n",cnt);
}
//---------------------------------------------------------PrintAll()
void PrintAll()
{
  Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=0;
  while((n=fread(&stu,sizeof(stu),1,fp)>0))
  {
    if(cnt==0)
      PrintTitle();
    Print(stu);
    cnt++;
  }
  fclose(fp);
  if(cnt==0)
    puts(" No data ");
  else
    printf(" A total of %d article \n",cnt);
}
//---------------------------------------------------------Input()
Student Input()
{
  Student stu;
  puts(" Please enter your name, class, student number and school :");
  scanf("%s%s%s%s",&stu.Name,&stu.ClassNo,&stu.No,&stu.Dept);
  puts(" Please enter your grades: Chinese, math, English ");
  scanf("%lf%lf%lf",&stu.Chinese,&stu.Math,&stu.English);
  stu.Ave=(stu.Chinese+stu.Math+stu.English)/3;
  if(stu.Chinese>=60&&stu.Math>=60&&stu.English>=60)
    stu.Pass=1;
  else
    stu.Pass=0;
  return stu;
}
//---------------------------------------------------------Insert()
void Insert()
{
  int cmd;
  do{
    Student stu=Input();
    FILE *fp=fopen(path,"ab+");
    fwrite(&stu,sizeof(stu),1,fp);
    fclose(fp);
    puts(" School success ");
    puts(" Continue typing? [1] is [0] no ");
    scanf("%d",&cmd);
  }while(cmd==1);
}
//---------------------------------------------------------Del()
void Del()
{
  int cmd,cmd2;
  char No[20];
  do{
  puts(" Please enter the student number to delete: ");
  scanf("%s",&No);
  Student stu;
  FILE *fp=fopen(path,"ab+");
  FILE *fp2=fopen(tmp,"wb");
  int n,find=0;
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    if(!strcmp(stu.No,No))
    {
      find=1;
      Print(stu);
      puts(" Are you sure you want to delete it? [1] determine [0] cancel ");
      scanf("%d",&cmd2);
      if(cmd2==1)
        puts(" deleted ");
      else
        fwrite(&stu,sizeof(stu),1,fp2);
    }
    else
    fwrite(&stu,sizeof(stu),1,fp2);
  }
  fclose(fp);
  fclose(fp2);
  if(find==0)
  {
    printf(" Student id not found %s\n",No);
    remove(tmp);
  }
  else
  {
    remove(path);
    rename(tmp,path);
  }
  puts(" Continue to delete? [1] is [0] no ");
  scanf("%d",&cmd);
  }while(cmd==1);
}
//---------------------------------------------------------Change()
void Change()
{
  int cmd;
  char No[20];
  do{
  puts(" Please enter the student number to be changed: ");
  scanf("%s",&No);
  Student stu;
  int find=0;
  FILE *fp=fopen(path,"ab+");
  FILE *fp2=fopen(tmp,"wb");
  int n;
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    if(!strcmp(stu.No,No))
    {
      find=1;
      stu=Input();
      fwrite(&stu,sizeof(stu),1,fp2);
    }
    else
      fwrite(&stu,sizeof(stu),1,fp2);
  }
  fclose(fp);
  fclose(fp2);
  if(find)
  {
    remove(path);
    rename(tmp,path);
    puts(" Change successful! ");
  }
  else
  {
    printf(" Student id not found %s\n",No);
    remove(tmp);
  }
  puts(" Continue the change? [1] is [0] no ");
  scanf("%d",&cmd);
  }while(cmd==1);
}
//---------------------------------------------------------AboutCourse
void Chinese()
{
  double sum=0,ave;
  int pass=0,fail=0,better=0;
  Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=Sort(1);
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    sum+=stu.Chinese;
    if(stu.Chinese>=60)
      {
        pass++;
        if(stu.Chinese>=80)
          better++;
      }
    else
      fail++;
  }
  ave=sum/cnt;
  puts(" Analysis of Chinese scores... ");
  printf(" A total of %d students \n Good: %d people \n Pass: %d people \n Fail: %d people \n Pass: %.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt);
  printf("%-8s%-10s%-12s%-s\n"," ranking "," The name "," Student id "," Chinese language and literature ");
  for(int i=0;i<cnt;i++)
  {
    printf(" The first %2d The name  %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Chinese);
  }
  printf(" Average score: %.2lf\n",ave);
}
void Math()
{
  double sum=0,ave;
  int pass=0,fail=0,better=0;
  Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=Sort(2);
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    sum+=stu.Math;
    if(stu.Math>=60)
      {
        pass++;
        if(stu.Math>=80)
          better++;
      }
    else
      fail++;
  }
  ave=sum/cnt;
  puts(" Math grade analysis... ");
  printf(" A total of %d students \n Good: %d people \n Pass: %d people \n Fail: %d people \n Pass: %.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt);
  printf("%-8s%-10s%-12s%-s\n"," ranking "," The name "," Student id "," mathematics ");
  for(int i=0;i<cnt;i++)
  {
    printf(" The first %2d The name  %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Math);
  }
  printf(" Average score: %.2lf\n",ave);
}
void English()
{
  double sum=0,ave;
  int pass=0,fail=0,better=0;
  Student stu;
  FILE *fp=fopen(path,"ab+");
  int n,cnt=Sort(3);
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    sum+=stu.English;
    if(stu.English>=60)
      {
        pass++;
        if(stu.English>=80)
          better++;
      }
    else
      fail++;
  }
  ave=sum/cnt;
  puts(" Analysis of English scores... ");
  printf(" A total of %d students \n Good: %d people \n Pass: %d people \n Fail: %d people \n Pass: %.2lf%%\n",cnt,better,pass,fail,100.0*pass/cnt);
  printf("%-8s%-10s%-12s%-s\n"," ranking "," The name "," Student id "," English ");
  for(int i=0;i<cnt;i++)
  {
    printf(" The first %2d The name  %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].English);
  }
  printf(" Average score: %.2lf\n",ave);
}
void Average()
{
  Student stu;
  int n,cnt=Sort(4);
  double sum=0;
  FILE *fp=fopen(path,"ab+");
  while((n=fread(&stu,sizeof(stu),1,fp))>0)
  {
    sum+=stu.Chinese;
    sum+=stu.Math;
    sum+=stu.English;
  }
  puts(" Gpa analysis... ");
  printf("%-8s%-10s%-12s%-s\n"," ranking "," The name "," Student id "," The average scores ");
  for(int i=0;i<cnt;i++)
  {
    printf(" The first %2d The name  %-10s%-12s%-8.2lf\n",i+1,students[i].Name,students[i].No,students[i].Ave);
  }
  printf(" Average score: %.2lf\n",sum/cnt/3);
}
//---------------------------------------------------------Search()
void Search()
{
  int cmd,cmd2;
  while(1)
  {
    system("cls");
    puts("[1]  Check by student number ");
    puts("[2]  Query by class ");
    puts("[3]  All output ");
    puts("[4]  Search by department ");
    puts("[5]  Course analysis ");
    puts("[0]  Return to main interface ");
    scanf("%d",&cmd);
    if(cmd==0)
      break;
    switch(cmd)
    {
    case 1:
      {
      char No[20];
      puts(" Please enter the student number to be inquired: ");
      scanf("%s",&No);
      PrintNo(No);
      }
      break;
    case 2:
      {
        char ClassNo[20];
        puts(" Please enter to inquire the class number: ");
        scanf("%s",&ClassNo);
        PrintClass(ClassNo);
      }
      break;
    case 3:
      PrintAll();
      break;
    case 4:
      {
        char Dept[20];
        puts(" Please enter the department to be inquired: ");
        scanf("%s",&Dept);
        PrintDept(Dept);
      }
      break;
    case 5:
      while(1)
      {
        puts("[1]  Language score analysis ");
        puts("[2]  Mathematical performance analysis ");
        puts("[3]  Analysis of English scores ");
        puts("[4]  Average score analysis ");
        puts("[0]  On the back 1 level ");
        scanf("%d",&cmd2);
        if(cmd2==0)
          break;
        switch(cmd2)
        {
        case 1:
          Chinese();
          break;
        case 2:
          Math();
          break;
        case 3:
          English();
          break;
        case 4:
          Average();
          break;
        default:
          puts(" Illegal instruction! ");
        }
      }
      break;
    default:
      puts(" Illegal instruction! ");
    }
    while(1)
    {
      puts("[0]  return ");
      puts("[1]  entry ");
      puts("[2]  delete ");
      puts("[3]  Modify the ");
      scanf("%d",&cmd2);
      if(cmd2==0)
        break;
      switch(cmd2)
      {
      case 1:
        Insert();
          break;
      case 2:
        Del();
        break;
      case 3:
        Change();
        break;
      default:
        puts(" Illegal instruction! ");
      }
    }
  }
}
//---------------------------------------------------------Menu()
void ShowMenu1()
{
  int i;
  puts("");
  printf("         ");
  for(i=0;i<14;i++)
    putchar(c);
  printf(" Performance management and analysis system ");
  for(i=0;i<14;i++)
    putchar(c);
  puts("");
  printf("        %c                       %c\n",c,c);
  printf("         %c         Please enter the instruction         %c\n",c,c);
  printf("        %c                       %c\n",c,c);
  printf("         %c        [0]  exit          %c\n",c,c);
  printf("        %c                       %c\n",c,c);
  printf("         %c        [1]  entry          %c\n",c,c);
  printf("        %c                       %c\n",c,c);
  printf("         %c        [2]  delete          %c\n",c,c);
  printf("        %c                       %c\n",c,c);
  printf("         %c        [3]  The query          %c\n",c,c);
  printf("        %c                       %c\n",c,c);
  printf("         %c        [4]  To change the          %c\n",c,c);
  printf("        %c                       %c\n",c,c);
  printf("         ");
  for(i=0;i<44;i++)
    putchar(c);
  puts("");
}
void ShowMenu2()
{
  int i;
  puts("");
  printf("         ");
  for(i=0;i<14;i++)
    putchar(c);
  printf(" Performance management and analysis system ");
  for(i=0;i<14;i++)
    putchar(c);
  puts("");
  printf("         %c                     %c\n",c,c);
  printf("        %c          Please enter the instruction          %c\n",c,c);
  printf("         %c                     %c\n",c,c);
  printf("        %c         [0]  exit           %c\n",c,c);
  printf("         %c                     %c\n",c,c);
  printf("        %c         [1]  entry           %c\n",c,c);
  printf("         %c                     %c\n",c,c);
  printf("        %c         [2]  delete           %c\n",c,c);
  printf("         %c                     %c\n",c,c);
  printf("        %c         [3]  The query           %c\n",c,c);
  printf("         %c                     %c\n",c,c);
  printf("        %c         [4]  To change the           %c\n",c,c);
  printf("         %c                     %c\n",c,c);
  printf("         ");
  for(i=0;i<44;i++)
    putchar(c);
  puts("");
}
//---------------------------------------------------------void RandColor()
void RandColor()
{
  int a,b;
  char param[20]="color ",param2[10];
  do{
    a=rand()%16;
    b=rand()%16;
  }while(a==b);
  sprintf(param2,"%x%x",a,b);
  strcat(param,param2);
  system(param);
}
//---------------------------------------------------------main()
int main()
{
  int cmd;
  srand(time(NULL));
  for(int i=0;i<8;i++)
  {
  RandColor();
  system("cls");
  ShowMenu1();
  system("cls");
  ShowMenu2();
  }
  system("color 0a");
  while(1)
  {
    system("cls");
    ShowMenu1();
    printf("[ ]\b\b");
    scanf("%d",&cmd);
    if(cmd==0)
      break;
    switch(cmd)
    {
    case 1:
      Insert();
      break;
    case 2:
      Del();
      break;
    case 3:
      Search();
      break;
    case 4:
      Change();
      break;
    default:
      puts(" Illegal orders! ");
    }
  }
  return 0;
}