利用结构体类型编写程序,实现输入一个学生的数据(如学号、姓名、性别、3门课程的成绩),计算并输出其平均成绩。简单一点
时间: 2024-02-02 16:05:15 浏览: 14
好的,以下是更简单的 C++ 代码:
```c++
#include <iostream>
using namespace std;
struct Student {
string id; // 学号
string name; // 姓名
char gender; // 性别
double score1; // 第一门课程成绩
double score2; // 第二门课程成绩
double score3; // 第三门课程成绩
};
int main() {
Student stu;
double avg;
cout << "请输入学生的学号、姓名、性别和3门课程的成绩:" << endl;
cin >> stu.id >> stu.name >> stu.gender >> stu.score1 >> stu.score2 >> stu.score3;
avg = (stu.score1 + stu.score2 + stu.score3) / 3.0;
cout << "平均成绩:" << avg << endl;
return 0;
}
```
这段代码与之前的代码相比,省略了一些输出信息。运行程序后,会提示输入学生数据,输入完毕后即可计算并输出平均成绩。
相关问题
利用结构体类型编写程序,实现输入一个学生的数据(如学号、姓名、性别、3门课程的成绩),计算并输出其平均成绩。
好的,以下是实现该功能的 C++ 代码:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义学生结构体类型
struct Student {
string id; // 学号
string name; // 姓名
char gender; // 性别
double score1; // 第一门课程成绩
double score2; // 第二门课程成绩
double score3; // 第三门课程成绩
};
int main() {
Student stu; // 定义一个学生对象
double avg; // 平均成绩
// 输入学生数据
cout << "请输入学生的学号、姓名、性别和3门课程的成绩(用空格分隔):" << endl;
cin >> stu.id >> stu.name >> stu.gender >> stu.score1 >> stu.score2 >> stu.score3;
// 计算平均成绩
avg = (stu.score1 + stu.score2 + stu.score3) / 3.0;
// 输出学生数据和平均成绩
cout << "学生信息如下:" << endl;
cout << "学号:" << stu.id << endl;
cout << "姓名:" << stu.name << endl;
cout << "性别:" << stu.gender << endl;
cout << "第一门课程成绩:" << stu.score1 << endl;
cout << "第二门课程成绩:" << stu.score2 << endl;
cout << "第三门课程成绩:" << stu.score3 << endl;
cout << "平均成绩:" << avg << endl;
return 0;
}
```
运行程序后,会提示输入学生数据,输入完毕后即可计算并输出平均成绩和学生信息。
利用结构体数组编写程序。要求从键盘输入3个 学生的学号、姓名及课程成绩。计算其平均分,并 输出最高分及获得最高分的学生学号,姓名。
下面是一个示例代码,可以实现输入3个学生的学号、姓名及课程成绩,计算平均分,并输出最高分及获得最高分的学生学号、姓名。
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 3
struct student {
int id;
char name[20];
float score;
};
int main() {
struct student stu[MAX_STUDENT];
float average = 0;
float max_score = 0;
int max_index = 0;
for (int i = 0; i < MAX_STUDENT; i++) {
printf("请输入第%d个学生的学号、姓名及成绩:\n", i + 1);
scanf("%d%s%f", &stu[i].id, stu[i].name, &stu[i].score);
average += stu[i].score;
if (stu[i].score > max_score) {
max_score = stu[i].score;
max_index = i;
}
}
average /= MAX_STUDENT;
printf("平均分为:%.2f\n", average);
printf("最高分为:%.2f,获得最高分的学生为:%d %s\n", max_score, stu[max_index].id, stu[max_index].name);
return 0;
}
```
示例输出:
```
请输入第1个学生的学号、姓名及成绩:
1001 Tom 89.5
请输入第2个学生的学号、姓名及成绩:
1002 Jack 92.0
请输入第3个学生的学号、姓名及成绩:
1003 Lucy 87.5
平均分为:89.67
最高分为:92.00,获得最高分的学生为:1002 Jack
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)