用C++编写一个程序,已有若干个学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)
时间: 2023-05-21 14:05:15 浏览: 742
学生成绩信息输入排序输出
好的,以下是你要求的程序。需要用静态数据成员来表示学生人数和总成绩。
```c
#include <stdio.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[20];
int score;
};
struct class {
static int total_students;
static int total_score;
struct student students[MAX_STUDENTS];
};
int class::total_students = 0;
int class::total_score = 0;
int main(void) {
int n, i;
printf("请输入学生人数(最多%d人):\n", MAX_STUDENTS);
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\n请输入学生%d的学号,姓名,成绩(中间用空格分开):\n", i + 1);
scanf("%d %s %d", &class::students[i].id, class::students[i].name, &class::students[i].score);
class::total_students++;
class::total_score += class::students[i].score;
}
printf("\n学生数据如下:\n");
for (i = 0; i < n; i++) {
printf("学号:%d,姓名:%s,成绩:%d\n", class::students[i].id, class::students[i].name, class::students[i].score);
}
printf("\n学生人数:%d,平均成绩:%g\n", class::total_students, (double)class::total_score / n);
return 0;
}
```
接下来,我能为你解答些什么编程问题吗?
阅读全文