From 92b0beaa68c6eb02ec0402fd25d3b41bbc462ae5 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 10 Jan 2019 10:43:13 +0800 Subject: [PATCH] 3 commit --- class01.c | 643 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ shl02.c | 263 ++++++++++++++++++++++ 2 files changed, 906 insertions(+) create mode 100644 class01.c create mode 100644 shl02.c diff --git a/class01.c b/class01.c new file mode 100644 index 0000000..785a800 --- /dev/null +++ b/class01.c @@ -0,0 +1,643 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + +unsigned int counts = 0; + + + +/*结构体定义*/ +typedef struct student { + char no[21]; + char name[11]; + bool fame; + unsigned short int age; + char oth[151]; + struct student * next; +}student; + +struct student * first = NULL;//第一个 +struct student * loc = NULL;//当前(一般为NULL) + +void input(void);//输入 +void print(void);//打印所有 +void print_1(const student *);//打印单个 +bool del(char *);//删除单个 +void creat(const student);//创造单个 +bool edit(char*);//编辑单个 +void save(void);//全部保存 +student * search_by_no(char *);//按学号查找 +void search_by_name(char *);//按名字搜索 +void search_by_sex(bool);//按性别搜索 +void search_by_age(unsigned short int);//按年龄搜索 +void show_menu(void);//显示菜单 +void exith(void);//退出(清理程序内容) +bool check_password(void);//检查密码 +void run(void);//程序开始接入点 +bool load(void);//从文件加载数据 +void fgets_nn(char * , int, FILE *);//丢弃换行符的整行char*输入 +bool check_no(char *);//检查学号是否冲突 + +int main() { + run(); + system("pause"); + return 0; +} + +void show_menu(void) { + printf("a.学生基本信息录入\n"); + printf("b.学生基本信息显示\n"); + printf("c.学生基本信息保存\n"); + printf("d.学生基本信息删除\n"); + printf("e.学生基本信息修改\n"); + printf("f.学生基本信息查询\n"); + printf("g.退出系统\n"); + char t = getch(); + switch (t) { + case 'a': + input(); + show_menu(); + break; + case 'b': + system("cls"); + print(); + system("pause"); + system("cls"); + show_menu(); + break; + case 'c': + save(); + system("cls"); + printf("保存完成!\n"); + show_menu(); + break; + case 'd': + { + char no_t[21]; + system("cls"); + print(); + printf("\n请输入要删除的学生的学号:\n"); + while (1) { + fgets_nn(no_t, 21, stdin); + if (!strcmp(no_t, "\0")) { + printf("学号不能为空:\n"); + continue; + } + break; + } + if (del(no_t)) { + system("cls"); + printf("删除成功!\n\n"); + } + else { + system("cls"); + printf("删除失败!不存在该学号的学生!\n\n"); + } + show_menu(); + break; + } + case 'e': + { + char no_t[21]; + system("cls"); + if (!check_password()) { + system("cls"); + printf("密码验证失败!\n"); + show_menu(); + } + system("cls"); + print(); + printf("\n请输入要修改的学生的学号:\n"); + while (1) { + fgets_nn(no_t, 21, stdin); + if (!strcmp(no_t, "\0")) { + printf("学号不能为空:\n"); + continue; + } + break; + } + if (edit(no_t)) { + system("cls"); + printf("修改成功!\n\n"); + } + else { + system("cls"); + printf("修改失败!不存在该学号的学生\n\n"); + } + show_menu(); + break; + } + case 'f': + { + system("cls"); + char mod; + while (1) { + printf("请输入模式:\n"); + printf("(1)按学号查询\n"); + printf("(2)按姓名查询\n"); + printf("(3)按性别查询\n"); + printf("(4)按年龄查询\n"); + mod = getch(); + if (mod - '0' < 1 || mod - '0'>4) { + system("cls"); + printf("输入错误\n\n"); + continue; + } + system("cls"); + break; + } + switch (mod - '0') { + case 1: + {//建立一个块,防止case内定义被编译器报错 + char tem[21]; + printf("\n请输入要查询的学生的学号:\n"); + while (1) { + fgets_nn(tem, 21, stdin); + if (!strcmp(tem, "\0")) { + system("cls"); + printf("学号不能为空,请输入要查询的学生的学号:\n"); + continue; + } + break; + } + student *ge = search_by_no(tem); + if (ge == NULL) { + printf("不存在该学号的学生!请检查输入的内容!\n"); + } + else { + system("cls"); + printf("查询到学号为%s的学生如下:\n\n", tem); + print_1(ge); + } + break; + } + case 2: + { + char name[11]; + printf("请输入学生姓名:\n"); + do { + fgets_nn(name, 11, stdin); + } while (!strcmp(name, "\0")); + search_by_name(name); + break; + } + case 3: + { + bool fame; + while (1) { + printf("请输入学生性别(1.男 2.女):\n"); + char t = getch(); + if (t == '1') { + fame = true; + break; + } + else if (t == '2') { + fame = false; + break; + } + else { + system("cls"); + printf("输入不正确,请重新输入学生性别(1.男 2.女):\n"); + continue; + } + } + search_by_sex(fame); + break; + } + case 4: + { + unsigned short int age; + while (1) { + printf("请输入学生年龄:\n"); + if (scanf("%hd", &age) != 1) { + system("cls"); + printf("输入不正确,请重新输入学生年龄:\n"); + while (getchar() != '\n')continue; + continue; + } + break; + } + search_by_age(age); + break; + } + } + system("pause"); + system("cls"); + show_menu(); + break; + } + case 'g': + save(); + system("cls"); + printf("数据已保存,正在退出系统!\n"); + exith(); + break; + default: + system("cls"); + printf("输入错误!\n\n"); + show_menu(); + break; + } +} + +void input(void) { + student temp; + char t; + while (1) { + system("cls"); + printf("请输入学生学号,退出输入请直接回车:\n"); + while (1) { + fgets_nn(temp.no, 21, stdin); + if (!check_no(temp.no)) + printf("学号已存在!\n"); + else + break; + } + if (!strcmp(temp.no, "\0")) { + return; + } + printf("请输入学生姓名:\n"); + do { + fgets_nn(temp.name, 11, stdin); + } while (!strcmp(temp.name, "\0")); + while (1) { + printf("请输入学生性别(1.男 2.女):\n"); + t = getch(); + if (t == '1') { + temp.fame = true; + printf("男\n"); + break; + } + else if (t == '2') { + temp.fame = false; + printf("女\n"); + break; + } + else { + printf("输入不正确\n"); + continue; + } + } + while (1) { + printf("请输入学生年龄:\n"); + if (scanf("%hd", &temp.age) != 1) { + printf("输入不正确\n"); + while (getchar() != '\n')continue; + continue; + } + break; + } + printf("备注:\n"); + while (getchar() != '\n')continue; + fgets_nn(temp.oth, 151, stdin); + creat(temp); + system("pause"); + counts++; + save(); + } + +} + +void creat(const student t) { + if (first == NULL) { + first = (student*)malloc(sizeof(student)); + loc = first; + first->next = NULL; + } + else { + loc->next= (student*)malloc(sizeof(student)); + loc = loc->next; + loc->next = NULL; + } + loc->age = t.age; + loc->fame = t.fame; + int i=0; + for(i=0;i<11&&t.name[i>0?i-1:0]!='\0';i++) + loc->name[i] = t.name[i]; + for (i = 0; i < 21&&t.no[i > 0 ? i - 1 : 0] != '\0'; i++) + loc->no[i] = t.no[i]; + for (i = 0; i < 151; i++) + loc->oth[i] = t.oth[i]; +} + +void print(void) { + student * l = first; + printf("系统内共有%d名学生数据:\n\n", counts); + while (l != NULL) { + print_1(l); + printf("\n"); + l = l->next; + } +} + +void print_1(const student * t) { + printf("学号:%s\n", t->no); + printf("姓名:%s\n", t->name); + printf("性别:%s\n", t->fame ? "男": "女"); + printf("年龄:%d\n", t->age); + printf("备注:%s\n", t->oth); +} + +void exith(void) { + student *l = first; + student n; + while (l != NULL) { + n = *l; + free(l); + l = n.next; + } + printf("系统已退出!\n"); + system("pause"); + exit(1); +} + +void run(void) { + int n=3; + while (n--) { + if (check_password()) { + load(); + show_menu(); + } + else if(n!=0){ + printf("密码错误,你还有%d次机会!\n",n); + } + } + printf("身份验证失败,正在退出系统\n"); + exith(); +} + +void save(void) { + FILE * data; + data = fopen("data", "w"); + student * l = first; + while (l != NULL) { + fprintf(data, "%s\n", l->no); + fprintf(data, "%s\n", l->name); + fprintf(data, "%d\n", l->fame); + fprintf(data, "%d\n", l->age); + fprintf(data, "%s\n", l->oth); + l = l->next; + } + fclose(data); +} + +bool load(void) { + FILE * data; + data = fopen("data", "r"); + if (data == NULL) + return false; + student temp; + while (1) { + fgets_nn(temp.no, 21, data); + if (feof(data)){ + break; + } + fgets_nn(temp.name, 11, data); + fscanf(data, "%hhu", &temp.fame); + fscanf(data, "%hd", &temp.age); + fgetc(data);//去除留下的\n + fgets_nn(temp.oth, 151, data); + creat(temp); + counts++; + } + fclose(data); + return true; +} + +bool del(char * no) { + + student *ed = search_by_no(no); + if (ed == NULL) + return false; + student temp; + if (ed == first) { + temp = *first; + free(first); + first = temp.next; + return true; + } + student *lu = first; + while (lu->next != NULL) { + if (strlen(lu->next->no) == strlen(no) && !strcmp(lu->next->no, no)) { + break; + } + lu = lu->next; + } + temp = *lu->next; + free(lu->next); + lu->next = temp.next; + counts--; + save(); + return true; +} + +bool check_password(void) { + FILE * password; + password = fopen("password", "r"); + if (password == NULL) { + printf("第一次进入系统,请先设置密码(最长16位,最短6位):\n"); + password = fopen("password", "w"); + system("attrib password +s +h +r"); + char pw[17]; + char pw2[17]; + while (1) { + fgets_nn(pw, 17, stdin); + if (strlen(pw) < 6) { + system("cls"); + printf("密码至少设置6位:\n"); + continue; + } + system("cls"); + printf("请确认密码:\n"); + fgets_nn(pw2, 17, stdin); + if (strcmp(pw, pw2)||(strlen(pw)!=strlen(pw2))) { + system("cls"); + printf("密码确认错误,请重新设置密码:\n"); + continue; + } + break; + } + fprintf(password, "%s", pw); + fclose(password); + system("cls"); + return true; + } + char pw[17]; + fgets_nn(pw, 17, password); + if (feof(password)) { + fclose(password); + system("attrib password -s -h -r"); + remove("password"); + return check_password(); + } + printf("请输入密码:\n"); + char pwi[17]; + fgets_nn(pwi, 17, stdin); + system("cls"); + if (!strcmp(pw, pwi)) + return true; + else + return false; +} + +student * search_by_no(char * no) { + student *l = first; + while (l != NULL) { + if (strlen(l->no) == strlen(no) && !strcmp(l->no, no)) { + return l; + } + l = l->next; + } + return NULL; +} + +void search_by_name(char * name) { + student *l = first; + unsigned int c = 0; + system("cls"); + while (l != NULL) { + if (strlen(l->name) == strlen(name) && !strcmp(l->name, name)) { + print_1(l); + printf("\n"); + c++; + } + l = l->next; + } + if (c == 0) { + printf("未检索到内容!\n"); + } + else { + printf("\n已检索到%d条名字为%s的学生\n\n", c, name); + } +} + +void search_by_sex(bool fame) { + student *l = first; + unsigned int c = 0; + system("cls"); + while (l != NULL) { + if (l->fame == fame) { + print_1(l); + printf("\n"); + c++; + } + l = l->next; + } + if (c == 0) { + printf("未检索到内容!\n"); + } + else { + printf("\n已检索到%d条性别为%s的学生\n\n", c, fame ? "男" : "女"); + } +} + +void search_by_age(unsigned short int age) { + student *l = first; + unsigned int c = 0; + system("cls"); + while (l != NULL) { + if (l->age == age) { + print_1(l); + printf("\n"); + c++; + } + l = l->next; + } + if (c == 0) { + printf("未检索到内容!\n"); + } + else { + printf("\n已检索到%d条年龄为%d的学生\n\n", c, age); + } +} + +bool edit(char * no) { + student *l = search_by_no(no); + if (l == NULL) + return false; + + printf("请输入学生学号:\n"); + while (1) { + char no_temp[21]; + fgets_nn(no_temp, 21, stdin); + if (!strcmp(no_temp, "\0")) { + printf("学号不能为空:\n"); + continue; + } + if (!(strlen(l->no) == strlen(no_temp) && !strcmp(l->no, no_temp))) + if (!check_no(no_temp)) + printf("学号已存在!\n"); + else { + int i=0; + for ( i = 0; i < 21; i++) + l->no[i] = no_temp[i]; + break; + } + else + break; + } + printf("请输入学生姓名:\n"); + do { + fgets_nn(l->name, 11, stdin); + } while (!strcmp(l->name, "\0")); + while (1) { + printf("请输入学生性别(1.男 2.女):\n"); + char t = getch(); + if (t == '1') { + l->fame = true; + break; + } + else if (t == '2') { + l->fame = false; + break; + } + else { + printf("输入不正确\n"); + continue; + } + } + while (1) { + printf("请输入学生年龄:\n"); + if (scanf("%hd", &l->age) != 1) { + printf("输入不正确\n"); + while (getchar() != '\n')continue; + continue; + } + break; + } + printf("备注:\n"); + while (getchar() != '\n')continue; + fgets_nn(l->oth, 151, stdin); + + save(); + return true; +} + +void fgets_nn(char *buffer, int max, FILE * file) { + fgets(buffer, max, file); + int i=0; + for (i = 0; i < max; i++) { + if (buffer[i] == '\n') { + buffer[i] = '\0'; + return; + } + } +} + +bool check_no(char * no) { + student *l = first; + while (l != NULL) { + if (strlen(l->no) == strlen(no) && !strcmp(l->no, no)) { + return false; + } + l = l->next; + } + return true; +} diff --git a/shl02.c b/shl02.c new file mode 100644 index 0000000..b18f1cc --- /dev/null +++ b/shl02.c @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + +unsigned int counts = 0; + + + +/*结构体定义*/ +typedef struct student { + char no[21]; + char name[11]; + bool fame; + unsigned short int age; + char oth[151]; + struct student * next; +}student; + +struct student * first = NULL;//第一个 +struct student * loc = NULL;//当前(一般为NULL) + +bool del(char *);//删除单个 +bool edit(char*);//编辑单个 +student * search_by_no(char *);//按学号查找 +void search_by_name(char *);//按名字搜索 +bool check_password(void);//检查密码 +void run(void);//程序开始接入点 +bool load(void);//从文件加载数据 +void fgets_nn(char * , int, FILE *);//丢弃换行符的整行char*输入 +bool check_no(char *);//检查学号是否冲突 + +int main() { + run(); + system("pause"); + return 0; +} +bool del(char * no) { + + student *ed = search_by_no(no); + if (ed == NULL) + return false; + student temp; + if (ed == first) { + temp = *first; + free(first); + first = temp.next; + return true; + } + student *lu = first; + while (lu->next != NULL) { + if (strlen(lu->next->no) == strlen(no) && !strcmp(lu->next->no, no)) { + break; + } + lu = lu->next; + } + temp = *lu->next; + free(lu->next); + lu->next = temp.next; + counts--; + save(); + return true; +} + bool edit(char * no) { + student *l = search_by_no(no); + if (l == NULL) + return false; + + printf("请输入学生学号:\n"); + while (1) { + char no_temp[21]; + fgets_nn(no_temp, 21, stdin); + if (!strcmp(no_temp, "\0")) { + printf("学号不能为空:\n"); + continue; + } + if (!(strlen(l->no) == strlen(no_temp) && !strcmp(l->no, no_temp))) + if (!check_no(no_temp)) + printf("学号已存在!\n"); + else { + int i=0; + for ( i = 0; i < 21; i++) + l->no[i] = no_temp[i]; + break; + } + else + break; + } + printf("请输入学生姓名:\n"); + do { + fgets_nn(l->name, 11, stdin); + } while (!strcmp(l->name, "\0")); + while (1) { + printf("请输入学生性别(1.男 2.女):\n"); + char t = getch(); + if (t == '1') { + l->fame = true; + break; + } + else if (t == '2') { + l->fame = false; + break; + } + else { + printf("输入不正确\n"); + continue; + } + } + while (1) { + printf("请输入学生年龄:\n"); + if (scanf("%hd", &l->age) != 1) { + printf("输入不正确\n"); + while (getchar() != '\n')continue; + continue; + } + break; + } + printf("备注:\n"); + while (getchar() != '\n')continue; + fgets_nn(l->oth, 151, stdin); + + save(); + return true; +} +student * search_by_no(char * no) { + student *l = first; + while (l != NULL) { + if (strlen(l->no) == strlen(no) && !strcmp(l->no, no)) { + return l; + } + l = l->next; + } + return NULL; +} +void search_by_name(char * name) { + student *l = first; + unsigned int c = 0; + system("cls"); + while (l != NULL) { + if (strlen(l->name) == strlen(name) && !strcmp(l->name, name)) { + print_1(l); + printf("\n"); + c++; + } + l = l->next; + } + if (c == 0) { + printf("未检索到内容!\n"); + } + else { + printf("\n已检索到%d条名字为%s的学生\n\n", c, name); + } +} +void run(void) { + int n=3; + while (n--) { + if (check_password()) { + load(); + show_menu(); + } + else if(n!=0){ + printf("密码错误,你还有%d次机会!\n",n); + } + } + printf("身份验证失败,正在退出系统\n"); + exith(); +} +bool check_password(void) { + FILE * password; + password = fopen("password", "r"); + if (password == NULL) { + printf("第一次进入系统,请先设置密码(最长16位,最短6位):\n"); + password = fopen("password", "w"); + system("attrib password +s +h +r"); + char pw[17]; + char pw2[17]; + while (1) { + fgets_nn(pw, 17, stdin); + if (strlen(pw) < 6) { + system("cls"); + printf("密码至少设置6位:\n"); + continue; + } + system("cls"); + printf("请确认密码:\n"); + fgets_nn(pw2, 17, stdin); + if (strcmp(pw, pw2)||(strlen(pw)!=strlen(pw2))) { + system("cls"); + printf("密码确认错误,请重新设置密码:\n"); + continue; + } + break; + } + fprintf(password, "%s", pw); + fclose(password); + system("cls"); + return true; + } + char pw[17]; + fgets_nn(pw, 17, password); + if (feof(password)) { + fclose(password); + system("attrib password -s -h -r"); + remove("password"); + return check_password(); + } + printf("请输入密码:\n"); + char pwi[17]; + fgets_nn(pwi, 17, stdin); + system("cls"); + if (!strcmp(pw, pwi)) + return true; + else + return false; +} +bool load(void) { + FILE * data; + data = fopen("data", "r"); + if (data == NULL) + return false; + student temp; + while (1) { + fgets_nn(temp.no, 21, data); + if (feof(data)){ + break; + } + fgets_nn(temp.name, 11, data); + fscanf(data, "%hhu", &temp.fame); + fscanf(data, "%hd", &temp.age); + fgetc(data);//去除留下的\n + fgets_nn(temp.oth, 151, data); + creat(temp); + counts++; + } + fclose(data); + return true; +} +void fgets_nn(char *buffer, int max, FILE * file) { + fgets(buffer, max, file); + int i=0; + for (i = 0; i < max; i++) { + if (buffer[i] == '\n') { + buffer[i] = '\0'; + return; + } + } +} +bool check_no(char * no) { + student *l = first; + while (l != NULL) { + if (strlen(l->no) == strlen(no) && !strcmp(l->no, no)) { + return false; + } + l = l->next; + } + return true; +}