大一下C语言要交课程设计,于是就写了一个学生信息管理系统,仅供参考
学生信息包括姓名,学号,性别,年龄和各科成绩,可根据需要自行修改添加
包括增、删、改查、查看所有、保存、清空7个功能,每个功能都封装成一个函数
保存功能是将所添加的信息保存到文件中
使用链表存储,包括文件操作,没有学生数量限制,各种信息由结构体封装
各种情况如学号错误、学生不存在等考虑比较完备
缺陷是界面没有优化,可以自行优化
部分运行截图如下:
完整代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct scores{
int math;
int physics;
int English;
int python;
int c;
int sum;
int average;
}scores;
typedef struct students{
char name[10];
char id[15];
char sex[5];
int age;
scores score;
}students;
struct node{
students student;
struct node* next;
};
struct node *head;
void menu(){
printf("学生信息管理系统\n");
printf("1、添加信息\n");
printf("2、删除信息\n");
printf("3、修改信息\n");
printf("4、查找信息\n");
printf("5、查看所有学生信息\n");
printf("6、清空信息\n");
printf("7、保存信息\n");
printf("0、退出系统\n");
}
void add(){
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->next=NULL;
struct node* i=head;
printf("请输入学生姓名:\n");
scanf("%s",new_node->student.name);
printf("请输入学生学号:\n");
scanf("%s",new_node->student.id);
while(i!=NULL){
if(strcmp(i->student.id,new_node->student.id)==0){
printf("该学生已存在!\n");
return;
}
i=i->next;
}
printf("请输入学生性别:\n");
scanf("%s",new_node->student.sex);
printf("请输入学生年龄:\n");
scanf("%d",&new_node->student.age);
printf("请输入学生成绩:\n");
printf("高数:");
scanf("%d",&new_node->student.score.math);
printf("物理:");
scanf("%d",&new_node->student.score.physics);
printf("英语:");
scanf("%d",&new_node->student.score.English);
printf("python:");
scanf("%d",&new_node->student.score.python);
printf("c语言:");
scanf("%d",&new_node->student.score.c);
new_node->student.score.sum=
new_node->student.score.math+
new_node->student.score.physics+
new_node->student.score.English+
new_node->student.score.python+
new_node->student.score.c;
new_node->student.score.average=
new_node->student.score.sum/5.0;
struct node *t=head;
if(head->next==NULL){
head->next=new_node;
new_node->next=NULL;
}
else{
while(t->next!=NULL&&t->next->student.score.sum>new_node->student.score.sum){
t=t->next;
}
if(t->next==NULL){
t->next=new_node;
new_node->next=NULL;
}
else{
new_node->next=t->next;
t->next=new_node;
}
}
printf("添加成功!\n");
}
void delete(){
struct node* t=head,*f;
char d_id[15];
printf("请输入你想要删除的学生学号:\n");
scanf("%s",d_id);
while(t->next!=NULL){
if(strcmp(t->next->student.id,d_id)==0){
if(t->next->next==NULL){
free(t->next);
t->next=NULL;
}
else{
f=t->next;
t->next=f->next;
free(f);
}
printf("删除成功!\n");
return;
}
t=t->next;
}
if(t->next==NULL){
printf("该学生不存在!\n");
}
}
void change(){
struct node* t=head->next;
char c_id[15];
printf("请输入你要修改的学生学号\n");
scanf("%s",c_id);
while(t!=NULL){
if(strcmp(t->student.id,c_id)==0){
printf("请输入学生姓名:\n");
scanf("%s",t->student.name);
printf("请输入学生学号:\n");
scanf("%s",t->student.id);
printf("请输入学生性别:\n");
scanf("%s",t->student.sex);
printf("请输入学生年龄:\n");
scanf("%d",&t->student.age);
printf("请输入学生成绩:\n");
printf("高数:");
scanf("%d",&t->student.score.math);
printf("物理:");
scanf("%d",&t->student.score.physics);
printf("英语:");
scanf("%d",&t->student.score.English);
printf("python:");
scanf("%d",&t->student.score.python);
printf("c语言:");
scanf("%d",&t->student.score.c);
t->student.score.sum=
t->student.score.math+
t->student.score.physics+
t->student.score.English+
t->student.score.python+
t->student.score.c;
t->student.score.average=
t->student.score.sum/5.0;
printf("修改成功!\n");
return;
}
t=t->next;
}
printf("该学生不存在!\n");
}
void search(){
struct node* t=head->next;
printf("请输入你要查找的学生学号:\n");
char s_id[15];
scanf("%s",s_id);
while(t!=NULL){
if(strcmp(t->student.id,s_id)==0){
printf("姓名:%s\n",t->student.name);
printf("学号:%s\n",t->student.id);
printf("性别:%s\n",t->student.sex);
printf("年龄:%d\n",t->student.age);
printf("成绩:\n");
printf("高数:%d\n",t->student.score.math);
printf("物理:%d\n",t->student.score.physics);
printf("英语:%d\n",t->student.score.English);
printf("python:%d\n",t->student.score.python);
printf("c语言:%d\n",t->student.score.c);
printf("总分:%d\n",t->student.score.sum);
printf("平均分:%d\n",t->student.score.average);
return;
}
t=t->next;
}
printf("该学生不存在!\n");
}
void show_all(){
struct node* t=head->next;
if(head->next==NULL){
printf("暂无学生信息!\n");
return;
}
printf("所有学生信息:\n\n");
while(t!=NULL){
printf("姓名:%s\n",t->student.name);
printf("学号:%s\n",t->student.id);
printf("性别:%s\n",t->student.sex);
printf("年龄:%d\n",t->student.age);
printf("成绩:\n");
printf("高数:%d\n",t->student.score.math);
printf("物理:%d\n",t->student.score.physics);
printf("英语:%d\n",t->student.score.English);
printf("python:%d\n",t->student.score.python);
printf("c语言:%d\n",t->student.score.c);
printf("总分:%d\n",t->student.score.sum);
printf("平均分:%d\n",t->student.score.average);
printf("\n");
t=t->next;
}
}
void clear(){
if(head->next==NULL)
printf("暂无学生信息!\n");
else{
struct node* pr=head->next,*la=pr->next;
head->next=NULL;
while(la!=NULL){
free(pr);
pr=la;
la=la->next;
}
free(pr);
printf("清空完成!\n");
}
}
void save(){
FILE* f;
f=fopen("students system.txt","w");
if(f==NULL){
printf("保存失败!\n");
return;
}
struct node* t=head->next;
if(head->next==NULL)
printf("暂无学生信息!\n");
else{
while(t!=NULL){
fprintf(f,"姓名:%s\n",t->student.name);
fprintf(f,"学号:%s\n",t->student.id);
fprintf(f,"性别:%s\n",t->student.sex);
fprintf(f,"年龄:%d\n",t->student.age);
fputs("成绩:\n",f);
fprintf(f,"高数:%d\n",t->student.score.math);
fprintf(f,"物理:%d\n",t->student.score.physics);
fprintf(f,"英语:%d\n",t->student.score.English);
fprintf(f,"python:%d\n",t->student.score.python);
fprintf(f,"c语言:%d\n",t->student.score.c);
fprintf(f,"总分:%d\n",t->student.score.sum);
fprintf(f,"平均分:%d\n",t->student.score.average);
fputs("\n",f);
t=t->next;
}
printf("保存成功!\n");
}
fclose(f);
}
void Input(){
int choice;
scanf("%d",&choice);
switch (choice)
{
case 0:
exit(0);
break;
case 1:
system("cls");
printf("添加学生信息:\n");
add();
system("pause");
system("cls");
break;
case 2:
system("cls");
printf("删除学生信息:\n");
delete();
system("pause");
system("cls");
break;
case 3:
system("cls");
printf("修改学生信息:\n");
change();
system("pause");
system("cls");
break;
case 4:
system("cls");
printf("查找学生信息:\n");
search();
system("pause");
system("cls");
break;
case 5:
system("cls");
show_all();
system("pause");
system("cls");
break;
case 6:
system("cls");
clear();
system("pause");
system("cls");
break;
case 7:
system("cls");
save();
system("pause");
system("cls");
break;
default:
system("cls");
break;
}
}
int main(){
head=(struct node*)malloc(sizeof(struct node));//头节点
head->next=NULL;
while(1){
menu();
Input();
}
return 0;
}