A super classic C++ structure of the problem
- 2020-04-02 01:26:18
- OfStack
Title description:
There are 10 students, and the data of each student includes the student number, name, English, math, and physics scores of three courses. Enter the data of 10 students from the keyboard, and ask to print out the total average scores of three courses, as well as the data of the students with the highest scores (including the student number, name, average scores of three courses and average scores).
C + + code:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020838382.jpg ">
There are 10 students, and the data of each student includes the student number, name, English, math, and physics scores of three courses. Enter the data of 10 students from the keyboard, and ask to print out the total average scores of three courses, as well as the data of the students with the highest scores (including the student number, name, average scores of three courses and average scores).
C + + code:
#include<iostream>
#include<string>
using namespace std;
struct Student{//Declare the struct Student
string num;
string name;
float english;
float match;
float physics;
float average;
};
void shuchu(Student &s){//Structure content output function
cout<<s.num<<"t";
cout<<s.name<<"t";
cout<<s.english<<"t";
cout<<s.match<<"t";
cout<<s.physics<<"t";
cout<<s.average<<endl;
}
int main(){
Student s[10];//Declares an array of structs
cout<<"numtnametenglishtmatchtphysics"<<endl;
int i=0;
for(;i<10;i++){//Initialize the structure statistics
cin>>s[i].num;
cin>>s[i].name;
cin>>s[i].english;
cin>>s[i].match;
cin>>s[i].physics;
s[i].average=(s[i].english+s[i].match+s[i].physics)/3;
}
float max=s[0].average;
int k=1;
cout<<"=============show data======="<<endl;
for(i=0;i<10;i++){
shuchu(s[i]);//Output the contents of the structure
if(s[i].average>max){//The data with the highest average score was obtained by the ring method
k=i;
max=s[i].average;
}
}
cout<<"The hightest:"<<endl;
shuchu(s[k]);//Output the highest score data
cout<<endl;
return 0;
}
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020838382.jpg ">