[C] 大一课设-简易命令行学习信息管理系统

2024-02-13 11:58

本文主要是介绍[C] 大一课设-简易命令行学习信息管理系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

大一时写的课设,今天翻到,在此保存。
若有帮到您最好,不足之处请多多包涵,欢迎指正错误。

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>/ 学生信息
typedef struct _student{  int num;  //学号char name[20]; //姓名char sex;  //性别int age;  //年龄
}stuDate; 学生表
typedef struct _stuNode{stuDate student;struct _stuNode *next; //下一个
}stuNode; bool
typedef enum{false,true} bool; 颜色函数 
void color(int aim);/光标位移函数
void gotoxy(int x, int y);/ 函数声明
void login();
void menu();
void interaction();
stuNode* addStuNode(stuNode **);
void initStuDate(stuNode *);
bool deleteStuNode(stuNode **,stuNode *);
stuNode* findStuNode(stuNode **,int );
void showStuDate(stuNode **);
void printStuDate(stuNode *);
bool SelectSort(stuNode **,int );
void saveStuDate(stuNode **);
bool fileRead(stuNode **);
void delete(stuNode **);
int judgePass(char *pass);//判断密码是否正确
void againScanf();//重新输入密码
void creatUse();//创建用户
//bool headIsEmpty(stuNode *); 主函数
int main() {login();system("mode con cols=52 lines=30");while ( 1 ) {menu();interaction();}return 0;
}char useName[30];
char pass[30];
 登入界面
