《带你学C带你飞》—— SE44传递结构体变量和结构体指针

it2022-05-05  107

使用结构体嵌套、结构体函数来写书的信息的程序 #include <stdio.h> struct Date { int year; int month; int day; }; struct Book { char title[200]; char author[40]; float price; struct Date date; char publisher[40]; }; struct Book getInput(struct Book book); void printBook(struct Book book); struct Book getInput(struct Book book) { printf("请输入书名:"); scanf("%s",book.title); printf("请输入作者:"); scanf("%s",book.author); printf("请输入售价:"); scanf("%f",&book.price); //scanf里字符型变量不可以写.f。要写%f printf("请输入出版日期:"); scanf("%d-%d-%d",&book.date.year, &book.date.month, &book.date.day); printf("请输入出版社:"); scanf("%s",book.publisher); return book; //这里一定要return book; } void printBook(struct Book book) //这个函数的类型写void,参数类型是结构体变量 { printf("书名:%s\n",book.title); printf("作者:%s\n",book.author); printf("售价:%.2f\n",book.price); printf("出版日期:%d-%d-%d\n",book.date.year, book.date.month, book.date.day); printf("出版社:%s\n",book.publisher); } int main(void) { struct Book b1,b2; printf("请输入第一本书的信息....\n"); b1 = getInput(b1); putchar('\n'); printf("请输入第二本书的信息....\n"); b2 = getInput(b2); printf("\n\n录入完毕,现在开始打印验证....\n\n"); printf("现在开始打印第一本书的信息:\n"); printBook(b1); putchar('\n'); printf("现在开始打印第二本书的信息:\n"); printBook(b2); return 0; }

为了提高程序运行效率,需要不直接传入结构体,而去传递结构体 的指针

#include <stdio.h> struct Date { int year; int month; int day; }; struct Book { char title[200]; char author[40]; float price; struct Date date; char publisher[40]; }; void getInput(struct Book *book); void printBook(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。要写%f printf("请输入出版日期:"); scanf("%d-%d-%d",&book->date.year, &book->date.month, &book->date.day); printf("请输入出版社:"); scanf("%s",book->publisher); } void printBook(struct Book *book) //这个函数的类型写void,参数类型是结构体变量 { printf("书名:%s\n",book->title); printf("作者:%s\n",book->author); printf("售价:%.2f\n",book->price); printf("出版日期:%d-%d-%d\n",book->date.year, book->date.month, book->date.day); printf("出版社:%s\n",book->publisher); } int main(void) { struct Book b1,b2; printf("请输入第一本书的信息....\n"); getInput(&b1); putchar('\n'); printf("请输入第二本书的信息....\n"); getInput(&b2); printf("\n\n录入完毕,现在开始打印验证....\n\n"); printf("现在开始打印第一本书的信息:\n"); printBook(&b1); putchar('\n'); printf("现在开始打印第二本书的信息:\n"); printBook(&b2); return 0; }

动态为结构体申请变量

#include <stdio.h> #include <stdlib.h> struct Date { int year; int month; int day; }; struct Book { char title[200]; char author[40]; float price; struct Date date; char publisher[40]; }; void getInput(struct Book *book); void printBook(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。要写%f printf("请输入出版日期:"); scanf("%d-%d-%d",&book->date.year, &book->date.month, &book->date.day); printf("请输入出版社:"); scanf("%s",book->publisher); } void printBook(struct Book *book) //这个函数的类型写void,参数类型是结构体变量 { printf("书名:%s\n",book->title); printf("作者:%s\n",book->author); printf("售价:%.2f\n",book->price); printf("出版日期:%d-%d-%d\n",book->date.year, book->date.month, book->date.day); printf("出版社:%s\n",book->publisher); } int main(void) { struct Book *b1,*b2; b1 = (struct Book *)malloc(sizeof(struct Book)); b2 = (struct Book *)malloc(sizeof(struct Book)); if (b1 == NULL || b2 == NULL) { printf("动态分配内存失败!\n"); //检查动态内存分配是否成功,失败exit(1); exit(1); } printf("请输入第一本书的信息....\n"); getInput(b1); putchar('\n'); printf("请输入第二本书的信息....\n"); getInput(b2); printf("\n\n录入完毕,现在开始打印验证....\n\n"); printf("现在开始打印第一本书的信息:\n"); printBook(b1); putchar('\n'); printf("现在开始打印第二本书的信息:\n"); printBook(b2); free(b1); //free函数和exit都需要使用stdlib.h头文件 free(b2); return 0; }

综上所学,构建一个图书馆程序

#include <stdio.h> #include <stdlib.h> struct Date { int year; int moon; int day; }; struct Book { char title[128]; char author[40]; float price; struct Date date; char publisher[40]; }; void getInput(struct Book *book); void printBook(struct Book * book); void getInput(struct Book *book) { printf("请输入书名: "); scanf("%s", book->title); printf("请输入作者: "); scanf("%s", book->author); printf("请输入售价: "); scanf("%f", &book->price); printf("请输入出版日期: "); scanf("%d-%d-%d", &book->date.year, &book->date.moon, &book->date.day); printf("请输入出版社: "); scanf("%s", book->publisher); } void printBook(struct Book *book) { printf("书名: %s\n", book->title); printf("作者: %s\n", book->author); printf("售价: %.2f\n", book->price); printf("出版日期: %d-%d-%d\n", book->date.year, book->date.moon, book->date.day); printf("出版社: %s\n", book->publisher); } int main(void) { int number; printf("你好请输入要存入的图书的数量: "); scanf("%d", &number); struct Book *bookarray[number]; for(int i = 0; i < number; i++){ bookarray[i] = (struct Book *)malloc(sizeof(struct Book)); if(bookarray[i] == NULL){ printf("Failure!!!"); exit(1); } printf("请输入第%d本书的信息\n", i+1); getInput(bookarray[i]); putchar('\n'); } printf("录入完毕现在打印验证....\n\n"); for(int i = 0; i < number; i++){ printf("第%d本书的信息\n", i+1); printBook(bookarray[i]); putchar('\n'); } return 0; }

最新回复(0)