C language to achieve class performance management system

  • 2020-11-03 22:31:40
  • OfStack

C Language course design - Class grade management system, for your reference, the specific content is as follows

Topic:

For a class of N students, each student has AN M course. The system can input, display, modify, sort, save and other operations. function

Requirements:

(1) This system adopts an array of structures, and the structure of each data should include: student number, name, and name of M course.
(2) This system displays the following menu:

Please select the system function item:

a, score entry

b, score display

c, grade saving

d, grade ranking

e, grade modification (password is required first)

f, Score statistics

1) Display the basic information of the student with the highest score in each course
2) Show the average score of each course
3) Show the number of students who exceeded the average score of a given course

g, exit system

1) After performing a specific function, the program will display the menu again.
2) Save the student's grades in the file.

Code:


#include<vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;


typedef struct Student{
 int ID; /* Student student id */
 char name[15]; /* The student's name */ 
 vector<double> subject; /* Because the number of courses is unknown, the number of courses USES a dynamic array vector*/
 double sum; /* The total scores of the students in each subject */
}Student;


struct Rule{ /* collation */ 
 bool operator()(const Student &stu1,const Student &stu2)const
 {
 if(stu1.sum!=stu2.sum) /* The total score is not equal in order of the total score from largest to smallest */ 
 return stu1.sum>stu2.sum;
 return stu1.ID>stu2.ID; /* The total score is equal according to the student number from large to small order */ 
 }
};


int N,M,i,j; /*N Is the number of students, M Is the number of courses */
double score; /* Temporary variable */
Student temp; /* Temporary variable */
vector<Student> Stu; /* This tutorial USES dynamic arrays vector To prevent the waste of memory space */
bool condition=true; /* System operation state */
char choice; /* Used for input to select functions */


/*---------------------------------------------a. Achievements recorded ---------------------------------------------*/
void InsertScore(){
 cout<<" Please enter the number of students in your class and the number of courses: ";
 cin>>N>>M;
 cout<<endl<<" Now please enter each student's student number, name, and name in turn "<<M<<" Grade of the course: "<<endl<<endl;
 for(i=0;i<N;++i){
 cin>>temp.ID>>temp.name;
 for(j=0;j<M;++j){
 cin>>score;
 temp.subject.push_back(score);
 }
 Stu.push_back(temp);
 /*------------------------------ this 1 Line is very important ------------------------------*/
 /*------------------------------ this 1 Line is very important ------------------------------*/
 vector<double>().swap(temp.subject);
 /*------------------------------ this 1 Line is very important ------------------------------*/
 /*------------------------------ this 1 Line is very important ------------------------------*/ 
 }
 cout<<endl<<" Record entry successful! "<<endl; 
}


/*---------------------------------------------b. Results show that ---------------------------------------------*/
void ShowScore(){
 cout<<" Student id \t\t"<<" The name \t\t";
 for(i=0;i<M;++i)
 cout<<" course "<<i+1<<"\t";
 cout<<endl;
 for(i=0;i<N;++i){
 cout<<Stu[i].ID<<"\t"<<Stu[i].name<<"\t";
 for(int j=0;j<M;++j)
 cout<<Stu[i].subject[j]<<"\t";
 cout<<endl;
 }
}


/*---------------------------------------------c. Results to save ---------------------------------------------*/
void SaveScore(){
 FILE *fp;
 if((fp=fopen("stu.dat","wb"))==NULL){
 cout<<" File opening error ";
 return; 
 }
 for(i=0;i<N;++i){
 if(fwrite(&Stu[i],sizeof(Student),1,fp)!=1)
 cout<<" Write error! "<<endl; 
 }
 fclose(fp);
 cout<<" Results saved successfully! "<<endl; 
}


/*---------------------------------------------d. Results sorting ---------------------------------------------*/
void SortScore(){
 for(i=0;i<N;++i){ /* Calculate everyone's total score */
 Stu[i].sum=0;
 for(j=0;j<M;++j)
 Stu[i].sum+=Stu[i].subject[j];
 }
 sort(Stu.begin(),Stu.end(),Rule()); /* Sort using the sort function */
 cout<<" Results sorted successfully! "<<endl;
}


