Lesson 26: Classes and Structures
P5740 【Deep Foundation 7. Example 9】 The Most Outstanding Student

#include<iostream>
#include<cstring>
using namespace std;
// Define structure
struct student{
string name;
float chinese;
float math;
float english;
float total;
};
int main(){
int n;
cin >> n;
student stus[1001];
// Record the index of the student with the highest total score
int index = -1;
float mmax = -1;
for(int i = 0; i < n; i++){
// Save each student's information into the array
student s = {};
cin >> s.name >> s.chinese >> s.math >> s.english;
s.total = s.chinese + s.math + s.english;
if(s.total > mmax){
mmax = s.total;
index = i;
}
stus[i] = s;
}
// Output the information of the student with the highest score
cout << stus[index].name << " ";
cout << stus[index].chinese << " ";
cout << stus[index].math << " ";
cout << stus[index].english << " ";
return 0;
}
P5741 【Deep Foundation 7. Example 10】 Evenly Matched Opponents – Enhanced Version

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
// Define structure
struct student{
string name;
int chinese;
int math;
int english;
int total;
};
// Determine if they are evenly matched
int opponent(student a, student b){
if(abs(a.chinese - b.chinese) > 5){
return 0;
}
if(abs(a.math - b.math) > 5){
return 0;
}
if(abs(a.english - b.english) > 5){
return 0;
}
if(abs(a.total - b.total) > 10){
return 0;
}
return 1;
}
int main(){
int n;
cin >> n;
student stus[1001];
for(int i = 0; i < n; i++){
// Save each student's information into the array
student s = {};
cin >> s.name >> s.chinese >> s.math >> s.english;
s.total = s.chinese + s.math + s.english;
stus[i] = s;
}
// Compare each student with the students behind them
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(opponent(stus[i], stus[j])){
cout << stus[i].name << " " << stus[j].name << "\n";
}
}
}
return 0;
}
P5742 【Deep Foundation 7. Example 11】 Grading

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
// Define structure
struct student{
int id;
int score;
int develop;
int compositeScore;
int getTotal(){
return score + develop;
}
};
// Determine if the student is excellent
int excellent(student a){
if (a.getTotal() > 140 && a.compositeScore >= 800){
return 1;
}
return 0;
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
// Save each student's information into the array
student s = {};
cin >> s.id >> s.score >> s.develop;
s.compositeScore = s.score * 7 + s.develop * 3;
if(excellent(s)){
cout << "Excellent\n";
} else {
cout << "Not excellent\n";
}
}
return 0;
}
P5744 【Deep Foundation 7. Exercise 9】 Training

#include<iostream>
#include<cstring>
using namespace std;
struct student{
string name;
int age;
int score;
};
// Training
student cultivate(student stu){
// Increase age by 1 year
stu.age += 1;
// Increase score by 20%
stu.score = stu.score + stu.score * 0.2;
if (stu.score > 600){
stu.score = 600;
}
return stu;
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
// Save each student's information into the array
student s = {};
cin >> s.name >> s.age >> s.score;
// Training
s = cultivate(s);
// Output
cout << s.name << " ";
cout << s.age << " ";
cout << s.score << "\n";
}
return 0;
}