#define MAX 14
#define BACKSPACE 8
少了 两个宏定义
#define BACKSPACE 8
少了 两个宏定义
#include <stdio.h> #include <string.h> #include <ctype.h> #include <conio.h> const int MaxLength_Password = 14; const int K_BACKSPACE = 0x08; const int K_ENTER = 0x0D; bool Get_Password(const char* password); int main(void) { const char* correct_password = "Hello, world"; printf_s("Please input your password: "); printf_s("\nYour password is %s.\n", Get_Password(correct_password) ? "correct" : "error"); _getch(); return 0; } bool Get_Password(const char* password) { char buffer[MaxLength_Password + 1]; bool escape = false; int counter = 0; while (!escape) { int keypress = _getch(); switch (keypress) { case K_ENTER: buffer[counter] = '\0'; escape = true; break; case K_BACKSPACE: if (counter > 0) { --counter; _putch(K_BACKSPACE); _putch(' '); _putch(K_BACKSPACE); } break; default: if (isgraph(keypress) || (keypress == ' ')) { _putch('*'); buffer[counter++] = keypress; if (counter == MaxLength_Password) { buffer[MaxLength_Password] = '\0'; escape = true; } } break; } } return (strcmp(buffer, password) == 0); }
#include "student.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #define MAX 14 #define BACKSPACE 8 struct StudentInfo//学生信息结构体 { char name[20];//姓名 char num[10];//学号 int Sore[3];//语文 数学 英语 StudentInfo *next; }; StudentInfo stu; StudentUser User; StudentInfo *head=NULL; bool IsExist(char * name, char * pw) { char pname[20]; char mima[20]; FILE * fp = fopen("user.txt", "r"); if (NULL == fp) return 0; while (1) { if (2 != fscanf(fp, "%s%s", pname, mima)) break; if ((strcmp(pname, name) == 0) && (strcmp(mima, pw) == 0)) { fclose(fp); Menu(); return 0; } } fclose(fp); return false; } void getpassword() { char ch; int i=0; while ((ch = getch()) !='\r' && i < MAX) { if (ch == BACKSPACE) { if (i > 0) { User.PassWord[--i] = '\0'; printf("\b "); printf("%c%c%c", BACKSPACE, ' ', BACKSPACE); } else putchar(7); } else { User.PassWord[i++] = ch; printf("*"); } } User.PassWord[i] = '\0'; } int main() { int count = 0; int i = 0; // char ch; while (1) { system("cls"); printf("请输入账号:"); gets(User.UserName); printf("请输入密码:"); getpassword(); gets(User.PassWord); // while ((ch = getch()) !='\r' && i < MAX) // { // if (ch == BACKSPACE) // { // if (i > 0) // { // User.PassWord[--i] = '\0'; // printf("\b "); // printf("%c%c%c", BACKSPACE, ' ', BACKSPACE); // } // else // putchar(7); // } // else // { // User.PassWord[i++] = ch; // printf("*"); // } // } // User.PassWord[i] = '\0'; // fflush(stdout); // fflush(stdin); // puts(User.PassWord); // fflush(stdout); if (IsExist(User.UserName, User.PassWord)) { break; } if (count == 2) { printf("您已经输错三次,系统将自动退出!\n"); break; } printf("帐号或者密码不正确,按回车键重新输入!"); count++; getchar(); } // system("cls"); // printf("请输入账号:"); // gets(User.UserName); // printf("请输入密码:"); // gets(User.PassWord); // if (IsExist(User.UserName, User.PassWord)) // { // printf("您已经输错三次,系统将自动退出!\n"); // } return 0; } //****************************************************************** void ex() { char ch; getchar(); do { printf("您确定要退出吗?(y/n):"); scanf("%c", &ch); } while ((ch != 'Y') && (ch != 'y') && (ch != 'N') && (ch != 'n')); if (ch == 'Y' || ch == 'y') { exit(0); } return; } //********************************************************************** int Menu() { // struct StudentInfo * parr; // int len = 0; int choose; // parr = (struct StudentInfo *)malloc((sizeof(struct StudentInfo) * len)); fflush(stdin); while (1) { system("cls"); printf("*************************\n"); printf("\t1:修改密码\n"); printf("\t2:录入学生信息\n"); printf("\t3:查找学生信息\n"); printf("\t4:修改学生信息\n"); printf("\t5:保存学生信息\n"); printf("\t6:排序\n"); printf("\t7:退出\n"); printf("*************************\n"); printf("请选择1--7 : "); scanf("%d", &choose); switch (choose) { case 1: exmima();break; case 2: insert();break; case 3: selecetlist();break; case 4: change();break; case 5: save();break; case 6: Sort(); break; case 7: ex();break; default: printf("你的选择不正确 请重新选择\n"); } getchar(); // getchar(); } } //******************************************************************** void selecet() { // char name[20]; FILE * fp = fopen("test.txt", "r"); if (!fp) { printf("文件打开失败!\n"); return; } char ch; while ((ch = fgetc(fp)) != EOF) { fputc(ch, stdout); } putchar('\n'); getchar(); getch(); fclose(fp); } //********************************************************************* void Sort() { int choose; fflush(stdin); while (1) { system("cls"); printf("*************************\n"); printf("\t1:按语文成绩排序\n"); printf("\t2:按数序成绩排序\n"); printf("\t3:按英语成绩排序\n"); // printf("\t4:按总分成绩排序\n"); printf("\t4:返回\n"); printf("*************************\n"); printf("请选择1--4 : "); scanf("%d", &choose); switch (choose) { case 1: SelectSort(0);break; case 2: SelectSort(1);break; case 3: SelectSort(2);break; // case 4: break; case 4: Menu();break; default: printf("你的选择不正确 请重新选择\n"); } } system("pause"); } //***************************************************************** void insert()//输入数据 { int len; int i;//控制循环 FILE * fp = fopen("test.txt", "a+"); if (!fp) { printf("文件没有打开!程序结束!"); getchar();//等待用户观察 exit(0); } printf("请输入需要存入的学生人数:"); scanf("%d", &len); for (i = 0; i < len; i++) { printf("请输入学生信息:\n"); printf("姓名:"); scanf("%8s", stu.name); //getchar(); // printf("总成绩: "); // scanf("%s",stu.Sore); getchar(); printf("学号:"); scanf("%5s", stu.num); getchar(); printf("语文成绩: "); scanf("%3d", &stu.Sore[0]); getchar(); printf("数学成绩: "); scanf("%3d", &stu.Sore[1]); getchar(); printf("英语成绩: "); scanf("%3d", &stu.Sore[2]); // else // { // for (int j = 0; j < len; j++)//将数据依次写入文件 // { fprintf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, stu.Sore[0],stu.Sore[1], stu.Sore[2]); // } // } } fclose(fp); system("pause"); printf("\n"); } //************************************************************************ void save() { int len; FILE * fp = fopen("test.txt", "a+"); if (!fp) { printf("文件没有打开!程序结束!"); getchar();//等待用户观察 exit(0); } else { for (int j = 0; j < len; j++)//将数据依次写入文件 { fprintf(fp, "姓名:%8s 学号:%5s 语文成绩:%3s 数学成绩:%3s 英语成绩:%3s\n", stu.name, stu.num, stu.Sore[0], stu.Sore[1], stu.Sore[2]); } } printf("学生数据已经保存!\n"); // 语文成绩:%s 数学成绩:%s 英语成绩:%s // stu.Sore[0],stu.Sore[1],stu.Sore[2] // getch(); fclose(fp); system("pause"); } //******************************************************************** void exmima() { char mima[20]; printf("请输入新的密码:"); scanf("%s", mima); FILE * fp = fopen("user.txt", "r+"); char name1[20]; char mima1[10]; long index; while (1) { index = ftell(fp); if (2 != fscanf(fp, "%s%s", name1, mima1)) break; if (strcmp(User.UserName, name1) == 0) { if (fseek(fp, index, 0)) fprintf(fp, "%s\t%s", User.UserName, mima); else fprintf(fp, "\n%s\t%s", User.UserName, mima); printf("密码修改成功\n"); break; } } system("pause"); fclose(fp); } //**************************************************************** void selecetlist() { int choose; while (1) { system("cls"); printf("*************************\n"); printf("\t1:按姓名查找\n"); printf("\t2:按学号查找\n"); printf("\t3:列出所有学生信息\n"); printf("\t4:返回\n"); printf("*************************\n"); printf("请选择1--4 : "); scanf("%d", &choose); switch (choose) { case 1: selecetname();break; case 2: selecetnum();break; case 3: selecet();break; case 4: Menu();break; default: printf("你的选择不正确 请重新选择\n"); } } system("pause"); } //*************************************************************** void selecetname() { int index; char name[20]; printf("请输入要查找的学生姓名:"); scanf("%s", name); FILE * fp = fopen("test.txt", "r"); if (!fp) { printf("打开文件失败!"); return; } while (1) { index = ftell(fp); if (5 != fscanf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, &stu.Sore[0], &stu.Sore[1], &stu.Sore[2])) { printf("没有这个学生的相关信息.\n"); break; } // fseek(fp, index, 0); if (strcmp(stu.name, name) == 0) { //fseek(fp, index, 0); printf("姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, stu.Sore[0], stu.Sore[1], stu.Sore[2]); break; } // break; } system("pause"); } //****************************************************************** void selecetnum() { int index; char num[10]; printf("请输入要查找的学生学号:"); scanf("%s", num); FILE * fp = fopen("test.txt", "r"); if (!fp) { printf("打开文件失败!"); return; } while (1) { index = ftell(fp); if (5 != fscanf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, &stu.Sore[0], &stu.Sore[1], &stu.Sore[2])) { printf("没有这个学生的相关信息.\n"); break; } // fseek(fp, index, 0); if (strcmp(stu.num, num) == 0) { //fseek(fp, index, 0); printf("姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, stu.Sore[0], stu.Sore[1], stu.Sore[2]); break; } // break; } system("pause"); } //**************************************************************************** void change() { char ch; char num[20]; int index; FILE * fp = fopen("test.txt", "r+"); if(!fp) { printf("无学生记录!\n"); return; } printf("请输入您要修改的学生的学号:"); scanf("%s", num); while (1) { index = ftell(fp); if (5 != fscanf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, &stu.Sore[0], &stu.Sore[1], &stu.Sore[2])) { printf("没有这个学生的相关信息.\n"); break; } if (strcmp(stu.num, num) == 0) { fseek(fp,index,0); printf("姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, stu.Sore[0], stu.Sore[1], stu.Sore[2]); printf("您确定要修改该学生信息(y/n)"); scanf(" %c", &ch); if (ch == 'y'||ch=='Y') { printf("请输入姓名:"); scanf("%s", stu.name); printf("请输入学号:"); scanf("%s", stu.num); printf("请输入语文成绩:"); scanf("%d", &stu.Sore[0]); printf("请输入数学成绩:"); scanf("%d", &stu.Sore[1]); printf("请输入英语成绩:"); scanf("%d", &stu.Sore[2]); fprintf(fp,"姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu.name, stu.num, stu.Sore[0], stu.Sore[1], stu.Sore[2]); printf("修改成功!\n"); // getch(); break; } // else // printf("没有该学生记录,请核对!\n"); } } fclose(fp); system("pause"); } //************************************************************* void SelectSort(int index)//按a排序 { StudentInfo stu1[20]; StudentInfo stu2; int i, j; int temp; // int t; int count = 0; FILE * fp = fopen("test.txt", "r"); if (!fp) { printf("打开文件失败!"); return; } while (1) { if (5 != fscanf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu1[count].name, stu1[count].num, &stu1[count].Sore[0], &stu1[count].Sore[1], &stu1[count].Sore[2])) break; count++; } for (i = 1;i < count;i++) { j = i; temp = stu1[j].Sore[index]; stu2=stu1[j]; while (j >= 1 && temp > stu1[j - 1].Sore[index]) { stu1[j].Sore[index]= stu1[j - 1].Sore[index]; stu1[j]=stu1[j-1]; j--; } stu1[j].Sore[index] = temp; stu1[j]=stu2; } for (i = 0;i < count;i++) { printf("姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu1[i].name, stu1[i].num, stu1[i].Sore[0], stu1[i].Sore[1], stu1[i].Sore[2]); } fclose(fp); system("pause"); } //************************************************************************** void SelectSort1(int index)//按a排序 { StudentInfo stu1[20]; StudentInfo stu2; int i, j; int temp; // int t; int count = 0; FILE * fp = fopen("test.txt", "r"); if (!fp) { printf("打开文件失败!"); return; } while (1) { if (5 != fscanf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu1[count].name, stu1[count].num, &stu1[count].Sore[0], &stu1[count].Sore[1], &stu1[count].Sore[2])) break; count++; } for (i = 1;i < count;i++) { j = i; temp = stu1[j].Sore[index]; stu2=stu1[j]; while (j >= 1 && temp > stu1[j - 1].Sore[index]) { stu1[j].Sore[index]= stu1[j - 1].Sore[index]; stu1[j]=stu1[j-1]; j--; } stu1[j].Sore[index] = temp; stu1[j]=stu2; } for (i = 0;i < count;i++) { printf("姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu1[i].name, stu1[i].num, stu1[i].Sore[0], stu1[i].Sore[1], stu1[i].Sore[2]); } fclose(fp); system("pause"); } //******************************************************************* void SelectSort2(int index)//按a排序 { StudentInfo stu1[20]; StudentInfo stu2; int i, j; int temp; // int t; int count = 0; FILE * fp = fopen("test.txt", "r"); if (!fp) { printf("打开文件失败!"); return; } while (1) { if (5 != fscanf(fp, "姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu1[count].name, stu1[count].num, &stu1[count].Sore[0], &stu1[count].Sore[1], &stu1[count].Sore[2])) break; count++; } for (i = 1;i < count;i++) { j = i; temp = stu1[j].Sore[index]; stu2=stu1[j]; while (j >= 1 && temp > stu1[j - 1].Sore[index]) { stu1[j].Sore[index]= stu1[j - 1].Sore[index]; stu1[j]=stu1[j-1]; j--; } stu1[j].Sore[index] = temp; stu1[j]=stu2; } for (i = 0;i < count;i++) { printf("姓名:%8s 学号:%5s 语文成绩:%3d 数学成绩:%3d 英语成绩:%3d\n", stu1[i].name, stu1[i].num, stu1[i].Sore[0], stu1[i].Sore[1], stu1[i].Sore[2]); } fclose(fp); system("pause"); }我这段代码要怎么才能正确输入密码?我试了输入密码之后回车没反映, 要回车两次才有效果,但是密码就会报错了。
struct StudentUser { char UserName[15]; char PassWord[15]; }; bool IsExist(char *name,char *pw);//验证帐号跟密码 int Menu();//菜单选择函数 void Sort();//排序选择函数 void selecet(); void insert(); void exmima(); void selecetlist(); void selecetname(); void selecetnum(); void save(); void exstu(); void change(); void SelectSort(int index); void SelectSort1(int index); void SelectSort2(int index);这个是上面包含的头文件#include "student.h"