鱼C论坛

 找回密码
 立即注册
查看: 2109|回复: 0

[技术交流] 一个简单的图书管理

[复制链接]
发表于 2021-3-4 19:22:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 11:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表