本文主要是介绍[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] 大一课设-简易命令行学习信息管理系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!