结构体
本帖最后由 林江楠 于 2021-11-8 19:50 编辑#include <stdio.h>
#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
struct Book *next;
}book;
void getInput(struct Book *book);
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title );
printf("请输入作者:");
scanf("%s",book->author );
printf("请输入价格:");
scanf("%f",&book->price );
printf("请输入日期:");
printf("请输入出版社:");
scanf("%s",book->publisher );
}
void addBook(struct Book **library);
void addBook(struct Book **library)
{
struct Book *book,*temp;
book = (struct Book *)malloc(sizeof(struct Book));
if(book == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(book);
if(*library != NULL)
{
temp = *library;
*library = book;
book->next = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1;
book = library;
while(book != NULL)
{
printf("Book%d:\n",count);
printf("书名:%s\n",book->title);
printf("作者:%s",book->author);
printf("价格:%.2f",book->price);
printf("日期:%d-%d-%d",book.date.year ,book.date.month ,book.date.day);
printf("出版社:%s",book->publisher);
book = book->next ;
count++;
}
}
void releaseLibrary(struct Book *library)
{
while(library != NULL)
{
free(library);
library = library->next ;
}
}
int main(void)
{
struct Book *library = NULL;
int ch;
while(1)
{
printf("请问是否要录入书籍信息(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y'&&ch != 'N');
if(ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
printf("请问是否需要打印图书馆信息(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y' && ch!= 'N');
if(ch== 'Y')
{
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
报错显示date不是一个结构体或共用体
想问一下怎末改一下
这程序可以使用结构体数组来实现吗?
getInput()函数需要传入的是一个地址,book是结构体的变量名,不是一个地址呀,问什么不会出错,而加上&就会出错
没有看代码逻辑,就仅仅只是能够通过编译,没有警告
不知道运行怎么样
#include <stdio.h>
#include <stdlib.h>
struct Date {
int year;
int month;
int day;
};
struct Book {
char title;
char author;
float price;
struct Date date;
char publisher;
struct Book *next;
} book;
void getInput(struct Book *book);
void getInput(struct Book *book) {
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
printf("请输入价格:");
//scanf("%f", &book->price);
scanf("%f", book->price);
printf("请输入日期:");
printf("请输入出版社:");
scanf("%s", book->publisher);
}
void addBook(struct Book **library);
void addBook(struct Book **library) {
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if(book == NULL) {
printf("内存分配失败!\n");
exit(1);
}
getInput(book);
if(*library != NULL) {
temp = *library;
*library = book;
book->next = temp;
} else {
*library = book;
book->next = NULL;
}
}
void printLibrary(struct Book *library) {
struct Book *book;
int count = 1;
book = library;
while(book != NULL) {
printf("Book%d:\n", count);
printf("书名:%s\n", book->title);
printf("作者:%s", book->author);
//printf("价格:%.2f", book->price);
printf("价格:%.2f", book->price);
printf("价格:%p", book->price);
//printf("日期:%d-%d-%d", book.date.year, book.date.month, book.date.day);
printf("日期:%d-%d-%d", book->date.year, book->date.month, book->date.day);
printf("出版社:%s", book->publisher);
book = book->next;
count++;
}
}
void releaseLibrary(struct Book *library) {
while(library != NULL) {
free(library);
library = library->next;
}
}
int main(void) {
struct Book *library = NULL;
int ch;
while(1) {
printf("请问是否要录入书籍信息(Y/N):");
do {
ch = getchar();
} while(ch != 'Y' && ch != 'N');
if(ch == 'Y') {
addBook(&library);
} else {
break;
}
}
printf("请问是否需要打印图书馆信息(Y/N):");
do {
ch = getchar();
} while(ch != 'Y' && ch != 'N');
if(ch == 'Y') {
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Date {
int year;
int month;
int day;
};
struct Book {
char title;
char author;
//float price;
float price;
struct Date date;
char publisher;
struct Book *next;
} book;
//void getInput(struct Book *book);
void getInput(struct Book *book) {
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
printf("请输入价格:");
//scanf("%f", &book->price);
//scanf("%f", book->price);
scanf("%f", &book->price);
printf("请输入日期:");
// 输入呢?
scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
printf("请输入出版社:");
scanf("%s", book->publisher);
}
//void addBook(struct Book **library);
void addBook(struct Book **library) {
//struct Book *book, *temp;
struct Book *book;
//book = (struct Book *)malloc(sizeof(struct Book));
book = malloc(sizeof(struct Book));
if(book == NULL) {
printf("内存分配失败!\n");
exit(1);
}
getInput(book);
/*
if(*library != NULL) {
temp = *library;
*library = book;
book->next = temp;
} else {
*library = book;
book->next = NULL;
}
*/
book->next = *library;
*library = book;
}
void printLibrary(struct Book *library) {
struct Book *book;
int count = 1;
book = library;
while(book != NULL) {
printf("Book%d:\n", count);
printf("书名:%s\n", book->title);
printf("作者:%s", book->author);
//printf("价格:%.2f", book->price);
//printf("价格:%.2f", book->price);
printf("价格:%.2f", book->price);
//printf("日期:%d-%d-%d", book.date.year, book.date.month, book.date.day);
printf("日期:%d-%d-%d", book->date.year, book->date.month, book->date.day);
printf("出版社:%s", book->publisher);
book = book->next;
count++;
}
printf("\n");
}
/*
void releaseLibrary(struct Book *library) {
while(library != NULL) {
free(library);
library = library->next;
}
}
*/
void releaseLibrary(struct Book *library) {
if(!library) return;
releaseLibrary(library->next);
free(library);
}
int main(void) {
struct Book *library = NULL;
int ch;
while(1) {
printf("请问是否要录入书籍信息(Y/N):");
do {
ch = getchar();
} while(ch != 'Y' && ch != 'N');
if(ch == 'Y') {
addBook(&library);
} else {
break;
}
}
printf("请问是否需要打印图书馆信息(Y/N):");
do {
ch = getchar();
} while(ch != 'Y' && ch != 'N');
if(ch == 'Y') {
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
第 73 行有错
printf("日期:%d-%d-%d",book.date.year ,book.date.month ,book.date.day);
这样修改
printf("日期:%d-%d-%d",book -> date.year , book -> date.month , book -> date.day) ; // 【修改】 jackz007 发表于 2021-11-8 17:06
第 73 行有错
这样修改
为什莫不能用点号运算符呢 林江楠 发表于 2021-11-8 19:19
为什莫不能用点号运算符呢
因为 book 是一个指针,指向结构体的指针
指针用 箭头(->)
结构体用 点 (.)
人造人 发表于 2021-11-8 19:27
因为 book 是一个指针,指向结构体的指针
指针用 箭头(->)
结构体用 点 (.)
记错了,我还以为指针直接可以用点号运算符,看了一眼笔记要加*进行解引用才可以,谢谢了 知道了,book在函数中是一个结构体指针 林江楠 发表于 2021-11-8 19:34
记错了,我还以为指针直接可以用点号运算符,看了一眼笔记要加*进行解引用才可以,谢谢了
你的代码还有其他问题,具体看 3 楼
指针和结构体用的符号不一样
页:
[1]