第一题:
要求你设计一个能够保存图书信息的结构。图书属性包括:书名(title)、作者(author)和单价信息(price),并按照下面要求完成对于各种图书的相关操作。
/*
struct books {
char title[100];
char author[20];
double price;
} doyle = { "My life as a budgie", "Mack Tom", 14.6 };
int main(void) {
struct books dicken = { "Thinking in C++", "Stephen Prata", 78 };
struct books panshin = { .title = "C++ Primer", .author = "Stanley Lippman",
.price = 92.5 };
printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
doyle.title, doyle.author, doyle.price);
printf("\n");
printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
dicken.title, dicken.author, dicken.price);
printf("\n");
printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
panshin.title, panshin.author, panshin.price);
printf("\n");
printf("“Thinking in C++”这本书的价格调整后为:\n");
printf("\n");
printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
dicken.title, dicken.author, dicken.price = 85);
return EXIT_SUCCESS;
}
*/
第二题:
为上面的关于图书的程序,添加三个函数:
/*(1)编写显示图书信息函数show()。参数为结构的指针。显示图书信息的结构如下:
The title is :My life as a budgie
The author is :Mack Tom
The price is :14.6
#include <stdio.h>
#include <stdlib.h>
struct Library {
const char title[20];
const char author[10];
double price;
} panshin;
void show(struct Library *doy) {
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",doy->title,
doy->author, doy->price);
}
int main(void) {
struct Library doyle = { "My life as a budgie", "Mack Tom", 14.6 };
show(&doyle);
return EXIT_SUCCESS;
}*/
/*(2)编写初始化结构变量函数init(),参数为结构的指针。函数功能是将结构变量中的成员进行初始化。
#include <stdio.h>
#include <stdlib.h>
struct Library {
const char title[20];
const char author[10];
double price;
} panshin;
void init(struct Library *doyle, struct Library *dicken, struct Library *panshin) {
doyle ->title;
dicken ->author;
panshin ->price;
}
int main(void) {
struct Library doyle = { "My life as a budgie", "Mack Tom", 14.6 };
struct Library dicken ={ "Thinking in C++", "Stephen Prata", 78 };
struct Library panshin = { "C++ Prinner", "Stanley Lippman", 92.5 };
init(&doyle,&dicken,&panshin);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
doyle->title, doyle->author, doyle->price);
print