图书管理(不含数据库,纯粹练习)

本文介绍了一个简单的图书管理系统实现,包括创建、添加、删除、搜索图书及显示所有图书信息等功能。该系统使用C语言编写,通过链表结构来存储图书数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#define NULL 0



typedef struct book
{
    char name[20];
    int price;
    int sum;
    struct book *next;
}node,*list;

node* creatlist(list l)
{
    node* p = NULL;
    node* q;
    int n,i;
    l = (list)malloc(sizeof(node));
    l->next = NULL;
    p = l;
    printf("how many book do we have:  \n");
    scanf("%d",&n);
    for(i = 0;i < n; i++)
    {

        q = (node*)malloc(sizeof(node));
        printf("input the name \n");
        scanf("%s",q->name);
        printf("\n");
        printf("input the price \n");
        scanf("%d",&q->price);
        while(p->next != NULL)
        {
            p = p->next;
        }
        q->next = p->next;
        p->next = q;
        p->sum++;
    }
    return l;
}


int search(list l,char *p)
{
    node* q;
    q = l->next;

    while(q != NULL)
    {
        if(!strncmp(q->name,p))
        {
            printf("price is %d\n",q->price);
            return 0;
        }
        q = q->next;

    }
    printf("the book isn't here\n");
    return -1;
}

list add(list l,char* p)
{
    node* q;
    list stu;

    q = l->next;
    while(q != NULL)
    {
        if((!strncmp(q->name,p))||(!strncmp(q->name,"0")))
        {
            stu = (list)malloc(sizeof(node));
            printf("input the new book name and price\n");
            scanf("%s%d",stu->name,&stu->price);
            stu->next = q->next;
            q->next = stu;
            return l;
        }
        q = q->next;
    }
    printf("the position not exitst\n");
    return -1;
}

int del(list l,char* p)
{
    list q,before;
    q = l->next;
    before = l;
    while(q != NULL)
    {
        if(!strncmp(q->name,p))
        {
            before->next = q->next;
            free(q);
            return l;
        }
        before = q;
        q = q->next;
    }
    printf("the book you want del is not in our store\n");
}

int pri(list l)
{
    list p;

    p = l->next;

    while(p != NULL)
    {
        printf("book name:%s\n",p->name);
        printf("price:%d\n",p->price);
        p = p->next;
    }
    return 0;
}

int main()
{
    list l;
    int cmd = 0;
    l = (list)malloc(sizeof(node));

    char name[20] = {0};
    while(1)
    {
        printf("input your cmd\n");
        printf("0:creat;1:add;2:del;3:serch;4print\n");
        scanf("%d",&cmd);
        if((cmd > 4)||(cmd < 0))
        {
            printf("cmd err\n");
            continue;
        }
        switch(cmd)
        {
            case 0:
                 l = creatlist(l);
            break;
            case 1:
                printf("which book you want to add after\n");
                scanf("%s",name);
                add(l, name);
            break;
            case 2:
                printf("which book you want to del\n");
                scanf("%s",name);
                del(l,name);
            break;
            case 3:
                printf("which book you want to serch\n");
                scanf("%s",name);
                search(l,name);
            break;
            case 4:
                pri(l);
            break;
            default:
                printf("cmd err\n");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值