/*---------------------------------------------e. Results modified ---------------------------------------------*/
void ModifyScore(){
 string password;
 int id,no;
 cout<<" Please enter your password to open the permission of grade modification: ";
 cin>>password;
 
 /*------------------------------ Incorrect password entry ------------------------------*/
 while(password!="GUXUNMEI"){
 cout<<endl<<" The password is wrong, please re-enter, if enter Q Then the function of grade modification is suspended: ";
 cin>>password;
 if(password=="Q")
 return;
 }
 
 /*------------------------------ Password entered correctly ------------------------------*/
 cout<<endl<<" Successfully enter the function of grade modification, if student id input -1 Then exit the function of grade modification !"<<endl; 
 while(1){
 cout<<endl<<" Please enter the student id of the student whose grade needs to be modified , The course number and the revised grade :";
 cin>>id;
 if(id==-1){
 cout<<endl<<" Successfully quit the score modification function! "<<endl;
 return; 
 }
 cin>>no>>score;
 if(no>M){ /* The course doesn't exist */ 
 cout<<endl<<" There is no such course! "<<endl;
 continue;
 }
 for(i=0;i<N;++i){
 if(Stu[i].ID==id){
 Stu[i].subject[no-1]=score;
 break;
 }
 }
 if(i==N){  /* Students don't exist */ 
 cout<<endl<<" There is no such student! "<<endl;
 continue; 
 }
 cout<<endl<<" Modified successfully! "<<endl; 
 }
}


/*---------------------------------------------f. Results statistical ---------------------------------------------*/
void CountScore(){
 int maxn[M],count[M];
 double k,average[M]; 
 memset(maxn,0,sizeof(maxn));
 
 /*-------------------- Displays basic information about the students with the highest scores in each course --------------------*/ 
 for(i=0;i<N;++i){
 for(j=0;j<M;++j){
 if(Stu[i].subject[j]>Stu[i].subject[maxn[j]])
 maxn[j]=i;
 }
 }
 for(i=0;i<M;++i){
 cout<<" course "<<i+1<<" Information of students with the highest scores: "<<Stu[i].ID<<"\t"<<Stu[i].name<<"\t";
 for(j=0;j<M;++j)
 cout<<Stu[i].subject[j]<<"\t";
 cout<<endl;
 }
 cout<<endl;
 
 /*--------------------------- Show the average grade of each course ---------------------------*/
 for(j=0;j<M;++j){
 k=0;
 for(i=0;i<N;++i)
 k+=Stu[i].subject[j];
 average[j]=k/N;
 cout<<" course "<<j+1<<" Average score is: "<<average[j]<<endl; 
 }
 cout<<endl;
 
 /*--------------------- Shows the number of students who exceed the average score of a given course ---------------------*/
 for(j=0;j<M;++j){
 count[j]=0;
 for(i=0;i<N;++i){
 if(Stu[i].subject[j]>average[j])
 ++count[j];
 }
 cout<<" More than courses "<<j+1<<" The number of students with average grades is: "<<count[j]<<endl; 
 }
}


/*---------------------------------------------g. Log out ---------------------------------------------*/
void ExitSystem(){
 condition=false; /* The global variable condition Control program operation */
 cout<<" Class performance management system has been withdrawn "<<endl; 
}

 
int main()
{
 while(condition){
 /*----------------------------------- The system menu -----------------------------------*/
 cout<<"        "<<endl;
 cout<<"------------------------- The system menu -------------------------"<<endl;
 cout<<"        "<<endl;
 cout<<"   Please select the system function item:     "<<endl;
 cout<<"    a. Achievements recorded    "<<endl;
 cout<<"    b. Results show that    "<<endl;
 cout<<"    c. Results to save    "<<endl;
 cout<<"    d. Results sorting    "<<endl;
 cout<<"    e. Results modified    "<<endl;
 cout<<"    f. Results statistical    "<<endl;
 cout<<"    g. Log out    "<<endl;
 cout<<"        "<<endl;
 cout<<"----------------------------------------------------------"<<endl;
 cout<<"        "<<endl;
 
 /*----------------------------------- Select system function items -----------------------------------*/
 cout<<" Please enter options: ";
 cin>>choice;
 cout<<endl;
 switch(choice){
 case 'a':InsertScore(); break;
 case 'b':ShowScore(); break;
 case 'c':SaveScore(); break;
 case 'd':SortScore(); break;
 case 'e':ModifyScore(); break;
 case 'f':CountScore(); break;
 case 'g':ExitSystem(); break;
 default:cout<<" This feature does not exist! "<<endl;
 } 
 }
 return 0;
}

For more information about the management system, please click "Management system Topics" to learn


Related articles: