结构体动态数组学生成绩
时间: 2024-06-18 14:03:48 浏览: 11
结构体动态数组可以用来存储学生成绩信息,比如学生的姓名、学号、各科成绩等。以下是一个示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
struct student {
string name;
int id;
double math_score;
double english_score;
double chinese_score;
};
int main() {
int n; // 学生人数
cout << "请输入学生人数:";
cin >> n;
student* students = new student[n]; // 动态分配内存
// 输入每个学生的信息
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "个学生的姓名:";
cin >> students[i].name;
cout << "请输入第" << i+1 << "个学生的学号:";
cin >> students[i].id;
cout << "请输入第" << i+1 << "个学生的数学成绩:";
cin >> students[i].math_score;
cout << "请输入第" << i+1 << "个学生的英语成绩:";
cin >> students[i].english_score;
cout << "请输入第" << i+1 << "个学生的语文成绩:";
cin >> students[i].chinese_score;
}
// 输出每个学生的信息及总分和平均分
for (int i = 0; i < n; i++) {
double total_score = students[i].math_score + students[i].english_score + students[i].chinese_score;
double avg_score = total_score / 3.0;
cout << "姓名:" << students[i].name << endl;
cout << "学号:" << students[i].id << endl;
cout << "数学成绩:" << students[i].math_score << endl;
cout << "英语成绩:" << students[i].english_score << endl;
cout << "语文成绩:" << students[i].chinese_score << endl;
cout << "总分:" << total_score << endl;
cout << "平均分:" << avg_score << endl;
cout << endl;
}
delete[] students; // 释放内存
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)