#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int booknum=0;
struct TIME{
int year;
int month;
int day;
};
struct BOOK{
char name[250];
struct TIME time;
char cbs[250];
}*book=NULL;//定义book指针并赋值为空
void inquire(struct BOOK *book,int booknum);//查询书籍
void add(struct BOOK *book,int booknum);//添加书籍
void delet(struct BOOK *book);//删除书籍
void print(struct BOOK *book,int i);//打印书籍
void select();//菜单选项
void printall(struct BOOK *book); //打印所有书籍信息
void delet(struct BOOK *book)
{
char ch[25];
printf("请输入书名:\n");
scanf("%s",ch);
for(int i=0;i<booknum;i++)
{
if(strcmp(book[i].name,ch)==0)
{
strcpy(book[i].name,book[i+1].name);
book->time.year=0; book->time.month=0; book->time.day=0;
strcpy(book[i].cbs,book[i+1].cbs);
booknum--;
}
}
}
void inquire(struct BOOK *book,int booknum)//查询书籍
{
char ch;
while((ch=getchar())!='p')
{
char ch[25];
printf("请输入查询的书名:\n");
scanf("%s",ch);
for(int i=0;i<booknum;i++)
{
if(strcmp(book[i].name,ch)==0)
{
printf("\t此书编号\t\t书名\t\t出版日期\t\t出版社\t\n\n");
print(&book[i],i);//传参到输出函数print
//break;
}
else printf("查无此书!\n");break;
}
printf("输入p停止查询,否则继续查询...\n");
}
select();
}
void add(struct BOOK *book,int booknum)//添加书籍
{
char ch;
while((ch=getchar())!='p')
{
booknum++;
printf("这是第%d本书\n",booknum);
printf("请输入书名:\n");
scanf("%s",book->name);
printf("请输入出版日期(年月日):\n");
scanf("%d%d%d",&(book->time.year),&(book->time.month),&(book->time.day));
printf("请输入出版社:\n");
scanf("%s",book->cbs);
printf("====添加完成!====\n");
printf("输入p停止添加\n");
}
select();
}
void print(struct BOOK *book,int i)
{
printf("\t%d\t\t%s\t\t",i,book->name);
printf("%d-%d-%d",book->time.year,book->time.month,book->time.day);
printf("\t\t%s\t\n",book->cbs);
}
void select()//选择操作
{
int code;
printf("-----------------------------------------------------------------------------------------------------------------------\n\n");
printf("\t\t\t1.录入书籍\t\t2.查询书籍\n\n\t\t\t3.删除书籍\t\t4.结束程序\t\t5.打印所有书籍信息\n\n");
printf("-----------------------------------------------------------------------------------------------------------------------\n");
printf("\t 请选择模式:");
scanf("%d",&code);//输入非数字将陷入死循环
printf("\n\n");
switch(code)
{
case 1:add(book,booknum); break;
case 2:inquire(book,booknum);break;
case 3:delet(book); break;
case 4:exit(0); break;
case 5:printall(book); break;
default:printf("请重新输入选项!!!\n");break;
}
select();
}
void printall(struct BOOK *book)
{
printf("\t此书编号\t\t书名\t\t出版日期\t\t出版社\t\n\n");
for(int j=0;j<booknum;j++)
{
print(&book[j],j);
}
printf("====所有书籍信息打印完毕!====\n");
select();
}
int main()
{
printf("请选择操作:\n");
select();
return 0;
}
c语言写了个图书管理代码,选项为1.2.3的时候运行bug,核对好多遍了,哪里出错了?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- qzjhjxj 2023-01-18 11:36关注
修改完善如下,改动处见注释,供参考:
#include<stdio.h> #include<stdlib.h> #include<string.h> int booknum = 0; struct TIME { int year; int month; int day; }; struct BOOK { char name[50]; struct TIME time; char cbs[250]; }book[100]; //*book = NULL;//定义book指针并赋值为空 修改 //void inquire(struct BOOK* book, int booknum);//查询书籍 //修改 //void add(struct BOOK* book, int booknum);//添加书籍 //修改 //void delet(struct BOOK* book);//删除书籍 //void print(struct BOOK* book, int i);//打印书籍 //void select();//菜单选项 //void printall(struct BOOK* book); //打印所有书籍信息 void print(struct BOOK* book, int i) { printf("\t%d\t%s\t", i, book->name); printf("%d-%d-%d", book->time.year, book->time.month, book->time.day); printf("\t%s\n", book->cbs); } void delet(struct BOOK* book) { int flg = 1; //修改 char ch[50]; printf("请输入书名:\n"); scanf("%s", ch); for (int i = 0; i < booknum; i++) { if (strcmp(book[i].name, ch) == 0) { for (int j = i; j < booknum - 1; j++) //修改 book[j] = book[j + 1]; //修改 //strcpy(book[i].name, book[i + 1].name); //修改 //book->time.year = 0; book->time.month = 0; book->time.day = 0;//修改 //strcpy(book[i].cbs, book[i + 1].cbs); //修改 i--; //修改 booknum--; //修改 flg = 0; //修改 printf("删除成功!\n"); //修改 } } if (flg) //修改 printf("未找到相关记录!\n"); } void inquire(struct BOOK* book)//查询书籍 void inquire(struct BOOK* book, int booknum) 修改 { char ch; do{ //修改 char name[50]; //char ch[25]; 修改 printf("请输入查询的书名:\n"); scanf("%s", name); // scanf("%s", ch); 修改 for (int i = 0; i < booknum; i++) { if (strcmp(book[i].name, name) == 0) //strcmp(book[i].name, ch { printf("\t此书编号\t书名\t出版日期\t出版社\n\n"); print(&book[i], i);//传参到输出函数print break; } //else printf("查无此书!\n"); break; if (i == booknum - 1) //修改 printf("查无此书!\n"); } printf("输入p停止查询,否则继续查询...\n"); getchar(); //修改 }while ((ch = getchar()) != 'p'); //修改 //select(); 修改 } void add(struct BOOK* book)//添加书籍 修改 void add(struct BOOK* book, int booknum) { int i = booknum; //修改 char ch; do{ //修改 printf("这是第%d本书\n", i + 1); printf("请输入书名:\n"); scanf("%s", book[i].name); printf("请输入出版日期(年月日):\n"); scanf("%d%d%d", &book[i].time.year, &book[i].time.month, &book[i].time.day); printf("请输入出版社:\n"); scanf("%s", book[i].cbs); i++; printf("====添加完成!====\n"); printf("输入p停止添加\n"); getchar(); //修改 } while ((ch = getchar()) != 'p');//修改 booknum = i; //修改 //select(); //修改 } void printall(struct BOOK* book) { printf("\t此书编号\t书名\t出版日期\t出版社\n\n"); for (int j = 0; j < booknum; j++) { print(&book[j], j); } printf("====所有书籍信息打印完毕!====\n"); //select(); //修改 } void select()//选择操作 { int code; printf("-------------------------------------------------------------------------------------------\n\n"); //修改 printf("\t\t\t1.录入书籍\t2.查询书籍\n\n\t\t\t3.删除书籍\t4.结束程序\n\n\t\t\t5.打印所有书籍信息\n\n"); //修改 printf("-------------------------------------------------------------------------------------------\n"); //修改 printf("\t 请选择模式:"); scanf("%d", &code);//输入非数字将陷入死循环 printf("\n\n"); switch (code) { case 1:add(book); break; //add(book, booknum); //修改 case 2:inquire(book); break; //inquire(book, booknum);//修改 case 3:delet(book); break; case 4:exit(0); break; case 5:printall(book); break; default:printf("请重新输入选项!!!\n"); break; } select(); } int main() { printf("请选择操作:\n"); select(); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 STM32 INMP441无法读取数据
- ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
- ¥15 用visualstudio2022创建vue项目后无法启动
- ¥15 x趋于0时tanx-sinx极限可以拆开算吗
- ¥500 把面具戴到人脸上,请大家贡献智慧
- ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
- ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
- ¥30 c#打开word开启修订并实时显示批注
- ¥15 如何解决ldsc的这条报错/index error
- ¥15 VS2022+WDK驱动开发环境