c语言学籍管理系统顺序表
时间: 2023-09-02 21:14:22 浏览: 114
以下是一个简单的学籍管理系统的顺序表实现(仅供参考):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char name[20];
int age;
char gender[5];
char id[20];
} Student;
// 定义学籍管理系统结构体
typedef struct {
Student* students; // 学生数组指针
int length; // 学生数组长度
int capacity; // 学生数组容量
} StudentList;
// 初始化学籍管理系统
void initStudentList(StudentList* list) {
list->students = (Student*) malloc(sizeof(Student) * 10); // 初始化数组长度为 10
list->length = 0;
list->capacity = 10;
}
// 打印学生信息
void printStudentInfo(Student* student) {
printf("name: %s, age: %d, gender: %s, id: %s\n", student->name, student->age, student->gender, student->id);
}
// 添加学生
void addStudent(StudentList* list, Student student) {
// 如果学生数组已满,扩容
if (list->length >= list->capacity) {
list->students = (Student*) realloc(list->students, sizeof(Student) * list->capacity * 2);
list->capacity *= 2;
}
// 将新学生添加到数组末尾
list->students[list->length++] = student;
}
// 根据学号查找学生
Student* findStudentById(StudentList* list, char* id) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->students[i].id, id) == 0) {
return &list->students[i];
}
}
return NULL;
}
int main() {
StudentList list;
initStudentList(&list);
// 添加学生
Student s1 = {"张三", 18, "男", "20210001"};
Student s2 = {"李四", 19, "女", "20210002"};
addStudent(&list, s1);
addStudent(&list, s2);
// 查找学生并打印信息
char id[20];
printf("请输入要查找的学生学号:");
scanf("%s", id);
Student* s = findStudentById(&list, id);
if (s != NULL) {
printStudentInfo(s);
} else {
printf("未找到该学生!\n");
}
// 释放内存
free(list.students);
return 0;
}
```
阅读全文