编写一个c语言学生管理系统
时间: 2023-12-26 07:21:57 浏览: 24
好的,我可以为您提供关于学生管理系统的C语言代码。您需要考虑的一些基本功能包括:添加新学生,删除学生,显示学生信息,修改学生信息等。以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student{
char name[20];
int id;
float score;
};
struct node{
struct student data;
struct node *next;
};
void menu();
void add_node(struct node **head);
void delete_node(struct node **head);
void display(struct node *head);
void modify(struct node *head);
int main(){
struct node *head = NULL;
int choice;
do{
menu();
scanf("%d", &choice);
switch(choice){
case 1:
add_node(&head);
break;
case 2:
delete_node(&head);
break;
case 3:
display(head);
break;
case 4:
modify(head);
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}while(choice != 5);
return 0;
}
void menu(){
printf("Welcome to the student management system!\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Display all students\n");
printf("4. Modify student information\n");
printf("5. Exit\n");
printf("Please enter your choice: ");
}
void add_node(struct node **head){
struct node *temp = (struct node*)malloc(sizeof(struct node));
printf("Please enter the student's name: ");
scanf("%s", temp->data.name);
printf("Please enter the student's ID: ");
scanf("%d", &temp->data.id);
printf("Please enter the student's score: ");
scanf("%f", &temp->data.score);
temp->next = *head;
*head = temp;
}
void delete_node(struct node **head){
int id;
struct node *temp = *head;
struct node *prev = NULL;
printf("Please enter the student's ID to delete: ");
scanf("%d", &id);
while(temp != NULL && temp->data.id != id){
prev = temp;
temp = temp->next;
}
if(temp == NULL){
printf("Student not found!\n");
}
else if(prev == NULL){
*head = temp->next;
free(temp);
}
else{
prev->next = temp->next;
free(temp);
}
}
void display(struct node *head){
struct node *temp = head;
printf("Name\tID\tScore\n");
while(temp != NULL){
printf("%s\t%d\t%.2f\n", temp->data.name, temp->data.id, temp->data.score);
temp = temp->next;
}
}
void modify(struct node *head){
int id;
struct node *temp = head;
printf("Please enter the student's ID to modify: ");
scanf("%d", &id);
while(temp != NULL && temp->data.id != id){
temp = temp->next;
}
if(temp == NULL){
printf("Student not found!\n");
}
else{
printf("Please enter the student's new name: ");
scanf("%s", temp->data.name);
printf("Please enter the student's new score: ");
scanf("%f", &temp->data.score);
printf("Student information modified successfully!\n");
}
}
```
这是一个简单的学生管理系统,可以满足基本的添加、删除、修改和显示学生信息的需求。请根据您的具体需求进行修改和完善。