C语言项目-学生信息管理系统
项目分析
本套学生信息管理系统非常适合新手练习,主要针对了链表的使用,还有对文本文件读和写操作。学生信息是用文本文件进行存储,程序主体分为六个步骤:增加、删除、修改、查询、输出、退出。
主界面对应着功能操作的数字,用数字按键来执行需要操作的功能,进入功能后若想退出,输入数字"0"可以返回主界面,每次功能执行完毕后,都可以返回主界面进行下一步操作。
功能展示:
1.输出学生信息
进入输出学生信息界面后,可查看学生信息情况,学生信息是以高分到低分进行排序,下面可显示学生最高单科成绩和总成绩。
2. 增加学生信息
进入增加学生信息功能,会提示输入的对应学生信息,依次输入学生信息即可添加成功,添加完成后,返回主题菜单进入输出学生信息界面就可以看到新增加的学员。
3. 删除学生信息
执行删除学生信息功能,需要输入学号进行查询学员,如果学号输入错误会提示“查无此人!”,学号输入正确继续操作即可果断删除学员。
4.修改学生信息
执行修改学生信息功能,同样是以查询学号方式进行操作,上面会显示要修改的学员信息,输入对应的数字可以选择性修改,修改过后,下面会显示修改过后的信息,可以和之前的学员信息相比较。
5.查询学生信息
执行查询学生信息功能后,有两种查询方式,可以选择对应的查询方式对学员进行查询,在学员非常多的情况下,可以方便查询学员信息。
项目源码
头文件"student information.h"
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>typedef struct Node {int id;char name[20]; //姓名char sex[10]; //性别int chinese, math, english; //语 数 英int sum; //总分struct Node* next; //指针域
}node;node List; //链表//读取文件
int readFile(node* L);//保存文件
int saveFile(node* L);//1.主菜单界面
void Welcome();//增加学生信息
void AddStuInfo(); //界面
//将学生信息插入到链表尾部
void insertStuInfo(node* L, node e); //功能//删除学生信息
//界面
void DetStuInfo(node *L);
//功能
void deleteStuInfo(node* pr); //修改学生信息
void FixStuInfo(node *L);//查询学生信息
void SrcStuInfo(node* L);
//按学号进行查询
node* searchStuInfoById(int id, node* L);//按姓名进行查询
node* searchStuInfoByName(char name[], node* L);//输出学生信息
void OptStuInfo(node *L);//退出程序
void goodBye();
主程序文件"Student Information.c"
#include "student information.h"int main()
{int choice = 0;readFile(&List);scanf_s("%d", &choice);while (1){//主菜单函数调用Welcome();scanf("%d", &choice);//1.主菜单界面void Welcome();switch (choice){case 1: //增加学生信息AddStuInfo();break;case 2: //删除学生信息DetStuInfo(&List);break;case 3: //修改学生信息FixStuInfo(&List);break;case 4: //查询学生信息SrcStuInfo(&List);break;case 5: //输出生信息OptStuInfo(&List);break;case 0: //退出程序goodBye();break;}printf("是否还需要继续操作?(yes:1 / no:0):");scanf_s("%d", &choice);if (choice == 0){break;}}return 0;
}// 主菜单界面
void Welcome()
{system("cls");printf("********************************\n");printf("***** 学生成绩管理系统 *****\n");printf("*****----------------------*****\n");printf("***** 1-增加学生信息 *****\n");printf("***** 2-删除学生信息 *****\n");printf("***** 3-修改学生信息 *****\n");printf("***** 4-查询学生信息 *****\n");printf("***** 5-输出学生信息 *****\n");printf("***** 0-退出管理系统 *****\n");printf("********************************\n");printf("请输入对应的功能键(数字):");
}
项目功能文件"Information Processing.c"
#include "student information.h"//增加学生信息
void AddStuInfo()
{//清屏system("cls");node st;printf("请输入新增学生相关信息\n");printf("学号:");scanf("%d", &st.id);printf("姓名:");scanf("%s", st.name);printf("性别:");scanf("%s", st.sex);printf("语文:");scanf("%d", &st.chinese);printf("数学:");scanf("%d", &st.math);printf("英语:");scanf("%d", &st.english);printf("总分:");scanf("%d", &st.sum);st.sum = st.chinese + st.math + st.english;insertStuInfo(&List, st);}//将学生信息插入到链表尾部
void insertStuInfo(node* L, node e)
{//头插法node* h = L;node* s = (node *)malloc(sizeof(node));*s = e;s ->next = h->next;h->next = s;//保存文件saveFile(L);}//删除学生信息
void DetStuInfo(node *L)
{system("cls");int id;node* p;printf("请输入要查找的学生学号:");scanf("%d", &id);node* st = searchStuInfoById(id, L);p = st;if (st == NULL){printf("查无此人!\n");return 0;}st = st->next;printf("-------------------------------------------------------\n");printf("学号\t|姓名\t|性别\t|语文\t|数学\t|英语\t|总分 |\n");printf("-------------------------------------------------------\n");printf("%d|%s\t|%s\t|%d\t|%d\t|%d\t| %d |\t\n", st->id, st->name, st->sex, st->chinese, st->math, st->english, st->sum);printf("-------------------------------------------------------\n");deleteStuInfo(p);saveFile(L);
}//删除学生信息
void deleteStuInfo(node* pr)
{node* s = pr->next;pr->next = s->next;s->next = NULL;free(s);
}//修改学生信息
void FixStuInfo(node *L)
{system("cls");int id;int choice = -1;printf("请输入要查找的学生学号:");scanf("%d", &id);node* st = searchStuInfoById(id, L);if (st == NULL){printf("查无此人!\n");return 0;}st = st->next;while (1){system("cls");printf("-------------------------------------------------------\n");printf("学号\t|姓名\t|性别\t|语文\t|数学\t|英语\t|总分 |\n");printf("-------------------------------------------------------\n");printf("%d|%s\t|%s\t|%d\t|%d\t|%d\t| %d |\t\n", st->id, st->name, st->sex, st->chinese, st->math, st->english, st->sum);printf("-------------------------------------------------------\n");printf("修改姓名 ----- 1\n");printf("修改性别 ----- 2\n");printf("修改语文 ----- 3\n");printf("修改数学 ----- 4\n");printf("修改英语 ----- 5\n");printf("请输入要修改的信息:");scanf("%d", &choice);switch (choice){case 1:printf("请输入姓名:");scanf("%s", st->name);break;case 2:printf("请输入性别:");scanf("%s", st->sex);break;case 3:printf("请输入语文:");scanf("%d", &st->chinese);break;case 4:printf("请输入数学:");scanf("%d", &st->math);break;case 5:printf("请输入英语:");scanf("%d", &st->english);break;}st->sum = st->chinese + st->math + st->english;printf("是否继续修改学生信息?(y-1 / n-0):\n"); scanf("%d", &choice);if (choice == 0){break;}}printf("-------------------------------------------------------\n");printf("学号\t|姓名\t|性别\t|语文\t|数学\t|英语\t|总分 |\n");printf("-------------------------------------------------------\n");printf("%d|%s\t|%s\t|%d\t|%d\t|%d\t| %d |\t\n", st->id, st->name, st->sex, st->chinese, st->math, st->english, st->sum);printf("-------------------------------------------------------\n");//保存文件saveFile(L);
}//查询学生信息
void SrcStuInfo(node* L)
{system("cls");int choice = 0;int id;char name[50];node* st;printf("按学号查询----- 1\n");printf("按姓名查询----- 2\n");printf("请输入查询方式:\n");scanf("%d", &choice);if (choice == 1){printf("请输入要查询的学号:");scanf("%d", &id);st = searchStuInfoById(id,L);if (st==NULL){printf("查无此人!\n");}else{st = st->next;printf("-------------------------------------------------------\n");printf("学号\t|姓名\t|性别\t|语文\t|数学\t|英语\t|总分 |\n");printf("-------------------------------------------------------\n");printf("%d|%s\t|%s\t|%d\t|%d\t|%d\t| %d |\t\n", st->id, st->name, st->sex, st->chinese, st->math, st->english, st->sum);printf("-------------------------------------------------------\n");}}else if (choice == 2){printf("请输入要查询的姓名:");scanf("%s", name);st = searchStuInfoByName(name, L);if (st == NULL){printf("查无此人!\n");}else {st = st->next;printf("-------------------------------------------------------\n");printf("学号\t|姓名\t|性别\t|语文\t|数学\t|英语\t|总分 |\n");printf("-------------------------------------------------------\n");printf("%d|%s\t|%s\t|%d\t|%d\t|%d\t| %d |\t\n", st->id, st->name, st->sex, st->chinese, st->math, st->english, st->sum);printf("-------------------------------------------------------\n");}}
}//按学号进行查询
node* searchStuInfoById(int id, node* L)
{node* p = L;while (p -> next != NULL){if (p -> next -> id == id){return p;}p = p->next;}return NULL;
}//按姓名进行查询
node* searchStuInfoByName(char name[], node* L)
{node* p = L;while (p->next != NULL){if (strcmp(name, p -> next -> name) == 0){return p;}p = p->next;}return NULL;
}//输出成绩最高分
int findMaxScore(node* L, int mode)
{//mode 0:语文 1:数学 2:英语 3:总分node* p = L->next;if (p != NULL){int maxs[4] = { -1, -1, -1, -1 };while (p != NULL){if (p->chinese > maxs[0]) maxs[0] = p->chinese;if (p->math > maxs[1]) maxs[1] = p->math;if (p->english > maxs[2]) maxs[2] = p->english;if (p->sum > maxs[3]) maxs[3] = p->sum;p = p->next;}return maxs[mode];}return -1;
}//排序比较规则
bool cmp(node a, node b)
{return a.sum > b.sum;
}
void stuScoreSort(node *L)
{for (node *p=L->next;p!=NULL;p=p->next){for (node *q=p;q!=NULL;q=q->next){if (!cmp(*p,*q)){//交换数据域node t = *p;*p = *q;*q = t;//处理指针域t.next = p->next;p->next = q->next;q->next = t.next;}}}
}
//输出学生信息
void OptStuInfo(node *L)
{system("cls");//学生成绩从高到低排序stuScoreSort(L);node* p = L->next;printf("-------------------------------------------------------\n");printf("学号\t|姓名\t|性别\t|语文\t|数学\t|英语\t|总分 |\n");printf("-------------------------------------------------------\n");if (p!=NULL){while (p!=NULL){printf("%d|%s\t|%s\t|%d\t|%d\t|%d\t| %d |\t\n", p->id, p->name, p->sex, p->chinese, p->math, p->english, p->sum);printf("-------------------------------------------------------\n");p = p->next;}}printf("最高分:\n");printf("语文:%d\n", findMaxScore(L, 0));printf("数学:%d\n", findMaxScore(L, 1));printf("英语:%d\n", findMaxScore(L, 2));printf("总分:%d\n", findMaxScore(L, 3));
}//退出程序
void goodBye()
{system("cls");printf("欢迎下次使用!\n");exit(0);//结束程序}//文件输入
int readFile(node* L)
{FILE* fpr = fopen("studentinfo.txt", "r");node st;node* s;node* t = L;if (fpr == NULL){return 0;}else {//fcanf()while (fscanf(fpr, "%d %s %s %d %d %d %d", &st.id, st.name, st.sex, &st.chinese, &st.math, &st.english, &st.sum) != EOF){s = (node*)malloc(sizeof(node));*s = st;t->next = s;t = s;t->next = NULL;}}return 1;
}//保存文件
int saveFile(node* L)
{FILE* fpw = fopen("studentinfo.txt","w");if (fpw == NULL) return 0;//fprintf(fpw, "xxx");node* p = L->next;while (p!=NULL){fprintf(fpw, "%d %s %s %d %d %d %d\n", p->id, p->name, p->sex, p->chinese, p->math, p->english, p->sum);p = p->next;}return 1;
}