diff --git a/2209040024/24 point game design b/2209040024/24 point game design new file mode 100644 index 0000000..7570acc --- /dev/null +++ b/2209040024/24 point game design @@ -0,0 +1,127 @@ +#include +#include + +#define SIZE 4 // 定义参与游戏的数字数量为4 + +/* 表达式结构体 + * value: 表达式的值 + * expression: 表达式字符串 + */ +typedef struct { + double value; + char expression[5 * (SIZE - 1) + SIZE * 2 + 1]; // 表达式字符串的长度为 5 * (SIZE - 1) + SIZE * 2 + 1 +} Exp; + +/* 处理24点游戏的函数 + * 如果能得到结果为24的表达式,返回1,否则返回0 + */ +int process24(Exp* exps, int size) { + Exp* stack = (Exp*) calloc(size - 1, sizeof(Exp)); + + /* + * 当只有一个数字时,判断是否等于24 + */ + if(size == 1) { + if(exps[0].value == 24) { + printf( "24 = %s\n", exps[0].expression ); + return 1; + } else { + return 0; + } + } + + /* + * 循环遍历所有可能的数字组合 + */ + int m, n, i, t; + for (m = 0; m < size - 1; m++) { + for (n = m + 1; n < size; n++) { + + /* + * 构建一个新的数字数组,排除当前两个数字 + */ + t = 0; + for (i = 0; i < size; i++) { + if (i != m && i != n) { + stack[t].value = exps[i].value; + sprintf(stack[t].expression, "%s", exps[i].expression); + t++; + } + } + + /* + * 尝试加法运算 + */ + stack[size - 2].value = exps[m].value + exps[n].value; + sprintf(stack[size - 2].expression, "(%s + %s)", exps[m].expression, exps[n].expression); + if (process24(stack, size - 1)) { + return 1; + } + + /* + * 尝试减法运算 + */ + if (exps[m].value > exps[n].value) { + stack[size - 2].value = exps[m].value - exps[n].value; + sprintf(stack[size - 2].expression, "(%s - %s)", exps[m].expression, exps[n].expression); + } + else { + stack[size - 2].value = exps[n].value - exps[m].value; + sprintf(stack[size - 2].expression, "(%s - %s)", exps[n].expression, exps[m].expression); + } + if (process24(stack, size - 1)) { + return 1; + } + + /* + * 尝试乘法运算 + */ + stack[size - 2].value = exps[m].value * exps[n].value; + sprintf(stack[size - 2].expression, "%s * %s", exps[m].expression, exps[n].expression); + if (process24(stack, size - 1)) { + return 1; + } + + /* + * 尝试除法运算 + */ + if (exps[m].value != 0) { + stack[size - 2].value = exps[n].value / exps[m].value; + sprintf(stack[size - 2].expression, "%s / %s", exps[n].expression, exps[m].expression); + if (process24(stack, size - 1)) { + return 1; + } + } + if (exps[n].value != 0) { + stack[size - 2].value = exps[m].value / exps[n].value; + sprintf(stack[size - 2].expression, "%s / %s", exps[m].expression, exps[n].expression); + if (process24(stack, size - 1)) { + return 1; + } + } + } + } + + return 0; // 无法找到24的组合,返回0 +} + +int main(void) { + int i, number; + Exp exps[SIZE]; + + // 输入4个数字并保存在exps数组中 + for (i = 0; i < SIZE; i++) { + scanf("%d", &number); + exps[i].value = (double) number; + sprintf(exps[i].expression, "%d", number); + } + + // 调用处理24点游戏的函数 + if (process24(exps, SIZE)) { + printf("Done\n"); + } else { + printf("No answer, try another 4 numbers.\n"); + } + + return 0; +} diff --git a/2209040024/Design of Book Information Management System b/2209040024/Design of Book Information Management System new file mode 100644 index 0000000..df9f75f --- /dev/null +++ b/2209040024/Design of Book Information Management System @@ -0,0 +1,1555 @@ + +/*头文件区*/ +#include +#include +#include +#include +#include + +/*宏定义区*/ +#define ADMIN_password "123456" /*管理员登录密码*/ + +/*结构体声明区*/ +typedef struct bookinfo/*系统图书信息 */ +{ + char number[15];/*图书编号*/ + char name[30];/*图书名称*/ + char author[20];/*作者*/ + char press[30];/*出版社*/ + char category[20]; /*类别*/ + float price;/*单价*/ + int quantity;/*馆藏量*/ + int time;/*借书次数*/ +}BOOKINFO; + +typedef struct bookgrasp/*会员借出的书籍信息*/ +{ + char number[15];/*图书编号*/ + char name[30];/*图书名称*/ + char author[20];/*作者*/ + char press[30];/*出版社*/ + char category[20]; /*类别*/ + float price;/*单价*/ + char account[30];/*借书者ID*/ +}BOOKGRASP; + +typedef struct member/*会员个人信息 */ +{ + char name[30]; + char ID[20]; + char password[30]; + char sex[15]; + char password_key[30]; +}MEMBER; + +/*菜单函数区*/ +void menu1(); +void menu2(); +void menu3(); +void AboutMe(); + +/*工具函数区*/ +int compare_password(char password[20]);/*密码比对函数 */ +void clock_delay(); /*时间延时函数*/ +void password_circle();/*密码框装饰函数*/ +void find_account_circle();/*找回帐户弹出框函数*/ + +/*会员信息系统层级函数区*/ +int user_account(char account[20]);/*个人账户信息*/ +void member_log_up();/*会员注册函数*/ +int member_log_in(char account[20]);/*会员登录函数*/ +void ADMIN_watch_member();/*查看会员信息函数*/ +void display_memberbook();/*查看所有图书借阅信息函数*/ +int check_repeat_ID(char id_account[20]);/*检查账户重复性函数*/ +void find_account();/*账户找回函数*/ + +/*图书信息系统层级函数区*/ +void add_book();/*输入新进的图书信息*/ +void delete_book();/*根据图书名称对图书信息进行删除*/ +void modify_book();/*修改图书信息*/ +void display_book();/*显示全部图书信息*/ +void search_book();/*根据图书名称显示图书的信息*/ +void borrow_book(char account[20]);/*借书*/ +void return_book(char account[20]);/*还书*/ +void display_price_lower();/*根据价格列出图书信息*/ +void dispaly_popular();/*图书受欢迎度排行榜*/ +int check_repeat_book(char number[30]);/*查重书籍编号*/ + +int main() +{ + int flag1=1,flag2=1,flag3=1;/* flag是判断条件,flag为 1 时为真,为 0 时为假 */ + char choice1,choice2,choice3; + FILE *p1,*p2,*p3; + + system("图书管理系统"); + + if((p1=fopen("library.txt","r"))==NULL)/*p1是图书管理系统的书籍信息文件指针*/ + { + p1=fopen("library.txt","w"); + fclose(p1); + } + if((p2=fopen("infomember.txt","r"))==NULL)/*p2是图书管理系统的会员个人信息文件指针*/ + { + p2=fopen("infomember.txt","w"); + fclose(p2); + } + if( (p3=fopen("memberbook.txt","r"))==NULL)/*p3是图书管理系统的会员书籍信息文件指针*/ + { + p3=fopen("memberbook.txt","w"); + fclose(p3); + } + + while(flag1) + { + system("cls"); + menu1();/*调出主菜单 */ + + printf("\n"); + printf("请您选择(1-4):"); + choice1=getch(); + while(choice1!='1'&&choice1!='2'&&choice1!='3'&&choice1!='4') + choice1=getch(); + printf("%c",choice1); + fflush(stdin); + + if(choice1=='1')/*会员登录*/ + { + int flag=0; + char account1[20],account_choice; + system("cls"); + password_circle(); + fflush(stdin); + gets(account1); + flag=member_log_in(account1);/*账户登录函数*/ + if(flag==2) + { + system("cls"); + password_circle(); + fflush(stdin); + gets(account1); + flag=member_log_in(account1);/*账户登录函数*/ + } + if(flag==1) + { + while(flag2&&flag3){ + system("cls"); + menu2(); + printf("\n"); + printf("请您选择(1-8):"); + choice2=getch(); + while(choice2!='1'&&choice2!='2'&&choice2!='3'&&choice2!='4'&&choice2!='5'&&choice2!='6'&&choice2!='7'&&choice2!='8') + choice2=getch(); + printf("%c",choice2); + + switch(choice2) + { + case '1':search_book(); break; + case '2':borrow_book(account1); break; + case '3':return_book(account1); break; + case '4':display_book(); break; + case '5':display_price_lower();break; + case '6':dispaly_popular();break; + case '7':if(user_account(account1)) + { + printf("\n\n\t\t\t登录信息改变,请重新登录..."); + clock_delay(); + flag3=0; + } + break; + case '8':flag2=0; + printf("\n\n\t\t\t正在退出用户界面..."); + clock_delay(); + break; + default:break;/*虽然压根不会读到default情况,但是怕后来开发者漏掉新加的case的while处理,所以加上*/ + } + } + }else{ + system("cls"); + find_account_circle();/*装饰弹出框函数*/ + fflush(stdin); + account_choice=getch(); + while(account_choice!='Y'&&account_choice!='y'&&account_choice!='N'&&account_choice!='n') + account_choice=getch(); + printf("%c",account_choice); + printf("\n"); + if(account_choice=='Y'||account_choice=='y') + find_account();/*账户找回函数*/ + } + flag2=1,flag3=1; + } + + else if(choice1=='2')/*会员注册*/ + { + system("cls"); + printf("\n\n\n\t\t\t正在进入用户注册界面..."); + clock_delay(); + member_log_up(); + } + + else if(choice1=='3')/*管理员登录*/ + { + if(compare_password(ADMIN_password)) + { + printf("\n\n\t\t\t\t --输入密码正确!--\n\n\t\t\t\t==正在进入管理员界面==\n"); + clock_delay(); + while(flag2){ + system("cls"); + menu3(); + printf("\n"); + printf("请您选择(1-8):"); + choice3=getch(); + while(choice3!='1'&&choice3!='2'&&choice3!='3'&&choice3!='4'&&choice3!='5'&&choice3!='6'&&choice3!='7'&&choice3!='8') + choice3=getch(); + printf("%c",choice3); + + switch(choice3) + { + case '1':add_book(); break; + case '2':delete_book(); break; + case '3':modify_book(); break; + case '4':search_book(); break; + case '5':display_book(); break; + case '6':ADMIN_watch_member();break; + case '7':display_memberbook();break; + case '8':flag2=0; + printf("\n\n\t\t\t正在退出管理员界面..."); + clock_delay(); + break; + default:break;/*虽然压根不会读到default情况,但是怕后来开发者漏掉新加的case的while处理,所以加上*/ + } + } + } + else{ + printf("\n\n\t\t\t\t --输入密码错误!--\n"); + clock_delay(); + } + flag2=1; + } + + else if(choice1=='4')/*退出系统*/ + { + flag1=0; + } + } + fflush(stdin); + system("cls"); + printf("你已经安全退出系统!(按任意键关闭界面)\n\n\t欢迎您的再次使用!\n\n"); + getch(); + return 0; +} + +void menu1() +{ + system("color E9"); + printf("\n\n"); + printf("\t\t\t *=======================================*\n"); + printf("\t\t\t| * - * - * 图*书*管*理*系*统 * - * - * |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| | [1] 会员登录 | |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| | [2] 会员注册 | |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| | [3] 管理员登录 | |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| | [4] 退出系统 | |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| * - * - * - * - * - * - * - * - * - * |\n"); + printf("\t\t\t *=======================================*\n"); +} +void menu2() +{ + system("color BC"); + printf("\n\n"); + printf("\t\t\t *=====================================*\n"); + printf("\t\t\t| | * - * - * -会-员-界-面- * - * - * | |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| | [1] 查找图书信息 | |\n"); + printf("\t\t\t| * [2] 借阅图书 * |\n"); + printf("\t\t\t| | [3] 归还图书 | |\n"); + printf("\t\t\t| * [4] 书库所有图书信息 * |\n"); + printf("\t\t\t| | [5] 价位书籍推荐 | |\n"); + printf("\t\t\t| * [6] 图书受欢迎度排行榜 * |\n"); + printf("\t\t\t| | [7] 您的账户信息 | |\n"); + printf("\t\t\t| * [8] 退出用户界面 * |\n"); + printf("\t\t\t| | | |\n"); + printf("\t\t\t| * - * - * - * - * - * - * - * - * - * |\n"); + printf("\t\t\t *=====================================*\n"); +} +void menu3() +{ + system("color 1D"); + printf("\n\n"); + printf("\t\t\t *======================================*\n"); + printf("\t\t\t| | * - * - * 管-理-员-界-面 * - * - * | |\n"); + printf("\t\t\t| * * |\n"); + printf("\t\t\t| | [1] 添加图书 | |\n"); + printf("\t\t\t| * [2] 删减图书 * |\n"); + printf("\t\t\t| | [3] 修改图书信息 | |\n"); + printf("\t\t\t| * [4] 查找图书信息 * |\n"); + printf("\t\t\t| | [5] 显示全部图书信息 | |\n"); + printf("\t\t\t| * [6] 显示所有会员信息 * |\n"); + printf("\t\t\t| | [7] 显示所有借阅信息 | |\n"); + printf("\t\t\t| * [8] 退出管理员界面 * |\n"); + printf("\t\t\t| | | |\n"); + printf("\t\t\t| * - * - * - * - * -- * - * - * - * - * |\n"); + printf("\t\t\t *======================================*\n"); +} + + +void add_book() /*输入新进的图书信息*/ +{ + char choice; + FILE *p1; + BOOKINFO newbook; + int flag; + system("cls"); + while(1) + { + flag=0; + fflush(stdin); + printf("请输入图书编号:"); + gets(newbook.number); + + if(check_repeat_book(newbook.number)) + { + printf("该图书编号已被使用!"); + printf("\n按任意键重新注册\n\n"); + getch(); + flag=1; + } + else{ + fflush(stdin); + printf("请输入书名:"); + gets(newbook.name); + + printf("请输入作者:"); + gets(newbook.author); + + printf("请输入出版社:"); + gets(newbook.press); + + printf("请输入类别(文学/科学/理学/工学/等):"); + gets(newbook.category); + + printf("请输入价格:"); + scanf("%f",&newbook.price); + + printf("请输入馆藏量:"); + scanf("%d",&newbook.quantity); + + printf("请输入借阅次数(新书输入0):"); + scanf("%d",&newbook.time); + fflush(stdin); + printf("是否保存该条书目(y/n):"); + choice=getch(); + while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n') + choice=getch(); + printf("%c",choice); + if(choice=='Y'||choice=='y') + { + p1=fopen("library.txt","a"); + fprintf(p1,"%s %s %s %s %s %f %d %d\n", + newbook.number,newbook.name,newbook.author,newbook.press, + newbook.category,newbook.price,newbook.quantity,newbook.time); + fclose(p1); + printf("\n该条书目已添加到library.txt文件中!\n"); + } + else + { + printf("\n本条书目未保存!\n"); + } + printf("\n\n是否继续添加书目(y/n):"); + choice=getch(); + while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n') + choice=getch(); + printf("%c",choice); + printf("\n"); + } + if(choice=='Y'||choice=='y'||flag==1) + continue; + else + break; + } +} + +void display_book()/*显示全部图书信息*/ +{ + FILE *p1; + int n; + BOOKINFO bookinfo[100]; + int booknumber=0; + system("cls"); + fflush(stdin); + p1=fopen("library.txt","r"); + while(!feof(p1)) + { + fscanf(p1,"%s %s %s %s %s %f %d %d\n", + bookinfo[booknumber].number,bookinfo[booknumber].name,bookinfo[booknumber].author, + bookinfo[booknumber].press,bookinfo[booknumber].category, + &bookinfo[booknumber].price,&bookinfo[booknumber].quantity,&bookinfo[booknumber].time); + booknumber++; + } + fclose(p1); + printf("当前图书馆内有%d目书籍\n\n",booknumber); + if(bookinfo[0].quantity==0) + { + printf("\n没有任何图书信息!\n\n"); + } + else + { + n=0; + printf("|---------------------------------图书信息---------------------------------|\n"); + printf("|编号 书名 作者 出版社 类别 单价 馆藏量 借阅数|\n"); + printf("| |\n"); + while(n\n\n2.<-书名->\n\n3.<-作者名->\n\n哪种方式查询图书:"); + select=getch(); + printf("%c",select); + printf("\n\n请您输入查找信息:"); + switch(select) + { + case '1':gets(number_compare);strcpy(search,number_compare);flag1=0;break; + case '2':gets(name_compare);strcpy(search,name_compare);flag1=0;break; + case '3':gets(author_compare);strcpy(search,author_compare);flag1=0;break; + default : + printf("\n不要输入菜单之外的数字~按任意键继续!"); + fflush(stdin); + getch(); + } + } + + flag1=1; + if(bookinfo[0].quantity==0) + { + printf("书库中没有任何信息!\n\n"); + printf("按任意键回到主菜单!\n\n"); + getch(); + break; + } + else + { + for(n=0;n=booknumber) + printf("\n没有查找符合您要求的书籍!\n"); + + printf("\n\n是否继续查询(y/n):"); + choice=getch(); + while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n') + choice=getch(); + printf("%c",choice); + printf("\n"); + if(choice=='Y'||choice=='y') + flag=1; + else + flag=0; + + } + } +} + +void delete_book()/*根据图书名称对图书信息进行删除*/ +{ + char search[20]="";/*search用来存放要删除的书名*/ + int n,i; + FILE *p1; + char choice; + BOOKINFO bookinfo[100]; + int booknumber; + + system("cls"); + while(1) + { + fflush(stdin); + printf("输入要删除的书本名称:"); + gets(search); + p1=fopen("library.txt","r"); + booknumber=0; + while(!feof(p1)) + { + fscanf(p1,"%s %s %s %s %s %f %d %d\n", + bookinfo[booknumber].number,bookinfo[booknumber].name,bookinfo[booknumber].author, + bookinfo[booknumber].press,bookinfo[booknumber].category, + &bookinfo[booknumber].price,&bookinfo[booknumber].quantity,&bookinfo[booknumber].time); + booknumber++; + } + fclose(p1); + if(bookinfo[0].quantity==0) + { + printf("书库中没有任何信息!\n\n"); + printf("按任意键回到主菜单!\n\n"); + getch(); + break; + }/*if结束*/ + else + { + for(n=0;n=booknumber) + printf("\n没有查找该书的任何信息!\n"); + else + { + printf("\n是否确认需要删除该条书目(Y/N):"); + choice=getch(); + while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n') + choice=getch(); + printf("%c",choice); + if(choice=='Y'||choice=='y') + { + for(i=n;i=booknumber) + printf("\n没有查找该书的任何信息!"); + else if(!flag2) + { + p1=fopen("library.txt","w"); + for(n=0;n=booknumber1) + printf("\n没有查找该书的任何信息!\n"); + else + { + p1=fopen("library.txt","w"); + for(n=0;nbookinfo[k].time) + k=j; + if(k!=i) + { + bookinfo[100]=bookinfo[i]; + bookinfo[i]=bookinfo[k]; + bookinfo[k]=bookinfo[100]; + } + } + + printf("|---------------------------图书受欢迎度排行榜-----------------------------|\n"); + printf("|编号 书名 作者 出版社 类别 单价 馆藏量 借阅数|\n"); + printf("| |\n"); + for(n=0;n0) + { + printf("\b \b");//清除*号和错符 + i--; + continue; + } + if(c!=8) + { + compare[i++]=c; + putchar('*'); + } + } + compare[i]='\0'; + if(strcmp(password,compare)==0) + { + printf("\a"); //响铃 + return 1; + } + else + { + printf("\a"); //响铃 + return 0; + } +} + +int check_repeat_ID(char *id_account)/*查重函数*/ +{ + FILE *p2; + MEMBER member[100]; + int flag1=0,flag2=1,n,number; + + p2=fopen("infomember.txt","r"); + number=0; + while(!feof(p2)) + { + fscanf(p2,"%s %s %s %s %s\n",member[number].ID,member[number].password, + member[number].password_key,member[number].name,member[number].sex); + number++; + } + fclose(p2); + for(n=0;n