c 语言 学生结构体有:学号,姓名,英语成绩,计算机成绩. 定义一个5个学生的数组,用函数完成如下功能: 做一个函数,可以录入学生信息 做一个函数,可以显示所有学生信息 做一个函数,可以增加一个学生信息 做一个函数,可以根据学号删除改学生信息 做一个函数,可以通过学号查找学生信息
时间: 2024-06-14 08:05:40 浏览: 73
C语言课设—结构体数组实现学生成绩管理功能.doc
以下是一个用C语言实现的学生结构体数组的功能示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 5
struct Student {
char* studentID;
char* name;
int englishScore;
int computerScore;
};
void inputStudentInfo(struct Student* students, int index) {
students[index].studentID = (char*)malloc(sizeof(char) * 20);
students[index].name = (char*)malloc(sizeof(char) * 20);
printf("请输入学生的学号:");
scanf("%s", students[index].studentID);
printf("请输入学生的姓名:");
scanf("%s", students[index].name);
printf("请输入学生的英语成绩:");
scanf("%d", &(students[index].englishScore));
printf("请输入学生的计算机成绩:");
scanf("%d", &(students[index].computerScore));
}
void displayAllStudents(struct Student* students) {
printf("所有学生的信息如下:\n");
for (int i = 0; i < MAX_STUDENTS; i++) {
printf("学生%d:\n", i + 1);
printf("学号:%s\n", students[i].studentID);
printf("姓名:%s\n", students[i].name);
printf("英语成绩:%d\n", students[i].englishScore);
printf("计算机成绩:%d\n", students[i].computerScore);
printf("\n");
}
}
void addStudent(struct Student* students) {
int index = -1;
for (int i = 0; i < MAX_STUDENTS; i++) {
if (students[i].studentID == NULL) {
index = i;
break;
}
}
if (index == -1) {
printf("学生数组已满,无法添加新学生。\n");
return;
}
inputStudentInfo(students, index);
}
void deleteStudent(struct Student* students, char* studentID) {
int index = -1;
for (int i = 0; i < MAX_STUDENTS; i++) {
if (students[i].studentID != NULL && strcmp(students[i].studentID, studentID) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到学号为%s的学生。\n", studentID);
return;
}
free(students[index].studentID);
free(students[index].name);
students[index].studentID = NULL;
students[index].name = NULL;
}
void searchStudent(struct Student* students, char* studentID) {
int index = -1;
for (int i = 0; i < MAX_STUDENTS; i++) {
if (students[i].studentID != NULL && strcmp(students[i].studentID, studentID) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到学号为%s的学生。\n", studentID);
return;
}
printf("学生的信息如下:\n");
printf("学号:%s\n", students[index].studentID);
printf("姓名:%s\n", students[index].name);
printf("英语成绩:%d\n", students[index].englishScore);
printf("计算机成绩:%d\n", students[index].computerScore);
}
int main() {
struct Student students[MAX_STUDENTS];
for (int i = 0; i < MAX_STUDENTS; i++) {
students[i].studentID = NULL;
students[i].name = NULL;
}
inputStudentInfo(students, 0);
inputStudentInfo(students, 1);
displayAllStudents(students);
addStudent(students);
displayAllStudents(students);
deleteStudent(students, "123");
displayAllStudents(students);
searchStudent(students, "456");
return 0;
}
```
阅读全文