|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.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];
};
struct Book getInput(struct Book book);
void printBook(struct Book book);
struct Book getInput(struct Book book)
{
printf("请输入书名:");
scanf_s("%s", book.title);
printf("请输入作者:");
scanf_s("%s", book.author);
printf("请输入价格:");
scanf_s("%.2f", &book.price);
printf("请输入日期:");
scanf_s("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
printf("请输入出版社:");
scanf_s("%s", book.publisher);
return book;
}
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.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("打印第一本书的信息...");
printBook(b1);
putchar('\n');
printf("打印第二本书的信息...");
printBook(b2);
return 0;
}
- #include <stdio.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];
- };
- //struct Book getInput(struct Book book);
- //void printBook(struct Book book);
- //struct Book getInput(struct Book book)
- //struct Book *getInput(struct Book *book)
- void getInput(struct Book *book)
- {
- printf("请输入书名:");
- //scanf_s("%s", book.title);
- scanf("%s", book->title);
- printf("请输入作者:");
- //scanf_s("%s", book.author);
- scanf("%s", book->author);
- printf("请输入价格:");
- //scanf_s("%.2f", &book.price);
- scanf("%2f", &book->price);
- printf("请输入日期:");
- //scanf_s("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
- scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
- printf("请输入出版社:");
- //scanf_s("%s", book.publisher);
- scanf("%s", book->publisher);
- //return book;
- }
- //void printBook(struct Book book)
- 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.month, book.date.day);
- printf("出版社:%s\n", book.publisher);
- */
- 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);
- getInput(&b1);
- putchar('\n');
- printf("请录入第二本书的信息:\n");
- //b2 = getInput(b2);
- getInput(&b2);
- printf("\n\n录入完毕,现在开始打印...\n\n");
- printf("打印第一本书的信息...");
- //printBook(b1);
- printBook(&b1);
- putchar('\n');
- printf("打印第二本书的信息...");
- //printBook(b2);
- printBook(&b2);
- return 0;
- }
复制代码
|
|