void login() {int temp;//界面设置system("mode con cols=52 lines=8");color(12);printf("★★★★★★★★★★★登入★★★★★★★★★★★★\n");printf("★用户名:                                      ★\n");printf("★密  码:                                      ★\n");printf("★                                              ★\n");printf("★★★★★★★★★★★★★★★★★★★★★★★★★\n");color(7);gotoxy(10,1);scanf("%s",useName);//获得用户名if ( getchar()=='\n' ) {gotoxy(10,2);scanf("%s",pass);//输入密码if (getchar()=='\n') {gotoxy(2,3);if ( strlen(useName)>20 || strlen(pass)>20 ) {//判断长度printf("用户名或密码长度错误,即将进入注册界面。。");for (temp=0; temp<5*10e7; temp++) ;creatUse();return ;}else {//判断密码temp = judgePass(pass);//judgePass(pass)if ( temp==-1 ) {//找到不该文件,用户名错误printf("用户名输入错误,即将进入注册界面。。");for (temp=0; temp<5*10e7; temp++) ;creatUse();return ;} else if ( temp==0 ) {//密码错误printf("密码错误。");againScanf();} else {printf("输入正确,即将进入。。。");for (temp=0; temp<5*10e7; temp++) ;//读取历史数据 passreturn ;}}}}
}///  菜单栏
void menu() {color(12);printf("★★★★★★★★★★★菜单栏★★★★★★★★★★★\n");printf("★             1、新建学生档案                  ★\n");   //输入几个printf("★             2、查看学生档案                  ★\n");   //全部、一个printf("★             3、将学生档案排序                ★\n");  //年龄、学号printf("★             4、删除学生档案                  ★\n");   //姓名、学号printf("★             5、保存数据                      ★\n");printf("★             6、安全退出                      ★\n");printf("★★★★★★★★★★★★★★★★★★★★★★★★★\n");color(7);
}/// 用户交互
void interaction() {int command,temp,count;static flag=0;static stuNode *head = NULL ;  //static是重点,不然除menu全部函数接口中head传值要变成三级指针if (flag==0) {//只要读取一次fileRead(&head);flag++;}scanf("%d",&command);switch (command) {case 1:	{//新建学生档案 输入几个printf("将要输入几个学生信息:");scanf("%d",&temp);count = 1;while ( temp-- ) {printf("第%d位学生档案,请输入:\n",count++);initStuDate(addStuNode(&head)); //建立学生信息}break;}		case 2:	{//查看学生档案 全部 一个printf("1、查看全部学生档案 2、查看一个学生档案\n");scanf("%d",&temp);if ( temp==1 ) showStuDate(&head);else if ( temp ==2 ) {printf("  1、按学生学号查找 2、按学生姓名查找\n");scanf("%d",&temp);color(12);printf("该学生档案:");printStuDate(findStuNode(&head,temp));}break;}case 3:	{//排序 年龄 学号printf("1、按年龄排序 2、按学号排序\n");scanf("%d",&temp);if ( SelectSort(&head,temp) ) {printf("排序成功。\n");} else {printf("排序失败,请确认学生信息是否有两个或两个以上。\n");}break;}case 4:	{//删除学生档案  姓名、学号printf("1、按学生学号删除 2、按学生姓名删除\n");scanf("%d",&temp);if ( deleteStuNode(&head,findStuNode(&head,temp)) )printf("删除成功!\n");elseprintf("删除失败。\n");break;}case 5: { //保存数据saveStuDate(&head);break;}case 6: {//安全退出 保存并还空间saveStuDate(&head);delete(&head);exit(0);}}printf("按任意键返回主菜单。\n");getch();system("cls");
}/// 新建学生信息
stuNode* addStuNode(stuNode **pHead) {stuNode *tail,*newNode;newNode = (stuNode *)malloc(sizeof(stuNode));newNode->next = NULL;  //下一个指针为0if ( *pHead==NULL ) {	//如果头指针为0,赋值并退出*pHead = newNode;} else {for ( tail=*pHead; tail->next; tail=tail->next) ; //找到当前最后一个表tail->next = newNode;  //把表接上去}return newNode; //返回创建的空间
}/// 建立信息
void initStuDate(stuNode *aim) {stuDate *pStuDate = &aim->student;printf(" 学号:");scanf("%d",&pStuDate->num);printf(" 姓名:");scanf("%s",pStuDate->name);printf(" 性别:");getchar();  //吃掉一个字符scanf("%c",&pStuDate->sex);printf(" 年龄:");scanf("%d",&pStuDate->age);
} 删除数据
bool deleteStuNode(stuNode **pHead,stuNode *aim) {stuNode *last,*count;if ( aim==NULL ) return false;
//	printf("here");for (count=*pHead,last=NULL; count; last=count,count=count->next) {if ( count==aim ) {if ( last )last->next = count->next;else  //要删除的是第一个*pHead = count->next;free(count);return true;}}return false;
} 查询学生(2姓名  1学号) 普通遍历方法
stuNode* findStuNode(stuNode **pHead,int command) {stuNode *p;char name[20]={0};int num=0;printf("  输入关键词查找:");if (command==1)scanf("%d",&num);else scanf("%s",name);for ( p=*pHead; p; p=p->next)if ( (p->student).num == num || strcmp( (p->student).name,name)==0 ) return p;printf("未找到该学生档案。\n");return NULL;
}///  输出学生信息 全部
void showStuDate(stuNode **pHead) {stuNode *p;if ( *pHead==NULL ) {printf("当前学生档案为空。\n");return ;}color(12);printf("学生档案:\n");printf("学号\t姓名\t性别\t年龄\n");for ( p=*pHead; p; p=p->next)printStuDate(p);
}/// 输出单个学生信息
void printStuDate(stuNode *pNode) {stuDate *pStu = &pNode->student;if ( pNode==NULL ) return ;color(12);printf("%d\t%s\t%c\t%d\n",pStu->num,pStu->name,pStu->sex,pStu->age);color(7);
} 排序(1年龄 2学号) 链表选择排序 小到大
bool SelectSort(stuNode **pHead,int command) {stuNode *cur = *pHead,*p = NULL,*min = NULL;stuDate temp;if ( cur==NULL || cur->next ==NULL )return false;if ( command==1 ) {while ( cur !=NULL ) {p = cur->next;min= cur;while ( p!=NULL ) { //找出最小值if ( min->student.age > p->student.age )min = p;p = p->next;}if ( min != cur ) {temp = cur->student;cur->student = min->student;min->student = temp;}cur = cur->next;}}else if ( command==2 ) {while ( cur !=NULL ) {p = cur->next;min= cur;while ( p!=NULL ) { //找出最小值if ( min->student.num > p->student.num )min = p;p = p->next;}if ( min != cur ) {temp = cur->student;cur->student = min->student;min->student = temp;}cur = cur->next;}}return true;
}///  数据保存
void saveStuDate(stuNode **pHead) {FILE *fp;char fileName[30];stuNode *count;strcpy(fileName,useName);strcat(fileName,".bin");if ( ( fp=fopen(fileName,"wb") )  == NULL  ) {printf("存储失败。");return;}for ( count=*pHead; count; count=count->next)fwrite(&count->student,sizeof(stuDate),1,fp);printf("保存成功。\n");fclose(fp);
}/// 打开数据
bool fileRead(stuNode **pHead) {FILE *fp;char fileName[30];stuNode *last;strcpy(fileName,useName);strcat(fileName,".bin");if ( ( fp=fopen(fileName,"rb") ) == NULL ) {printf("读取失败,请确认是否有历史记录。\n");return false;}while ( !feof(fp) ) {fread(&addStuNode(pHead)->student,sizeof(stuDate),1,fp);}//feof()的问题多取一组数据,暴力删除for (last=*pHead; last->next; last=last->next) ;deleteStuNode(pHead,last);printf("读取成功。\n");fclose(fp);return true;
}/// 程序关闭,还空间
void delete(stuNode **pHead) {stuNode *count=*pHead,*p;while ( count ) {p=count;count=count->next;free(p);}
}///  颜色函数
void color(int aim) {HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ; SetConsoleTextAttribute(hConsole,aim) ;
}// 光标移动函数
void gotoxy(int x, int y) {COORD pos;pos.X = x; pos.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}/// 判断密码
int judgePass(const char *pass) {FILE *fp;char fileName[30],realPass[30];strcpy(fileName,useName);strcat(fileName,".txt");if ( (fp=fopen(fileName,"rb")) ==NULL ) {return -1;//错误用户名}fscanf(fp,"%s",realPass);if ( strcmp(pass,realPass)==0 )return 1;else return 0;
}// 重新输入密码
void againScanf() {int time = 2;while ( time ) {gotoxy(2,3);printf("密码错误,您还有%d次输入密码的机会",time--);gotoxy(10,2);printf("                           ");gotoxy(10,2);scanf("%s",pass);if ( judgePass(pass)==1 ) {//密码正确退出printf("输入正确,即将进入。。。");for (time=0; time<5*10e7; time++) ;return ;}}gotoxy(2,3);printf("三次输入密码错误,即将进入注册界面。。");for (time=0; time<5*10e7; time++) ;creatUse();
}/ 创建用户
void creatUse() {char twoPass[30],fileName[30];int temp;FILE *fp;//用户名、密码输入界面system("mode con cols=54 lines=8");color(12);printf("★★★★★★★★★★ 注册界面 ★★★★★★★★★★★\n");printf("★用 户 名:                                      ★\n");printf("★密    码:                                      ★\n");printf("★确认密码:                                      ★\n");printf("★用户名、密码不能超过20个字符(汉字2个字符计算) ★\n");printf("★★★★★★★★★★★★★★★★★★★★★★★★★★\n");color(7);gotoxy(12,1);scanf("%s",useName);//获得用户名if ( getchar()=='\n' ) {while ( strlen(useName)>20 ) {gotoxy(12,1);printf("用户名超过20个字符                    ");getch();gotoxy(12,1);printf("                                      ");gotoxy(12,1);scanf("%s",useName);}gotoxy(12,2);scanf("%s",pass);//输入密码while ( strlen(pass)>20 ) {gotoxy(12,2);printf("密码超过20个字符                    ");getch();gotoxy(12,2);printf("                                    ");gotoxy(12,2);scanf("%s",pass);}gotoxy(12,3);scanf("%s",twoPass);//输入密码while ( strcmp(pass,twoPass)!=0 ) {gotoxy(12,3);printf("密码与上一次不一致                  ");getch();gotoxy(12,3);printf("                                    ");gotoxy(12,3);scanf("%s",twoPass);}gotoxy(2,4);printf("输入正确,即将进入。。。                        ");for (temp=0; temp<5*10e7; temp++) ;//存放密码.txtstrcpy(fileName,useName);strcat(fileName,".txt");
//printf("%s",fileName);getchar();getchar();if ( ( fp=fopen(fileName,"wb") )== NULL ) {printf("创建文件错误。\n");exit(0);}fprintf(fp,"%s",pass);fclose(fp);//创建存数据的文件.binstrcpy(fileName,useName);strcat(fileName,".bin");
//printf("%s",fileName);getchar();getchar();if ( ( fp=fopen(fileName,"wb") )== NULL ) {printf("创建文件错误。\n");exit(0);}fclose(fp);}//创建文件}

这篇关于[C] 大一课设-简易命令行学习信息管理系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/705472

相关文章

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Python的pip在命令行无法使用问题的解决方法

《Python的pip在命令行无法使用问题的解决方法》PIP是通用的Python包管理工具,提供了对Python包的查找、下载、安装、卸载、更新等功能,安装诸如Pygame、Pymysql等Pyt... 目录前言一. pip是什么?二. 为什么无法使用?1. 当我们在命令行输入指令并回车时,一般主要是出现以

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定