|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 焦糖橙子 于 2021-8-8 09:57 编辑
建立一个图书馆,录入书本信息
我在录入售价的时候出了问题,能看看哪里写错了吗?
#include<stdio.h>
#include<stdlib.h>
void get_imput(struct Book *library);//录入信息
void printBook(struct Book *library);//打印信息
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
};
int main(void)
{
int n;
struct Book *library[n];
printf("请输入要录入的书本数量:");
scanf("%d",&n);
for(int i=0;i<n;i++)//分配内存空间
{
library[i]=(struct Book*)malloc(sizeof(struct Book));
}
for(int i=0;i<n;i++)
{
printf("正在录入第 %d 本书的信息...\n",i+1);
get_imput(library[i]);
}
printf("====已经全部录入,现在开始打印验证====\n");
for(int i=0;i<n;i++)
{
printf("打印第 %d 本书的信息...\n",i+1);
}
for(int i=0;i<n;i++)//释放内存空间
{
free(library[i]);
}
return 0;
}
void get_imput(struct Book *library)
{
printf("请输入书名:");
scanf(" %s",library->title);
printf("请输入作者:");
scanf(" %s",library->author);
printf("请输入售价:");
scanf("%f",library->price);
printf("请输入出版日期:");
scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
printf("请输入出版社:");
scanf(" %s",library->publisher);
}
void printBook(struct Book *library)
{
printf("书名:《%s》",library->title);
printf("作者:%s",library->author);
printf("售价:%.2f",library->price);
printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
printf("出版社:%s",library->publisher);
}
#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 get_imput(struct Book *library);//录入信息
void printBook(struct Book *library);//打印信息
int main(void)
{
int n;
printf("请输入要录入的书本数量:");
scanf("%d",&n);
struct Book *library[n];
for(int i=0;i<n;i++)//分配内存空间
{
library[i]=(struct Book*)malloc(sizeof(struct Book));
}
for(int i=0;i<n;i++)
{
printf("正在录入第 %d 本书的信息...\n",i+1);
get_imput(library[i]);
}
printf("====已经全部录入,现在开始打印验证====\n");
for(int i=0;i<n;i++)
{
printf("打印第 %d 本书的信息...\n",i+1);
}
for(int i=0;i<n;i++)//释放内存空间
{
free(library[i]);
}
return 0;
}
void get_imput(struct Book *library)
{
printf("请输入书名:");
scanf(" %s",library->title);
printf("请输入作者:");
scanf(" %s",library->author);
printf("请输入售价:");
scanf("%f",&library->price);
printf("请输入出版日期:");
scanf("%d-%d-%d",&library->date.year,&library->date.month,&library->date.day);
printf("请输入出版社:");
scanf(" %s",library->publisher);
}
void printBook(struct Book *library)
{
printf("书名:《%s》",library->title);
printf("作者:%s",library->author);
printf("售价:%.2f",library->price);
printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
printf("出版社:%s",library->publisher);
}
|
|