统计学生成绩c语言分支结构
时间: 2024-11-01 17:06:28 浏览: 5
c语言分支结构完整版资料.ppt
统计学生成绩可以使用C语言中的分支结构来实现。可以通过输入学生的各科成绩信息,计算出每个人的总成绩,并根据总成绩进行排名和统计。以下是一个简单的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#define MaxLength 255
//数据元素
typedef struct {
int id;//学号
char name[20];//姓名
int score[5];//英语、政治、专业课1、专业课2的成绩
int total;//总成绩
} studentElement;
//顺序表结构
typedef struct {
studentElement studentArray[MaxLength];
int length;
} seqList;
//初始化函数
int InitList(seqList* seqList) {
int n;
printf("请输入学生人数:");
scanf("%d", &n);
seqList->length = n;
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &seqList->studentArray[i].id);
printf("姓名:");
scanf("%s", seqList->studentArray[i].name);
printf("英语成绩:");
scanf("%d", &seqList->studentArray[i].score[0]);
printf("政治成绩:");
scanf("%d", &seqList->studentArray[i].score[1]);
printf("专业课1成绩:");
scanf("%d", &seqList->studentArray[i].score[2]);
printf("专业课2成绩:");
scanf("%d", &seqList->studentArray[i].score[3]);
seqList->studentArray[i].total = seqList->studentArray[i].score[0] + seqList->studentArray[i].score[1] + seqList->studentArray[i].score[2] + seqList->studentArray[i].score[3];
}
return 1;
}
//统计函数
void Statistics(seqList* seqList) {
int sum = 0;
int max = seqList->studentArray[0].total;
int min = seqList->studentArray[0].total;
for (int i = 0; i < seqList->length; i++) {
sum += seqList->studentArray[i].total;
if (seqList->studentArray[i].total > max) {
max = seqList->studentArray[i].total;
}
if (seqList->studentArray[i].total < min) {
min = seqList->studentArray[i].total;
}
}
printf("总分:%d,平均分:%d,最高分:%d,最低分:%d\n", sum, sum / seqList->length, max, min);
}
int main() {
seqList seqList;
InitList(&seqList);
Statistics(&seqList);
return 0;
}
```
阅读全文