题目
对大多数只学了一学期c语言的学生来说,这题目简直是魔鬼,哪怕学了一年这题都是魔鬼。
源码
https://gist.github.com/locbytes/fe05bb303e443f4feb19d624ebe35f4e
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define op_del 1
#define op_edit 2
#define op_search 3
#define op_ISBN 1
#define op_Name 2
#define vip 1
#define guest 2
struct Book{
char ISBN[14];
char Name[20];
double Price;
char Author[20];
char Publish[30];
};
struct Book book[100];
struct Bucket{
char ISBN[14];
int buyNum;
};
struct Bucket bucket[100];
void welcome();
void manageBook();
void addBook();
void delBook();
void editBook();
void searchBook();
void bookList();
void bookOperator(int cmd,int type);
void buyBook();
void vipLogin();
void guestLogin();
void buy(int type, char *uid);
void addBucket(int type);
void writeFile(int type);
void history();
int bookNum;
int bucketSize;
double price;
int main(){
bookNum=0;
welcome();
return 0;
}
void welcome(){
int welcome_code;
printf("欢迎使用图书销售系统\n");
printf("\n");
printf("1.图书基本信息管理\n");
printf("2.购书结算处理\n");
printf("3.销售历史记录\n");
printf("0.退出系统\n");
printf("\n");
printf("请输入您要进行的操作:");
scanf("%d",&welcome_code);
switch(welcome_code){
case 1:
manageBook();
break;
case 2:
buyBook();
break;
case 3:
history();
break;
case 0:
printf("感谢您使用本软件,再见!");
exit(0);
break;
}
}
void manageBook(){
printf("\n");
int manage_code;
printf("图书基本信息管理\n");
printf("\n");
printf("1.增加图书\n");
printf("2.删除图书\n");
printf("3.修改图书\n");
printf("4.查找图书\n");
printf("5.书库总表\n");
printf("0.返回首页\n");
printf("\n");
printf("请输入您要进行的操作:");
scanf("%d",&manage_code);
switch(manage_code){
case 1:
addBook();
break;
case 2:
delBook();
break;
case 3:
editBook();
break;
case 4:
searchBook();
break;
case 5:
bookList();
break;
case 0:
welcome();
break;
}
}
void addBook(){
printf("\n");
char ISBN[14];
char Name[20];
double Price;
char Author[20];
char Publish[30];
printf("增加图书\n");
printf("\n");
printf("请输入编号:\n");
scanf("%s",&book[bookNum].ISBN);
printf("请输入书名:\n");
scanf("%s",&book[bookNum].Name);
printf("请输入价格:\n");
scanf("%lf",&book[bookNum].Price);
printf("请输入作者:\n");
scanf("%s",&book[bookNum].Author);
printf("请输入出版时间:\n");
scanf("%s",&book[bookNum].Publish);
++bookNum;
getchar();
char ans;
printf("是否继续增加?Y/N\n");
scanf("%c", &ans);
if(ans=='N'||ans=='n'){
manageBook();
}else if(ans=='Y'||ans=='y'){
addBook();
}else{
return;
}
}
void delBook(){
printf("\n");
int del_code;
printf("删除图书\n");
printf("\n");
printf("1.按编号删除\n");
printf("2.按书名删除\n");
printf("0.取消操作\n");
printf("请输入您要进行的操作:");
scanf("%d",&del_code);
switch(del_code){
case 0:
manageBook();
break;
case 1:
bookOperator(op_del,op_ISBN);
break;
case 2:
bookOperator(op_del,op_Name);
break;
}
}
void editBook(){
printf("\n");
int edit_code;
printf("修改图书\n");
printf("\n");
printf("1.编号修改\n");
printf("2.书名修改\n");
printf("0.取消操作\n");
printf("请输入您要进行的操作:");
scanf("%d",&edit_code);
switch(edit_code){
case 0:
manageBook();
break;
case 1:
bookOperator(op_edit,op_ISBN);
break;
case 2:
bookOperator(op_edit,op_Name);
break;
}
}
void searchBook(){
printf("\n");
int search_code;
printf("查询图书\n");
printf("\n");
printf("1.编号查询\n");
printf("2.书名查询\n");
printf("0.取消操作\n");
printf("请输入您要进行的操作:");
scanf("%d",&search_code);
switch(search_code){
case 0:
manageBook();
break;
case 1:
bookOperator(op_search,op_ISBN);
break;
case 2:
bookOperator(op_search,op_Name);
break;
}
}
void bookList(){
printf("\n");
int i;
for(i=0;i<bookNum;++i){
if(strcmp(book[i].ISBN,"0")!=0){
printf("编号:%s\n",book[i].ISBN);
printf("书名:%s\n",book[i].Name);
printf("价格:%lf\n",book[i].Price);
printf("作者:%s\n",book[i].Author);
printf("出版时间:%s\n",book[i].Publish);
printf("--------------------------------\n");
}
}
manageBook();
}
void bookOperator(int cmd, int type){
printf("\n");
char ISBN[14];
char Name[20];
int i;
switch(cmd){
case op_del:
switch(type){
case op_ISBN:
printf("请输入编号:\n");
scanf("%s",&ISBN);
for(i=0;i<bookNum;++i){
if(strcmp(book[i].ISBN,ISBN)==0){
strcpy(book[i].ISBN, "0");
strcpy(book[i].Name, "");
book[i].Price=0;
strcpy(book[i].Author, "");
strcpy(book[i].Publish, "");
}
}
break;
case op_Name:
printf("请输入书名:\n");
scanf("%s",&Name);
for(i=0;i<bookNum;++i){
if(strcmp(book[i].Name,Name)==0){
strcpy(book[i].ISBN, "0");
strcpy(book[i].Name, "");
book[i].Price=0;
strcpy(book[i].Author, "");
strcpy(book[i].Publish, "");
}
}
break;
}
break;
case op_edit:
switch(type){
case op_ISBN:
printf("请输入编号:\n");
scanf("%s",&ISBN);
for(i=0;i<bookNum;++i){
if(strcmp(book[i].ISBN,ISBN)==0){
printf("请输入编号:\n");
scanf("%s",&book[i].ISBN);
printf("请输入书名:\n");
scanf("%s",&book[i].Name);
printf("请输入价格:\n");
scanf("%lf",&book[i].Price);
printf("请输入作者:\n");
scanf("%s",&book[i].Author);
printf("请输入出版时间:\n");
scanf("%s",&book[i].Publish);
}
}
break;
case op_Name:
printf("请输入书名:\n");
scanf("%s",&Name);
for(i=0;i<bookNum;++i){
if(strcmp(book[i].Name,Name)==0){
printf("请输入编号:\n");
scanf("%s",&book[i].ISBN);
printf("请输入书名:\n");
scanf("%s",&book[i].Name);
printf("请输入价格:\n");
scanf("%lf",&book[i].Price);
printf("请输入作者:\n");
scanf("%s",&book[i].Author);
printf("请输入出版时间:\n");
scanf("%s",&book[i].Publish);
}
}
break;
}
break;
case op_search:
switch(type){
case op_ISBN:
printf("请输入编号:\n");
scanf("%s",&ISBN);
for(i=0;i<bookNum;++i){
if(strcmp(book[i].ISBN,ISBN)==0){
printf("编号:%s\n",book[i].ISBN);
printf("书名:%s\n",book[i].Name);
printf("价格:%lf\n",book[i].Price);
printf("作者:%s\n",book[i].Author);
printf("出版时间:%s\n",book[i].Publish);
printf("--------------------------------\n");
}
}
break;
case op_Name:
printf("请输入书名:\n");
scanf("%s",&Name);
for(i=0;i<bookNum;++i){
if(strcmp(book[i].Name,Name)==0){
printf("编号:%s\n",book[i].ISBN);
printf("书名:%s\n",book[i].Name);
printf("价格:%lf\n",book[i].Price);
printf("作者:%s\n",book[i].Author);
printf("出版时间:%s\n",book[i].Publish);
printf("--------------------------------\n");
}
}
break;
}
break;
}
manageBook();
}
void buyBook(){
printf("\n");
int buy_code;
printf("购书结算处理\n");
printf("\n");
printf("1.会员登录\n");
printf("2.非会员进入\n");
printf("0.返回首页\n");
printf("\n");
printf("请输入您要进行的操作:");
scanf("%d",&buy_code);
switch(buy_code){
case 1:
vipLogin();
break;
case 2:
guestLogin();
break;
case 0:
welcome();
break;
}
}
void vipLogin(){
printf("\n");
char uid[20];
printf("会员登录\n");
printf("\n");
printf("请输入您的会员号:\n");
scanf("%s",&uid);
buy(vip, uid);
}
void guestLogin(){
buy(guest, "非会员");
}
void buy(int type, char *uid){
printf("\n");
bucketSize=0;
switch(type){
case vip:
printf("会员结账\n");
break;
case guest:
printf("非会员结账\n");
break;
}
addBucket(type);
getchar();
printf("确认结账?Y/N\n");
char ans;
scanf("%c", &ans);
if(ans=='Y'||ans=='y'){
printf("购买成功\n");
writeFile(type);
welcome();
}else{
welcome();
}
}
void addBucket(int type){
printf("\n");
char ans;
int i,j;
price=0;
printf("请输入你要购买的图书编号:");
scanf("%s",&bucket[bucketSize].ISBN);
printf("请输入购买数量:");
scanf("%d",&bucket[bucketSize].buyNum);
++bucketSize;
printf("编号 书名 价格 作者 出版时间 数量\n");
for(i=0;i<bucketSize;++i){
for(j=0;j<bookNum;++j){
if(strcmp(bucket[i].ISBN,book[j].ISBN)==0){
printf("%s ",book[j].ISBN);
printf("%s ",book[j].Name);
printf("%lf ",book[j].Price);
printf("%s ",book[j].Author);
printf("%s ",book[j].Publish);
printf("%d ",bucket[i].buyNum);
double temp = book[j].Price*bucket[i].buyNum;
price+=temp;
printf("\n");
break;
}
}
}
printf("\n---------------------------------------------\n");
printf("结账信息 总价格:%.2lf",price);
switch(type){
case vip:
printf(" 会员价:%.2lf\n", price*0.5);
break;
case guest:
printf("\n");
break;
}
getchar();
printf("是否继续选择图书?Y/N\n");
scanf("%c", &ans);
if(ans=='Y'||ans=='y'){
addBucket(type);
}else{
return;
}
}
void writeFile(int type){
int i,j;
price=0;
time_t now;
struct tm *tm_now;
time(&now);
tm_now = localtime(&now);
FILE *fp = NULL;
fp = fopen("log.txt", "a");
fprintf(fp, "购买时间:%d/%d/%d\n", tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday);
fprintf(fp, "---------------------------------------------\n");
fprintf(fp, "编号 书名 价格 作者 出版时间 数量\n");
for(i=0;i<bucketSize;++i){
for(j=0;j<bookNum;++j){
if(strcmp(bucket[i].ISBN,book[j].ISBN)==0){
fprintf(fp, "%s ",book[j].ISBN);
fprintf(fp, "%s ",book[j].Name);
fprintf(fp, "%lf ",book[j].Price);
fprintf(fp, "%s ",book[j].Author);
fprintf(fp, "%s ",book[j].Publish);
fprintf(fp, "%d ",bucket[i].buyNum);
double temp = book[j].Price*bucket[i].buyNum;
price+=temp;
fprintf(fp, "\n");
}
}
}
fprintf(fp, "\n---------------------------------------------\n");
fprintf(fp, "结账信息 总价格:%.2lf",price);
switch(type){
case vip:
fprintf(fp, " 会员价:%.2lf\n", price*0.5);
break;
case guest:
fprintf(fp, "\n");
break;
}
fprintf(fp, "\n");
fclose(fp);
}
void history(){
printf("\n");
printf("售书历史记录\n");
printf("\n");
FILE *fp = NULL;
char buff[255];
fp = fopen("log.txt", "r");
while(fgets(buff, 255, fp)!=NULL){
printf("%s", buff);
}
fclose(fp);
welcome();
}