马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
首先是头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void input(struct Book *book); //录入书籍
void inquire(struct Book *book, int book_num); //查询书籍名称
void print(struct Book *book); //打印书籍
void menu(struct Book *book); //目录
struct Time
{
int year;
int month;
int day;
};
struct Book
{
char name[200];
struct Time time;
char chubanshe[200];
};
其次是代码部分
#include <test1.h>
void input(struct Book *book) //录入书籍
{
printf("请输入书名:");
scanf("%s",book->name);
printf("请输入年月日:");
scanf("%d%d%d",&(book->time.year),&(book->time.month),&(book->time.day));
printf("请输入出版社:");
scanf("%s",book->chubanshe);
printf("\n======书籍录入成功======\n\n");
}
void inquire(struct Book *book, int book_num) //查询书籍名称
{
char name[200];
int i,flag = 0;
printf("\n请输入书籍名称:\n\n");
scanf("%s",name);
for(i = 0;i < book_num; i++)
{
if(strcmp(book[i].name, name) == 0)
{
printf("这是第%d本书\n",i + 1);
print(&book[i]);
flag = 1;
break;
}
}
if(flag == 0)
{
printf("\n===请重新输入书籍名称===\n\n");
inquire(book,book_num);
}
}
void print(struct Book *book) //输出查询到的书籍
{
printf("\n======书籍查询成功======\n\n");
printf("书 名:%s\n",book -> name);
printf("日 期:%d-%d-%d\n", book -> time.year, book -> time.month, book -> time.day);
printf("出版社:%s\n\n",book -> chubanshe);
}
void menu(int *flag) //目录
{
int mode;
printf("请选择模式:\n");
printf("1.输入1录入书籍;\n");
printf("2.输入2查询书籍;\n");
printf("3.输入3删除书籍;\n"); //未做
printf("4.输入4退出系统;\n");
scanf("%d",flag);
}
int main(void)
{
struct Book *book = NULL;
int book_num = 0;
int flag;
while(1)
{
menu(&flag);
switch(flag)
{
case 1:
{
book_num++;
printf("这是第%d本书\n",book_num);
book = (struct Book *)realloc(book,(book_num + 1) * sizeof(struct Book *));
input(&book[book_num - 1]); break;
}
case 2: inquire(book,book_num); break;
// case 3: delet(book); break;
case 4: free(book);exit(1);
default:printf("\n请重新输入\n\n");break;
}
}
return 0;
}
|