鱼C论坛

 找回密码
 立即注册
查看: 2647|回复: 2

求结构体数组指针构建图书馆管理系统的程序

[复制链接]
发表于 2021-3-5 11:33:21 | 显示全部楼层 |阅读模式

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

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

x
感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-5 11:49:53 | 显示全部楼层
        楼主越来越懒了,以前写了那么多结构数组的代码练习,自己拼凑个大概还做不到吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-4-10 14:31:14 | 显示全部楼层
我的数组元素是结构体,而不是结构体指针,怎么改成结构体指针呢
#include <stdio.h>
#include <stdlib.h>

struct Date
{
        int year;
        int month;
        int day;
};

struct Book
{
        char title[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[40];
};

void getInput(struct Book *);
void printBook(struct Book *);

void getInput(struct Book *book)
{
        printf("title: ");
        scanf("%s", book->title);
        printf("author: ");
        scanf("%s", book->author);
        printf("price: ");
        scanf("%f", &book->price);
        printf("date: ");
        scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
        printf("publisher: ");
        scanf("%s", book->publisher);
}

void printBook(struct Book *book)
{
        printf("title: %s\n", book->title);
        printf("author: %s\n", book->author);
        printf("price: %.2f\n", book->price);
        printf("date: %u\n", book->date.year, book->date.month, book->date.day);
        printf("publisher: %s\n", book->publisher);
}

int main(void)
{
        int count = 1, zhiling, temp, i;
        struct Book *b = NULL;

        b = (struct Book *)malloc(sizeof(struct Book));
        if(b == NULL)
        {
                printf("Failed!\n");
                exit(1);
        }

        printf("Write the first book!\n");
        getInput(&b[0]);

        do
        {
                printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
                scanf("%d", &zhiling);

                switch(zhiling)
                {
                        case 0:{
                                free(b);
                                printf("Thanks for use!\n");
                                return 0;
                        }break;

                        case 1:{
                                count++;
                                b = (struct Book *)realloc(b, count * sizeof(struct Book));
                                if(b == NULL)
                                {
                                        printf("Failed!\n");
                                        exit(1);
                                }

                                printf("This is No.%d book!\n", count);
                                getInput(&b[count - 1]);
                        }break;

                        case 2:{
                                for(i = 0; i < count; i++)
                                {
                                        printf("No.%d book: %s\n", i + 1, b[i].title);
                                }
                                printf("Which one do you want?");
                                scanf("%d", &temp);

                                printBook(&b[temp - 1]);
                        }break;
                }
                putchar('\n');
        }while(1);

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 19:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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