c语言结构体和联合体例题
第一题:要求你设计一个能够保存图书信息的结构。图书属性包括:书名(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);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
dicken->title, dicken->author, dicken->price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
panshin->title, panshin->author, panshin->price);
return EXIT_SUCCESS;
}
*/
/*(3)编写从键盘上接受图书信息的函数input(),参数为结构的指针。函数的功能是从键盘上接收相关图
书信息,并将信息保存到指针所执行的图书结构变量里。
#include <stdio.h>
#include <stdlib.h>
struct Library {
const char title[20];
const char author[10];
double price;
};
void input(struct Library *doyle,struct Library dicken,){
scanf("%s%s%lf", doyle->title, doyle->author,&doyle->price);
scanf("%s%s%lf",dicken->title, dicken->author, &dicken->price);
scanf("%s%s%lf", panshin->title, panshin->author, &panshin->price);
}
int main(void) {
struct Library doyle;
struct Library dicken;
struct Library panshin ;
input(&doyle,&dicken,&panshin);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
doyle->title, doyle->author, doyle->price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
dicken->title, dicken->author, dicken->price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
panshin->title, panshin->author, panshin->price);
return EXIT_SUCCESS;
}*/
/*(4)主程序按照下面流程完成功能实现:
a)定义三个图书对象doyle、dicken、panshin。
b)对结构对象进行初始化。
c)从键盘上接收图书信息,分别保存到三个图书对象中。
d)输出三个图书对象的图书信息。
#include <stdio.h>
#include <stdlib.h>
struct Library {
const char title[20];
const char author[10];
double price;
}doyle,dicken,panshin;
int main(void) {
struct Library doyle;
struct Library dicken;
struct Library panshin ;
scanf("%s%s%lf", &doyle.title, &doyle.author, &doyle.price);
scanf("%s%s%lf",&dicken.title, &dicken.author, &dicken.price);
scanf("%s%s%lf", &panshin.title, &panshin.author, &panshin.price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
doyle.title, doyle.author, doyle.price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
dicken.title, dicken.author, dicken.price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
panshin.title, panshin.author, panshin.price);
return EXIT_SUCCESS;
}
*/
第三题:
创建一个图书馆library(结构数组),里面一共包含了上面这三本书。
创建一个结构数组library,使用上面所设计的函数init()对每本书进行初始化。
使用上面所设计的函数input()从键盘上接收图书信息。
使用上面的函数show,将输入的图书信息显示出来。
#include <stdio.h>
#include <stdlib.h>
struct Library {
const char title[20];
const char author[10];
double price;
} book[3];
void input(struct Library *(book+1),struct Library *(book+2),struct Library *(book+3)){
scanf("%s%s%lf", (book+1)->title, (book+1)->author
补充:软件开发 , C语言 ,
上一个:C语言学习笔记
下一个:C语言中结构体小技巧
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