C语言创建一个名为student的结构体数组包含学号姓名分数数据类型自己定义从终端输入学生的信息并打印分数及格的学生信息输入3人即可。封装函数实现把其中第几个学生信息清零。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 10
typedef struct {
int id; // 学号
char name[20]; // 姓名
float score; // 分数
} Student;
Student student[MAX_STUDENT];
int count = 0;
void clearStudent(int index) {
if (index < 0 || index >= count) {
printf("Invalid index.\n");
return;
}
student[index].id = 0;
strcpy(student[index].name, "");
student[index].score = 0;
}
int main() {
int i;
for (i = 0; i < 3; i++) {
printf("Input student %d's id, name and score: ", i + 1);
scanf("%d %s %f", &student[i].id, student[i].name, &student[i].score);
if (student[i].score >= 60) {
printf("Student %d passed.\n", i + 1);
printf("ID: %d, Name: %s, Score: %.1f\n", student[i].id, student[i].name, student[i].score);
}
else {
printf("Student %d failed.\n", i + 1);
}
count++;
}
int index;
printf("Input the index of student to clear: ");
scanf("%d", &index);
clearStudent(index);
for (i = 0; i < count; i++) {
if (student[i].id != 0) {
printf("ID: %d, Name: %s, Score: %.1f\n", student[i].id, student[i].name, student[i].score);
}
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/eRVV 著作权归作者所有。请勿转载和采集!