#include <iostream>
#include <string>
using namespace std;
struct Student {
string num;
string name;
float score[3];
};
int main()
{
Student students[10];
float sum = 0.0, max = 0.0;
int maxIndex = 0;
cout << "请输入学生信息:" << endl;
for (int i = 0; i < 10; i++)
{
cout << "学生学号:";
cin >> students[i].num;
cout << "学生姓名:";
cin >> students[i].name;
cout << "学生成绩:";
cin >> students[i].score[0] >> students[i].score[1] >> students[i].score[2];
cout << endl;
}
for (int i = 0; i < 10; i++)
{
sum = 0.0;//易忽略
for (int j = 0; j < 3; j++)
{
sum += students[i].score[j];
}
cout <<"学号:"<<students[i].num<< "的平均成绩是:" << sum / 3 << endl;
cout << endl;
//判断是否最高
if (sum / 3 > max)
{
max = sum / 3;
maxIndex = i;
}
}
cout << endl;
cout << "最高分的学生学号是:" << students[maxIndex].num << endl;
cout << "姓名是:" << students[maxIndex].name << endl;
cout << "成绩是:" << students[maxIndex].score[0] << " " << students[maxIndex].score[1] << " " << students[maxIndex].score[2] << endl;
cout << "平均成绩是:" << max << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
struct Student {
string num;
string name;
float* score;
};
int main()
{
Student* students = new Student[10];
for (int i = 0; i < 10; i++)
{
cout << "学生学号:";
cin >> students[i].num;
cout << "学生姓名:";
cin >> students[i].name;
students[i].score = new float[3];
cout << "学生成绩:";
cin >> students[i].score[0] >> students[i].score[1] >> students[i].score[2];
cout << endl;
}
float maxAvg = 0.0;
int maxIndex = 0;
for (int i = 0; i < 10; i++)
{
float sum = 0.0;
for (int j = 0; j < 3; j++)
{
sum += students[i].score[j];
}
float avg = sum / 3;
cout << "平均成绩是:" << avg << endl;
if (avg > maxAvg)
{
maxAvg = avg;
maxIndex = i;
}
}
cout << endl;
cout << "最高分的学生学号是:" << students[maxIndex].num << endl;
cout << "姓名是:" << students[maxIndex].name << endl;
cout << "成绩是:" << students[maxIndex].score[0] << " " << students[maxIndex].score[1] << " " << students[maxIndex].score[2] << endl;
cout << "平均成绩是:" << maxAvg << endl;
for (int i = 0; i < 10; i++)
{
delete[] students[i].score;
}
delete[] students;
return 0;
}