从C到C++快速入门(9. 学生成绩分析程序)

①示例代码:

#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;
}

在这里插入图片描述

发布了41 篇原创文章 · 获赞 1 · 访问量 490

猜你喜欢

转载自blog.csdn.net/weixin_44773006/article/details/103423086