Design a student status management system

Posted by chrisdburns on Wed, 22 Dec 2021 16:37:02 +0100

I am also a code Xiaobai. I know a lot of modifications and night places in this code. I also hope to see the big guys can leave the axe on time! Thank you, thank you!

[title]

[design]

1. First of all, we should establish a student Dat file and enter student information in it.

Input data from the keyboard and establish the data file student Dat: as shown in the figure

 

Use the keyboard to input the relevant information of four students. For different table items, use spaces to separate them.

2. For seven table items, we create a Student class. A Student object can save the basic information of a Student. For multiple students, we think of using arrays to implement it.

3. We use a string to read the student information of each line and store it in an element of a student array.

For files, I use the fstream class in c + + to read and write files. The last requirement is to make student The dat data is sorted and written to the corresponding file. Here we set the target file to English dat,mathout.dat,programout.dat,PEout.dat for writing.

4. Function realization:

To query the student or student number is to find the information of the array elements of the output student.

For all students, calculating the average score according to the class is to calculate the average score output for each subject.

The result file is generated by sorting according to English, mathematics, program design and sports scores. It is to use ofstream class to create four output streams to four files, and then write the sorted data to them. The sorting here adopts the direct insertion method.

5. Finally, because you can't execute a function of the system, you can exit. We have to use multiple functions, so after executing a function, you can return to the main page and continue to select the function. The main framework used here is do{}while(); The way.

Less nonsense, less code!

[Code]

