C语言学生管理系统
草木之花 人气:0注意:没有用到数据库使用链表完成此系统!
多文件实现
正式开始
代码都可以直接使用
不想看的,直接复制代码块里面的内容就行!
我用的visual studio 2019 有些使用了 _s 如果是用别的编译器,可以自行修改!
功能介绍
增,删,改,查,退出,保存,以至于格式化!
1.录入学生信息
2.查看录入的学生信息(全部学生信息)
3.修改已录入的学生信息(以学号)
4.删除已录入的学生信息(以学号)
5.保存信息到文件
6.指定查找(以学号)
7.隐藏选项(格式化链表--清空)
'q'退出系统
实现功能
创建源文件main.c 函数部分
//不一一介绍,不懂得去查就行,要学会Google! #include "myList.h" //引入自己写得一个头文件 //菜单界面 void menu(void);//函数声明,菜单显示函数. //按钮互动 void keydown(struct Node* List); int main(void) { struct Node* List = createrList();//创建一个叫List的链表 readInfoFromFile(List, "student.txt");//读取在student.txt的文件 然后写入List链表中 while (true) {//一直循环,知道用户不用这个程序后,输入'q' 退出程序! //显示菜单 menu(); //然后读取用户输入的值,进行操作! keydown(List); system("pause");//暂停程序用的 system("cls");//执行完一次,就清屏一次,看起来比较舒服 } system("pause");//不闪退!同上面作用 return 0; } //这里是用一个结构体,到时用来储存我们的提示信息 //结构体 把菜单要输入的内容都放进这里面 //功能 struct hint_menu {//桌面菜单使用! char one_menu[25]; char two_menu[30]; char three_menu[30]; char four_menu[30]; char five_menu[25]; char six_menu[25]; char seven_menu[25]; }; //桌面菜单 void menu(void) { //使用指针 chosen 指向我们的结构体 ,然后给它动态分配空间 // 类型 分配的大小 struct hint_menu* chosen = (struct hint_menu*)malloc(sizeof(struct hint_menu)); if (chosen) {//这里的if()可写可不写,我写了是划分代码块好看点 //存入要输入内容 //这里修改内容即可 , 想添加就去结构体那先添加一下数组 strcpy_s(chosen->one_menu, sizeof(chosen->one_menu), "1.录入学生信息"); strcpy_s(chosen->two_menu, sizeof(chosen->two_menu), "2.查看已录入的学生信息"); strcpy_s(chosen->three_menu, sizeof(chosen->three_menu), "3.修改已录入的学生信息"); strcpy_s(chosen->four_menu, sizeof(chosen->four_menu), "4.删除已录入的学生信息"); strcpy_s(chosen->five_menu, sizeof(chosen->five_menu), "5.保存至文件"); strcpy_s(chosen->six_menu, sizeof(chosen->six_menu), "6.指定查找"); strcpy_s(chosen->seven_menu, sizeof(chosen->seven_menu), "q.退出系统"); //其实这样比较麻烦 //你们可以直接定义一个字符串 //char inset[20] = "1.录入学生信息"; 以此类推 //输出你存入的内容 printf("\n\n\n\n"); printf("\t\t\t\t******************欢迎进入学生管理系统*******************\n"); printf("\t\t\t\t*\t\t %s\t\t\t\t*\n", chosen->one_menu); printf("\t\t\t\t*\t\t %s \t\t*\n", chosen->two_menu); printf("\t\t\t\t*\t\t %s \t\t*\n", chosen->three_menu); printf("\t\t\t\t*\t\t %s \t\t*\n", chosen->four_menu); printf("\t\t\t\t*\t\t %s \t\t\t\t*\n", chosen->five_menu); printf("\t\t\t\t*\t\t %s \t\t\t\t*\n", chosen->six_menu); printf("\t\t\t\t*\t\t %s \t\t\t\t*\n", chosen->seven_menu); printf("\t\t\t\t*********************************************************\n"); printf("\t\t\t\t请输入你的选项1~6 and (q quit program):"); fflush(stdout);//刷新输出缓冲流 } } //功能实现 ,按钮互动 void keydown(struct Node* List) { struct student info; char num[12]; char choose,ch; choose = enter(); switch (choose) { case '1': printf("\t\t\t\t******************录入学生信息******************\n"); printf("\t\t\t\t请输入要录入的学生:学号\t姓名\t性别\t年龄\t电话\t\n"); printf("\t\t\t\t请输入学号:"); scanf_s("%s",info.number, (unsigned int)sizeof(info.number)); printf("\t\t\t\t请输入姓名:"); scanf_s("%s",info.name,(int)sizeof(info.name)); printf("\t\t\t\t请输入%s的性别:",info.name); scanf_s("%s",info.gender,(int)sizeof(info.gender)); printf("\t\t\t\t请输入%s的年龄:", info.name); scanf_s("%hd", &info.age); printf("\t\t\t\t请输入%s的电话:", info.name); scanf_s("%s", info.tel, (int)sizeof(info.tel)); insertNodeByHead(List, info); printf("\t\t\t\t录入完成! Done!"); break; case '2': printf("\t\t\t\t***************查看已录入学生信息************\n"); printfNode(List); printf("\t\t\t\t一共有%d个人\n", LengthNode(List)); break; case '3': printf("\t\t\t\t******************修改学生信息*******************\n"); printfNode(List); printf("\t\t\t\t请输入需要修改的学生学号:"); scanf_s("%s", num, (unsigned int)sizeof(num)); upDataNode(List,num); break; case '4': printf("\t\t\t\t******************删除学生信息*******************\n"); printfNode(List); printf("\t\t\t\t请输入需要删除的学生学号:"); scanf_s("%s",num,(unsigned int)sizeof(num)); deteleNodeAppoinNumber(List, num); break; case '5': printf("\t\t\t\t******************保存至文件*******************\n"); weiteInfoToFile(List, "use_stu.txt"); printf("\t\t\t\t备份完成\n"); printf("\t\t\t\tDone\n"); break; case '6': printf("\t\t\t\t******************指定位置查找*******************\n"); printf("\t\t\t\t请输入想要查找的学生学号:"); scanf_s("%s",num, (unsigned int)sizeof(num)); printfToInput(List,num); printf("\t\t\t\t\t\t\t\tDone\n"); break; case '7': printf("\t\t\t\t******************格式化链表*******************\n"); printf("\t\t\t\t*********************************\n"); printf("\t\t\t\t******************注意***********\n"); printf("\t\t\t\t********此操作无法撤回!**********\n"); printf("\t\t\t\t(确认请输入[Y] 取消请选择[q]):"); ch = enter(); switch (ch) { case 'Y': formattedLinkedList(List); weiteInfoToFile(List, "student.txt"); break; case 'q': printf("\t\t\t\t退出成功"); return; break; default: printf("\t\t\t\t(确认请输入[Y] 取消请选择[q]):"); break; } break; case 'q': printf("\t\t\t\t正常退出系统成功\n"); exit(0); break; default: printf("\n\t\t\t\t请重新输入(1~5 and (q quit program))\n"); break; } weiteInfoToFile(List,"student.txt"); }
创建源文件头文件 enter.h 部分
#pragma once //防止重复引用 #include "myList.h" //处理写入 char enter(void); //函数声明 char enter(void) { short count = 1;//次数 char input = getchar(); // 读取单个字符 fflush(stdin);//清空输入缓存区,防止读取后,又读取 for (int i = 1; i <= 12; i++) {//如果超过误输入超过13次,强制退出程序 if (input == '\n') {//如果读取的一直是回车,就会执行,否则返回该值 count++; scanf_s("%c", &input, 3); fflush(stdin); if (count == 5) { printf("\n\t\t\t\t\t\t别再调皮了!\n"); continue; } else if (count == 11) { printf("\n\t\t\t\t\t\t别在摁回车键了!最后一次机会了\n"); continue; } else if (count == 13) { printf("\n\t\t\t\t\t\t程序已强制退出!byebye"); exit(0); } } else { return input; } } return 0; }
重头戏来咯
创建头文件 myList.h
#pragma once //前面没有引用是应为这里都引用了,所以引用一次头文件就欧克了 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <ctype.h> #include "enter.h" //定义一个学生类型的结构体 struct student { char name[20]; //姓名 char gender[3];//性别 char number[12]; //学号 char tel[12];//电话号码 short age;//年龄 //需要可以在添加 }; //结点 struct Node { struct student data; //数据域 struct Node* next; //指针域 }; //创建链表 struct Node* createrList(void) { struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); if (headNode) { //初始化 headNode->next = NULL; } return headNode; } //创建结点 struct Node* createNode(struct student data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode) { //把数据存进去 newNode->data = data; newNode->next = NULL; } return newNode; } //插入结点 参数:插入哪个链表 插入结点的数据是多少 void insertNodeByHead(struct Node* headNode, struct student data) { //创建结点 struct Node* newNode = createNode(data);//赋值 //使插入的结点接在 headNode后面 newNode->next = headNode->next; headNode->next = newNode; } //打印链表 也就是遍历 void printfNode(struct Node* headNode) { struct Node* pMove = headNode->next; printf("\t\t\t\t\t学号\t姓名\t性别\t年龄\t电话\n"); while (pMove != NULL) { printf("\n\t\t\t\t\t%06s\t%s\t%s\t%hd\t%s", pMove->data.number, pMove->data.name, pMove->data.gender, pMove->data.age, pMove->data.tel); pMove = pMove->next; } printf("\n"); } //指定位置删除 void deteleNodeAppoinNumber(struct Node* headNode, char number[12]) { struct Node* posNode = headNode->next; struct Node* posFrontNode = headNode; if (posNode == NULL) { printf("\t\t\t\t\t\t表中没有参数\n"); } else { while (strcmp(posNode->data.number,number)!=0) {//如果不是的话 posFrontNode = posNode; posNode = posNode->next; if (posNode == NULL) {//找到最后一个也没有找到 printf("\t\t\t\t\t\t表中没有该学号的学生.\n"); return; } } //找到了 posFrontNode->next = posNode->next; free(posNode); printf("\t\t\t\t\t\tDone it!\n"); } } //更新已录入内容 void upDataNode(struct Node* headNode, char number[12]) { struct Node* posNode = headNode->next; struct Node* posFrontNode = headNode; struct student info; char chosen; if (posNode == NULL) { printf("\t\t\t\t\t\t无法修改,该表里面没有内容\n"); } else { while (strcmp(posNode->data.number,number)!=0) { posFrontNode = posNode; posNode = posNode->next; if (posNode == NULL) { printf("\t\t\t\t\t\t该表中没有此学号的学生,无法修改\n"); return; } } while (true) { printf("\t\t\t\t\t\t请输入要修改什么选项:" "\n\t\t\t\t\t\t(1.学号\t2.姓名\t3.性别\t4.年龄\t5.电话)" "\n\t\t\t\t\t\t(如果不需要了选择'Q')\n-->:"); chosen = enter(); fflush(stdin); switch (chosen) { case '1': printf("\t\t\t\t\t\t请输入需要更改的学生信息:\n"); printf("\t\t\t\t\t\t请输入学号:"); scanf_s("%s", info.number,(int)sizeof(info.number)); strcpy_s(posNode->data.number, sizeof(posNode->data.number),info.number); printf("\t\t\t\t\t\tDone!\n"); break; case '2': printf("\t\t\t\t\t\t请输入需要更改的学生信息:\n"); printf("\t\t\t\t\t\t请输入姓名:"); scanf_s("%s", info.name, (int)sizeof(info.name)); fflush(stdin); strcpy_s(posNode->data.name, sizeof(posNode->data.name), info.name); printf("\t\t\t\t\t\tDone!\n"); break; case '3': printf("\t\t\t\t\t\t请输入%s的性别:", posNode->data.name); scanf_s("%s", info.gender, (int)sizeof(info.gender)); fflush(stdin); strcpy_s(posNode->data.gender, sizeof(posNode->data.gender), info.gender); printf("\t\t\t\t\t\tDone!\n"); break; case '4': printf("\t\t\t\t\t\t请输入%s的年龄:", posNode->data.name); scanf_s("%hd", &info.age); posNode->data.age = info.age; printf("\t\t\t\t\t\tDone!\n"); break; case '5': printf("\t\t\t\t\t\t请输入%s的电话:", posNode->data.name); scanf_s("%s", info.tel, (int)sizeof(info.tel)); fflush(stdin); strcpy_s(posNode->data.tel, sizeof(posNode->data.tel), info.tel); printf("\t\t\t\t\t\tDone!\n"); break; case'Q': printf("\t\t\t\t\t\t退出此选项"); return; default: printf("\n\t\t\t\t\t\t请重新输入(1~5 and (q quit ))\n"); break; } } } } //指定位置查看 void printfToInput(struct Node* headNode, char number[12]) { struct Node* posNode = headNode->next; struct Node* posFrontNode = headNode; if (posNode == NULL) { printf("\t\t\t\t\t\t该表为空\n"); }else { while (strcmp(posNode->data.number,number)!=0) { posFrontNode = posNode; posNode = posNode->next; if (posNode == NULL) { printf("\t\t\t\t\t\t没有找到该学生\n"); return; } } printf("\t\t\t\t\t\t学号\t姓名\t性别\t年龄\t电话\n"); printf("\n\t\t\t\t\t\t%06s\t%s\t%s\t%hd\t%s", posNode->data.number, posNode->data.name, posNode->data.gender, posNode->data.age, posNode->data.tel); } printf("\n"); } //读文件 bool readInfoFromFile(struct Node* headNode, char* fileName) { struct student data; //打开文件 FILE* fp; fopen_s(&fp, fileName, "r"); if (fp == NULL) { fopen_s(&fp, fileName, "w+"); } //2操作 if (fp == NULL) { return EOF; } while (fscanf_s(fp, "%s\t%s\t%s\t%hd\t%s", data.number,(int)sizeof(data.number), data.name, (int)sizeof(data.name), data.gender, (int)sizeof(data.gender), &data.age, data.tel, (int)sizeof(data.tel)) != EOF) { insertNodeByHead(headNode, data); } //关闭文件 if (fp == NULL) { return EOF; } fclose(fp); return 0; } //写文件 bool weiteInfoToFile(struct Node* headNode, char* fileName) { FILE* fp; fopen_s(&fp, fileName, "w"); struct Node* pMove = headNode->next; while (pMove) { if (fp == NULL) { return EOF; } fprintf_s(fp, "\n\t\t\t\t\t\t %s\t%s\t%s\t%hd\t%s", pMove->data.number, pMove->data.name, pMove->data.gender, pMove->data.age, pMove->data.tel); pMove = pMove->next; } if (fp == NULL) { return EOF; } fclose(fp); return 0; } //求链表长度 int LengthNode(struct Node* headNode) { int lenth = 0; struct Node* pMove = headNode->next; while (pMove) { lenth++; pMove = pMove->next; } return lenth; } //格式化模式! void formattedLinkedList(struct Node* headNode) { struct Node* posNode ; if (headNode == NULL) { printf("\t\t\t\t\t\t该表为空\n"); } else { while (headNode != NULL) { posNode = headNode->next; free(headNode); headNode = posNode; } } }
加载全部内容