设为首页 加入收藏

TOP

c语言结构体和联合体例题(二)
2014-11-23 22:57:45 来源: 作者: 【 】 浏览:9
Tags:语言 结构 联合体 例题
itle, 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
#include
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
#include
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,&(book+1)->price);
scanf("%s%s%lf",(book+2)->title, (book+2)->author, &(book+2)->price);
scanf("%s%s%lf",(book+3)->title, (book+3)->author, &(book+3)->price);
}
void init() {
struct Library book[3];
}
void show(struct Library *(book+1), struct Library *(book+2), struct Library *(book+3)) {
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
(book+1)->title, (book+1)->author, (book+1)->price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
(book+2)->title,(book+2)->author, (book+2)->price);
printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
(book+3)->title, (book+3)->author, (book+3)->price);
}
int main(){
input(&(book+1),&(book+2),&(book+3));
init();
show(&(book+1),&(book+2),&(book+3));
return EXIT_SUCCESS;
}
第四题:
设计一个表示汽车信息的结构。
int main(){
struct car {
char name[20];
char sex[5];
char buyDate[20];
} owner = { "Jone", "M", "2008-01-01" };
struct company {
char name[20];
char tel[10];
} leaseCompany = { "hualong", "010-88064420" };
union data {
struct car owner;
struct company leaseCompany;
};
struct carData {
char make[20];
int status;
union data {
struct car owner;
struct company leaseCompany;
} ownerInfo;
} ;
struct carData flits = { .status = 0, .make = "volvo", .ownerInfo.ownerCar.sex =
'M', .ownerInfo.ownerCar.buyDate = '2008-11-21',
.ownerInfo.ownerCar.name = 'Rebort Carter' };
return 0;
}
第五题:
Wiliam Wingate从事比萨分析服务。对于每个比萨饼,他都需要
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言中##的用法 下一篇C语言学习笔记

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: