C语言-图书管理系统-指针

C语言-图书管理系统-指针

需要源码的请加微信249278427,申请请备注好需要的源码标题,顺便点赞、评论、转发,谢谢!

一、运行效果:
在这里插入图片描述
二、代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct book {
    char num[20];
    char name[48];
    char author[24];
    char press[24];
    char date[12];
    float price;
};

struct Node {
//    int data;
    struct book data;
    struct Node *next;
};

//创建表
struct Node *createList() {
    struct Node *headNode = (struct Node *) malloc(sizeof(struct Node));
    headNode->next = NULL;
    return headNode;
}

//创建节点
struct Node *createNode(struct book data) {
    struct Node *newNode = (struct Node *) malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

//校验时间
char *checkDate() {
    printf("出版日期:");
    int year, month, day, leap;
    while (1) {
        int mon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        fflush(stdin);
        scanf("%d/%d/%d", &year, &month, &day);
        if (year < 1 || month < 1 || month > 12) {
            printf("不正确的日期,请重新输入:");
            continue;
        }

        leap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));

        if ((day < 1 || day > mon[month]) && ((month != 2) || ((month == 2) && (day > mon[month] + leap)))) {
            printf("不正确的日期,请重新输入:");
            continue;

        }
        break;
    }
    static char ret[12];

    sprintf(ret, "%d/%d/%d", year, month, day);

    return ret;
}

//插入节点
void insertNodeByHead(struct Node *headNode, struct book data) {
    printf("统一书号:");
    scanf("%s", data.num);
    printf("书   名:");
    scanf("%s", data.name);
    printf("作   者:");
    scanf("%s", data.author);
    printf("出 版 社:");
    scanf("%s", data.press);
    char *date;
    date = checkDate(data);
    sprintf(data.date, "%s", date);
    printf("价   格:");
    scanf("%f", &data.price);

    struct Node *newNode = createNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}

//查找功能
struct Node *searchInfoByData(struct Node *headNode) {
    printf("书名:");
    char i[20];
    scanf("%s", i);
    char *name = i;

    struct Node *pMove = headNode->next;
    struct Node *posFrontNode = headNode;

    if (pMove == NULL) {
        printf("查无此书!\n");
        return 0;
    }

    //书号是字符串,字符串:strcmp
    while (strcmp(pMove->data.name, name) != 0) {
        posFrontNode = pMove;
        pMove = posFrontNode->next;
        if (pMove == NULL) {
            printf("查无此书!\n");
            return 0;
        }
    }

    if (pMove != NULL) {
        printf("统一书号-------- 书名---------- 作者----------- 出版社-------- 出版日期-------- 价格---\n");
    }

    while (pMove) {
        //书号是字符串,字符串:strcmp
        while (strcmp(pMove->data.name, name) != 0) {
            posFrontNode = pMove;
            pMove = posFrontNode->next;
            if (pMove == NULL) {
                return 0;
            }
        }

        if (pMove != NULL) {
            printf("%s\t%s\t%s\t\t%s\t%s\t%0.2f\n", pMove->data.num, pMove->data.name, pMove->data.author,
                   pMove->data.press,
                   pMove->data.date, pMove->data.price);
            pMove = pMove->next;

        }
    }
    return pMove;
}

//指定位置删除
void deletePointNode(struct Node *headNode) {
    printf("统一书号:");
    char i[20];
    scanf("%s", i);
    char *num = i;

    struct Node *posNode = headNode->next;
    struct Node *posFrontNode = headNode;
    if (posNode == NULL) {
        printf("查无此书!\n");
        return;
    }

    //书号是字符串,字符串:strcmp
    while (strcmp(posNode->data.num, num) != 0) {
        posFrontNode = posNode;
        posNode = posFrontNode->next;
        if (posNode == NULL) {
            printf("查无此书!\n");
            return;
        }
    }

    char choice = 0;
    while (choice != 'y' && choice != 'Y' && choice != 'N' && choice != 'n') {
        printf("删除(Y/N)?");
        fflush(stdin);
        scanf("%c", &choice);
        switch (choice) {
            case 'Y':
            case 'y':
                posFrontNode->next = posNode->next;
                free(posNode);
                break;
            case 'N':
            case 'n':
                break;
            default:
                printf("不正确的选项,请重新选择!\n");
        }
    }
}

//更新指定图书
int updatePointNode(struct Node *headNode, struct book data) {
    char i = 0;
    printf("统一书号:");
    char j[20];
    scanf("%s", j);
    char *num = j;

    struct Node *list = headNode;
    struct Node *posNode = headNode->next;
    struct Node *posFrontNode = headNode;

    if (posNode == NULL) {
        printf("查无此书!\n");
        return 0;
    }

    //书号是字符串,字符串:strcmp
    while (strcmp(posNode->data.num, num) != 0) {
        posFrontNode = posNode;
        posNode = posFrontNode->next;
        if (posNode == NULL) {
            printf("查无此书!\n");
            return 0;
        }
    }

    char choice = 0;
    while (choice != 'y' && choice != 'Y' && choice != 'N' && choice != 'n') {
        printf("修改(Y/N)?");
        fflush(stdin);
        scanf("%c", &choice);
        switch (choice) {
            case 'Y':
            case 'y':
                posFrontNode->next = posNode->next;
                free(posNode);

                i = 1;
                break;

            case 'N':
            case 'n':
                return 0;
            default:
                printf("不正确的选项,请重新选择!\n");
        }
    }

    if (i == 1) {
        insertNodeByHead(list, data);
    }
}


//打印链表
void printList(struct Node *headNode) {
    struct Node *pMove = headNode->next;
    printf("统一书号-------- 书名---------- 作者----------- 出版社-------- 出版日期-------- 价格---\n");
    while (pMove) {
        printf("%s\t%s\t%s\t\t%s\t%s\t%0.2f\n", pMove->data.num, pMove->data.name, pMove->data.author,
               pMove->data.press,
               pMove->data.date, pMove->data.price);
        pMove = pMove->next;
    }
    printf("\n");
}


int main() {
    struct Node *list = createList();

    char choice = 0;
    struct book data;

    while (choice != 'q' && choice != 'Q') {
        printf("A-添加 F-查找 R-删除 M-修改 S-显示 Q-退出 >");
        fflush(stdin);
        choice = getchar();
        switch (choice) {
            case 'a':
            case 'A':
                //添加
                insertNodeByHead(list, data);
                break;
            case 'f':
            case 'F':
                //查找(按书名找)
                searchInfoByData(list);
                break;
            case 'r':
            case 'R':
                //删除(根据书号)
                deletePointNode(list);
                break;
            case 'm':
            case 'M':
                //修改(根据书号)
                updatePointNode(list, data);
                break;
            case 's':
            case 'S':
                //打印列表
                printList(list);
                break;
            case 'q':
            case 'Q':
                //退出
                printf("退出\n");
                break;
            default:
                printf("不正确的选项,请重新选择!\n");
                break;
        }

    }

    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值