学生信息管理系统c语言源代码
时间: 2023-12-26 07:28:06 浏览: 32
以下是一个简单的学生信息管理系统的C语言源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct Student {
char name[20];
int age;
char gender[10];
char major[20];
} Student;
// 定义学生链表节点
typedef struct StudentNode {
Student student;
struct StudentNode* next;
} StudentNode;
// 创建节点
StudentNode* CreateNode() {
StudentNode* node = (StudentNode*)malloc(sizeof(StudentNode));
node->next = NULL;
return node;
}
// 插入信息
void InsertInfo(StudentNode* head) {
StudentNode* node = CreateNode();
printf("请输入学生姓名:");
scanf("%s", node->student.name);
printf("请输入学生年龄:");
scanf("%d", &(node->student.age));
printf("请输入学生性别:");
scanf("%s", node->student.gender);
printf("请输入学生专业:");
scanf("%s", node->student.major);
node->next = head->next;
head->next = node;
printf("插入成功!\n");
}
// 浏览信息
void BrowseInfo(StudentNode* head) {
if (head->next == NULL) {
printf("暂无学生信息!\n");
return;
}
StudentNode* p = head->next;
while (p != NULL) {
printf("姓名:%s\t年龄:%d\t性别:%s\t专业:%s\n", p->student.name, p->student.age, p->student.gender, p->student.major);
p = p->next;
}
}
// 删除信息
void DeleteInfo(StudentNode* head) {
if (head->next == NULL) {
printf("暂无学生信息!\n");
return;
}
char name[20];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
StudentNode* p = head;
while (p->next != NULL) {
if (strcmp(p->next->student.name, name) == 0) {
StudentNode* temp = p->next;
p->next = temp->next;
free(temp);
printf("删除成功!\n");
return;
}
p = p->next;
}
printf("未找到该学生信息!\n");
}
// 修改信息
void ModifyInfo(StudentNode* head) {
if (head->next == NULL) {
printf("暂无学生信息!\n");
return;
}
char name[20];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
StudentNode* p = head->next;
while (p != NULL) {
if (strcmp(p->student.name, name) == 0) {
printf("请输入学生姓名:");
scanf("%s", p->student.name);
printf("请输入学生年龄:");
scanf("%d", &(p->student.age));
printf("请输入学生性别:");
scanf("%s", p->student.gender);
printf("请输入学生专业:");
scanf("%s", p->student.major);
printf("修改成功!\n");
return;
}
p = p->next;
}
printf("未找到该学生信息!\n");
}
// 查找信息
void SearchInfo(StudentNode* head) {
if (head->next == NULL) {
printf("暂无学生信息!\n");
return;
}
char name[20];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
StudentNode* p = head->next;
while (p != NULL) {
if (strcmp(p->student.name, name) == 0) {
printf("姓名:%s\t年龄:%d\t性别:%s\t专业:%s\n", p->student.name, p->student.age, p->student.gender, p->student.major);
return;
}
p = p->next;
}
printf("未找到该学生信息!\n");
}
// 释放链表内存
void FreeList(StudentNode* head) {
StudentNode* p = head;
while (p != NULL) {
StudentNode* temp = p;
p = p->next;
free(temp);
}
}
// 主函数
int main() {
StudentNode* head = CreateNode();
while (1) {
printf("--学生信息管理系统--\n");
printf("\t\t\t1.插入信息\n");
printf("\t\t\t2.浏览信息\n");
printf("\t\t\t3.删除信息\n");
printf("\t\t\t4.修改信息\n");
printf("\t\t\t5.查找信息\n");
printf("\t\t\t6.退出系统\n");
printf("---\n");
int choice;
printf("请输入操作选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
InsertInfo(head);
break;
case 2:
BrowseInfo(head);
break;
case 3:
DeleteInfo(head);
break;
case 4:
ModifyInfo(head);
break;
case 5:
SearchInfo(head);
break;
case 6:
FreeList(head);
printf("退出系统!\n");
return 0;
default:
printf("无效的选项!\n");
break;
}
system("pause");
system("cls");
}
return 0;
}
```