写一个c语言写出学生管理系统,支持增删改查
时间: 2023-03-24 14:03:16 浏览: 168
好的,我可以回答这个问题。学生管理系统可以使用结构体来存储学生的信息,包括学号、姓名、性别、年龄、成绩等。可以使用链表来存储多个学生的信息,实现增删改查的功能。具体实现可以参考以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct student {
int id;
char name[MAX_NAME_LEN];
char gender;
int age;
float score;
struct student *next;
} Student;
Student *create_student(int id, char *name, char gender, int age, float score) {
Student *s = (Student *)malloc(sizeof(Student));
s->id = id;
strncpy(s->name, name, MAX_NAME_LEN);
s->gender = gender;
s->age = age;
s->score = score;
s->next = NULL;
return s;
}
void add_student(Student **head, Student *s) {
if (*head == NULL) {
*head = s;
} else {
Student *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = s;
}
}
void delete_student(Student **head, int id) {
if (*head == NULL) {
return;
}
if ((*head)->id == id) {
Student *p = *head;
*head = (*head)->next;
free(p);
return;
}
Student *p = *head;
while (p->next != NULL && p->next->id != id) {
p = p->next;
}
if (p->next != NULL) {
Student *q = p->next;
p->next = q->next;
free(q);
}
}
void update_student(Student *s, char *name, char gender, int age, float score) {
strncpy(s->name, name, MAX_NAME_LEN);
s->gender = gender;
s->age = age;
s->score = score;
}
Student *find_student(Student *head, int id) {
Student *p = head;
while (p != NULL && p->id != id) {
p = p->next;
}
return p;
}
void print_student(Student *s) {
printf("id: %d, name: %s, gender: %c, age: %d, score: %.2f\n", s->id, s->name, s->gender, s->age, s->score);
}
void print_all_students(Student *head) {
Student *p = head;
while (p != NULL) {
print_student(p);
p = p->next;
}
}
int main() {
Student *head = NULL;
add_student(&head, create_student(1, "Tom", 'M', 18, 90.5));
add_student(&head, create_student(2, "Jerry", 'F', 19, 85.));
add_student(&head, create_student(3, "Mike", 'M', 20, 92.5));
print_all_students(head);
delete_student(&head, 2);
print_all_students(head);
Student *s = find_student(head, 3);
if (s != NULL) {
update_student(s, "Michael", 'M', 21, 95.);
}
print_all_students(head);
return ;
}
阅读全文