c++编写一个学生成绩管理程序,完成学生信息输入、修改、统计和输出等功能。程序运行后先输入学生人数,动态创建学生信息(结构体)数组,然后循环显示功能操作菜单,提示用户根据菜单选择相应操作。
时间: 2024-02-22 14:59:23 浏览: 39
好的,这是一个比较常见的任务,我会为您提供一些代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
struct Student{
string name;
int 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 >> students[i].score;
}
int option;
while(true){
cout << "请选择操作:\n";
cout << "1. 修改学生信息\n";
cout << "2. 统计总分和平均分\n";
cout << "3. 输出所有学生信息\n";
cout << "4. 退出程序\n";
cin >> option;
if(option==1){
int index;
cout << "请输入要修改的学生序号:";
cin >> index;
if(index>=1 && index<=n){
cout << "请输入修改后的学生姓名和成绩:";
cin >> students[index-1].name >> students[index-1].score;
cout << "修改成功!\n";
}else{
cout << "学生序号不合法!\n";
}
}else if(option==2){
int total = 0;
for(int i=0; i<n; i++){
total += students[i].score;
}
double average = total / (double)n;
cout << "总分:" << total << ",平均分:" << average << endl;
}else if(option==3){
for(int i=0; i<n; i++){
cout << "学生姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}else if(option==4){
cout << "程序已退出。\n";
break;
}else{
cout << "选项不合法,请重新选择。\n";
}
}
delete[] students; // 释放动态创建的结构体数组的内存
return 0;
}
```
以上代码实现了学生成绩管理程序的基本功能,包括学生信息输入、修改、统计和输出等功能。请您根据实际需求进行修改和完善。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)