#include<iostream>
#include<cstring>
#include<fstream>
#include<bits/stdc++.h>
using namespace std;
class Student{
public:
	string name;
	string id;
	string gender;
	string english;
	string math;
	string program;
	string PE;
};
//Due to the compiler, the stod function cannot be used. Rewrite it here
double stod(string str){
	double num=0;
	for(int i=0;str[i]!='\0';i++){
		num=num*10+str[i]-'0';
//		cout<<"num-for:"<<num<<endl;
	}
//	cout<<"num:"<<num<<endl;
	return num;
} 
//Store the file information into the student class array 
Student match(Student student,string s){
	int table=0,j=0;
//	cout<<"match"<<endl;
//	cout<<s<<endl;
//	for(int i=0;s[i]!='\0';i++)
//	cout<<s[i]<<"|";
	for(int i=0;s[i]!='\0';i++){
		if(s[i]!=' '){
			switch(table){
			case 0:{student.name+=s[i];
					j++;
				break;
			}
			case 1:{student.id+=s[i];
					j++;
				break;
			}
			case 2:{student.gender+=s[i];
					j++;
				break;
			}
			case 3:{student.english+=s[i];
					j++;
				break;
			}
			case 4:{student.math+=s[i];
					j++;
				break;
			}
			case 5:{student.program+=s[i];
					j++;
				break;
			}
			case 6:{student.PE+=s[i];
					j++;
				break;
			}
		}
		}
		else{
			switch(table){
			case 0:student.name[j]='\0';break;
			case 1:student.id[j]='\0';break;
			case 2:student.gender[j]='\0';break;
			case 3:student.english[j]='\0';break;
			case 4:student.math[j]='\0';break;
			case 5:student.program[j]='\0';break;
			case 6:student.PE[j]='\0';break;
		}
			table++;
			j=0;
		}
	}
//	Cout < < name: < < student Name < < "" < < student No.: < < student ID < < "" < < gender: < < student Gender < < "" < < English score: < < student English < < "" < < mathematics score: < < student Math < < "" < < "programming score:" < < student Program < < "" < < sports performance: < < student PE<<endl;
//	cout<<"table:"<<table<<endl;
	return student;
}
//1. Query students or student numbers and display information
void find(Student student[],int number){
	int function;
	cout<<endl;
	cout<<"There are two ways to find students' information:"<<endl;
	cout<<"1.Use student name"<<endl;
	cout<<"2.Use student ID"<<endl;
	cout<<"Which channel do you use? Enter the serial number before the function"<<endl;
	cin>>function;
	if(function==1){
		cout<<"Please enter the student's name:"<<endl;
		string name;
		cin>>name;
		for(int i=0;i<number;i++){
			if(student[i].name==name){
				cout<<"The basic information of the student is:"<<endl;
				cout<<"full name:"<<student[i].name<<" "<<"Student No.:"<<student[i].id<<" "<<"Gender:"<<student[i].gender<<" "<<"English score:"<<student[i].english<<" "<<"Math scores:"<<student[i].math<<" "<<"Program design score:"<<student[i].program<<" "<<"Sports performance:"<<student[i].PE<<endl;
				break;
			}
		}
	}
	else{
		cout<<"Please enter the student number:"<<endl;
		string id;
		cin>>id;
		for(int i=0;i<number;i++){
			if(student[i].id==id){
				cout<<"The basic information of the student is:"<<endl;
				cout<<"full name:"<<student[i].name<<" "<<"Student No.:"<<student[i].id<<" "<<"Gender:"<<student[i].gender<<" "<<"English score:"<<student[i].english<<" "<<"Math scores:"<<student[i].math<<" "<<"Program design score:"<<student[i].program<<" "<<"Sports performance:"<<student[i].PE<<endl;
				break;
			}
		}
	}
}
//2. For all students, calculate the average score according to the class
void average(Student student[],int number){
	cout<<"There are in this class"<<number<<"people"<<endl;
	//Define the GPA of each subject 
	double senglish=0;
	double smath=0;
	double sprogram=0;
	double sPE=0;
	for(int i=0;i<number;i++){
		senglish+=(stod(student[i].english)/number);
//		cout<<"senglish:"<<senglish<<" student[i].english:"<<student[i].english<<endl;
		smath+=(stod(student[i].math)/number);
//		cout<<"smath:"<<smath<<endl;
		sprogram+=(stod(student[i].program)/number);
//		cout<<"sprogram:"<<sprogram<<endl;
		sPE+=(stod(student[i].PE)/number);
//		cout<<"sPE:"<<sPE<<endl;
	}
	cout<<"The grades of each subject in this class are:"<<endl;
	cout<<"Average score in English:"<<senglish<<" Average score in Mathematics:"<<smath<<" Average score of programming:"<<sprogram<<" Average sports score:"<<sPE<<endl; 
}
//3. The result files are generated according to English, mathematics, programming and sports scores
void sort(Student student[],int number){
	//Set different file output according to ranking 
	ofstream englishout("englishout.dat");
	ofstream mathout("mathout.dat");
	ofstream programout("programout.dat");
	ofstream PEout("PEout.dat");
	Student med;
	int j;
	//
	for(int i=0;i<number;i++){
		for(int j=i+1;j<number;j++){
			if(stod(student[j].english)>stod(student[i].english)){
				med=student[j];
				student[j]=student[i];
				student[i]=med;
			}
		}
	}
	for(int i=0;i<number;i++){
		englishout<<student[i].name<<" "<<student[i].id<<" "<<student[i].gender<<" "<<student[i].english<<" "<<student[i].math<<" "<<student[i].program<<" "<<student[i].PE<<endl;
	}
	//
	for(int i=0;i<number;i++){
		for(int j=i+1;j<number;j++){
			if(stod(student[j].math)>stod(student[i].math)){
				med=student[j];
				student[j]=student[i];
				student[i]=med;
			}
		}
	}
	for(int i=0;i<number;i++){
		mathout<<student[i].name<<" "<<student[i].id<<" "<<student[i].gender<<" "<<student[i].english<<" "<<student[i].math<<" "<<student[i].program<<" "<<student[i].PE<<endl;
	}
	//
	for(int i=0;i<number;i++){
		for(int j=i+1;j<number;j++){
			if(stod(student[j].program)>stod(student[i].program)){
				med=student[j];
				student[j]=student[i];
				student[i]=med;
			}
		}
	}
	for(int i=0;i<number;i++){
		programout<<student[i].name<<" "<<student[i].id<<" "<<student[i].gender<<" "<<student[i].english<<" "<<student[i].math<<" "<<student[i].program<<" "<<student[i].PE<<endl;
	}
	//
	for(int i=0;i<number;i++){
		for(int j=i+1;j<number;j++){
			if(stod(student[j].PE)>stod(student[i].PE)){
				med=student[j];
				student[j]=student[i];
				student[i]=med;
			}
		}
	}
	for(int i=0;i<number;i++){
		PEout<<student[i].name<<" "<<student[i].id<<" "<<student[i].gender<<" "<<student[i].english<<" "<<student[i].math<<" "<<student[i].program<<" "<<student[i].PE<<endl;
	}
	//
	englishout.close();
	mathout.close();
	programout.close();
	PEout.close();
}
int main(){
	cout<<"==========Welcome to the student status management system=========="<<endl; 
	string s;
	char orderagain;
	int function;
	Student student[100];
	int number=0;//Number of students
	ifstream in("student.dat ");
	while(getline(in,s)){
		student[number]=match(student[number],s);
	//	Cout < < name: < < student [number] Name < < "" < "student number:" < < student [number] ID < < "" < < gender: < < student [number] Gender < < "" < < English score: < < student [number] English < < "" < < math score: < < student [number] Math < < "" < < programming score: < < student [number] Program < < "" < < sports performance: < < student [number] PE<<endl;
		number++;
	}
//	cout<<"number:"<<number<<endl;
//	for(int i=0;i<number;i++){
//		Cout < < name: "< < student [i] Name < < "" < < student No.: < < student [i] ID < < "" < "gender:" < < student [i] Gender < < "" < < English score: < < student [i] English < < "" < < mathematics score: < < student [i] Math < < "" < "programming score:" < < student [i] Program < < "" < < sports performance: < < student [i] PE<<endl;
//	}
	do{
		cout<<"The functions of this system include:"<<endl;
		cout<<"1.Realize the query of students or student numbers and display information"<<endl;
		cout<<"2.For all students, the average score is calculated according to the class"<<endl;
		cout<<"3.The result files are generated according to the ranking of English, mathematics, programming and sports scores"<<endl;
		cout<<"What kind of function do you want to realize? Please enter the serial number before the function:"<<endl;
		cin>>function;
		switch(function){
			//Realize the query of students or student numbers and display information
			case 1:find(student,number);break;
			//For all students, the average score is calculated according to the class
			case 2:average(student,number);break;
			//The result files are generated according to the ranking of English, mathematics, programming and sports scores
			case 3:sort(student,number);break;
			default :{cout<<"Command input error, restart the system!"<<endl;orderagain='y';}
		}
		cout<<"When will you return to the main page?"<<endl;
		cin>>orderagain;
	}while(orderagain=='y');
	cout<<"Thank you for your use!"<<endl;
	in.close();
	return 0;  
}

[results]

The first is the operation of the system:

Supplement the implementation of the last function:

 

 

 

 

 

[summary]

Shortcomings of code implementation function:

  1. First of all, my original idea was to output the file name in cmd, and then let my program run. Knowing the lack of basic knowledge, I searched the web page, and I didn't find the result I wanted. As a result, only one student can be fixed in the code Dat's file.
  2. When reading a file using a string, I initially set a mixed character string with Chinese characters, numbers and letters, but when I traverse the read string, Chinese characters will be intercepted incompletely, resulting in the emergence of Chinese garbled code. Compromise. Finally, I chose the mixed character string of numbers and letters to reduce my material hhh~~

Well, for this problem, the two main points I encountered are like this. hhh ~ ~ ~ I worked hard on the first question all day.

Topics: C++