①示例代码:
#include<iostream>
#include<string>
using namespace std;
struct student {
string name;
double score;
void print();
};
void student::print() {
cout << name << " " << score << endl;
}
int main() {
student stu;
stu.name = "Li Ping";
stu.score = 78.5;
stu.print();
}
②示例代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct student {
string name;
double score;
void print();
};
void student::print() {
cout << name << " " << score << endl;
}
int main() {
vector<student> students; //动态数组; 类似 student students[3];
// cout << students.size(); // 一开始没任何数据,为 0
while (1) {
student stu;
cout << "请输入姓名 分数:\n";
cin >> stu.name >> stu.score;
if (stu.score < 0) break;
students.push_back(stu);
}
for (int i = 0; i < students.size(); i++) {
students[i].print();
}
}
③示例代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct student {
string name;
double score;
void print();
};
void student::print() {
cout << name << " " << score << endl;
}
int main() {
vector<student> students; //动态数组; 类似 student students[3];
// cout << students.size(); // 一开始没任何数据,为 0
while (1) {
student stu;
cout << "请输入姓名 分数:\n";
cin >> stu.name >> stu.score;
if (stu.score < 0) break;
students.push_back(stu);
}
for (int i = 0; i < students.size(); i++) {
students[i].print();
}
double min = 100, max = 0, average = 0;
for (int i = 0; i < students.size(); i++) {
if (students[i].score < min) {
min = students[i].score;
}
if (students[i].score > max) {
max = students[i].score;
}
average += students[i].score;
}
average /= students.size();
cout << "平均分、最高分、最低分:" << average << " " << max << " " << min << endl;
}