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
[csharp] view plain copy
print?#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(),参数为结构的指针。函数功能是将结构变量中的成员进行初始化。
[csharp] view plain copy
print?#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,将输入的图书信息显示出来。
print?#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,&(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; }
第四题:
设计一个表示汽车信息的结构。
print?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.比萨饼的重量。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。
程序将请求用户输入上述信息,然后显示这些信息。
[html] view plain copy
print?/*int main(){ struct pisa{ char name[20]; int zhijing; int zhongliang; }a={"Wiliam Wingate",6,2}; printf("比萨饼公司的名称:%s\n比萨饼的直径:%d\n比萨饼的重量:%d n",a.name,a.zhijing,a.zhongliang); return 0; }*/
第六题:
要求设计一个能够保存学生信息的结构。学生信息包括:姓名(Name)、年级(Grade)和成绩(score),并按照下面要求完成对于学生信息的操作。
print?/*struct stu { char Name[100]; char Grade[20]; int score; } stu1 = { "姜楠", "二年级", 78}; int main(void) { struct stu stu2 = { "何北", "二年级", 85 }; struct stu stu3 = { .Name = "董璐", .Grade = "二年级", .score = 99 }; printf("The Name is :%s\nThe Grade is :%s\nThe Score is :%d\n", stu1.Name, stu1.Grade, stu1.score); printf("\n"); printf("The Name is :%s\nThe Grade is :%s\nThe Score is :%d\n", stu2.Name, stu2.Grade, stu2.score); printf("\n"); printf("The Name is :%s\nThe Grade is :%s\nThe Score is :%d\n", stu3.Name, stu3.Grade, stu3.score); printf("\n"); return EXIT_SUCCESS; }*/
第七题:
为上面关于学生信息的程序添加三个函数:
1.编写显示学生信息的函数showInfo(),参数为结构的指针。显示学生信息的结构如下:
The Name is:Donglu
The Grade is:Two
The Score is:99
[html] view plain copy
print?
#include <stdio.h> #include <stdlib.h> struct Student { char Name[20]; int Grade[4]; int score; }; void showInfo(struct Student *stu3) { printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu3->Name, stu3->Grade,stu3->score); } int main() { struct Student stu3 = { 'Donglu', 'Two', 99 }; showInfo(&stu3); return EXIT_SUCCESS; } * */ /*```* 2.编写初始化结构变量的函数init(),参数为结构的指针。函数功能是将结构变量中的成员进行初始化。[csharp] view plain copy
print?
#include <stdio.h> #include <stdlib.h> struct Student { char Name[20]; int Grade[4]; int score; }; void init(struct Student *stu1,struct Student *stu2,struct Student *stu3){ stu1->Name; stu1->Grade; stu1->score; stu2->Name; stu2->Grade; stu2->score; stu3->Name; stu3->Grade; stu3->score; }
*/
/*3.编写从键盘上接收学生信息的函数input(),参数也是结构的指针。函数的功能是从键盘上接收相关学生信息,并把信息保存到指针所指向的结构变量里。[csharp] view plain copy
print?
#include <stdio.h> #include <stdlib.h> struct Student { char Name[20]; int Grade[4]; int score; }; void input(struct Student *stu1,struct Student *stu2,struct Student *stu3){ scanf("%s%d%d",&stu1->Name,&stu1->Grade,&stu1->score); scanf("%s%d%d",&stu2->Name,&stu2->Grade,&stu2->score); scanf("%s%d%d",&stu3->Name,&stu3->Grade,&stu3->score); } */
/*4.主函数按照下面流程完成功能实现:a)定义三个学生对象stu1,stu2,stu3.b)对结构对象进行初始化.c)从键盘上接收学生信息,分别保存到三个学生对象中。d)输出三个学生对象的信息。`#include <stdio.h> #include <stdlib.h> struct Student { char Name[20]; int Grade[4]; int score; }; void input(struct Student *stu1,struct Student *stu2,struct Student *stu3){ scanf("%s%d%d",&stu1->Name,&stu1->Grade,&stu1->score); scanf("%s%d%d",&stu2->Name,&stu2->Grade,&stu2->score); scanf("%s%d%d",&stu3->Name,&stu3->Grade,&stu3->score); } int main(){ struct Student stu1; struct Student stu2; struct Student stu3; input(&stu1,&stu2,&stu3); printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu1->Name,stu1- >Grade,stu1->score); printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu2->Name,stu2- >Grade,stu2->score); printf("The Name is:%s\n The Grade is:%d\n The Score is :%d", stu3->Name,stu3- >Grade,stu3->score); }` 第八题:用一个数组存放图书信息,每本图书包含书名(booktitle)、作者(author)、出版年月(date)、出版社(publishunit)、借出数目(lendnum)、库存数目(stocknum)等信息。编写程序输入若干本图书的信息,按出版年月排序后输出。
`#include <stdio.h> #include <stdlib.h> struct Data { int year; int month; int day; }; truct library { char booktitle[50]; char author[10]; struct Data data; char publishunit[100]; int lendnum; int stocknum; }; nt main() { int i, j, n, temp = 0; struct library book[n]; printf("请输入要处理的图书数量:\n"); fflush(stdout); scanf("%d", &n); for (i = 0; i < n; i++) { printf("请输入第%d本书的信息:\n", i + 1); printf("书名:"); fflush(stdout); scanf("%s", &book[i].booktitle); printf("作者:"); scanf("%s", &book[i].author); printf("出版年月:"); scanf("%s", &book[i].data); printf("出版社:"); scanf("%s", &book[i].publishunit); printf("借出数:"); scanf("%s", &book[i].lendnum); printf("库存数:"); scanf("%s", &book[i].stocknum); } for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (book[i].publishunit < book[j].publishunit) { temp = book[i]; book[i] = book[j]; book[j] = temp; } } printf("\n排序后的图书信息:"); for (i = 0; i < n; i++) { printf( "\n书名: %s\n, 作者: %s\n, 出版年月: %s\n, 出版社: %s\n, 借出数: %s\n, 库存数:%s\n", book[i].booktitle, book[i].author, book[i].data, book[i].publishunit, book[i].lendnum, book i].stocknum); } } return EXIT_SUCCESS; }
第九题:
编写程序,用union实现两个数的加、减、乘、除运算,每种运算用函数完成,并请考虑多个数的运算如何实现。
union yunsuan{ int a; int b; }f; int add(int a,int b) { int sum =0; f.a = a; sum+= f.a; f.b = b; sum+=f.b; printf("%d\n",sum); return sum; } int jian(int a,int b){ int sum =0; f.a = a; sum-=f.a; f.b =b; sum-=f.b printf("%d\n",sum); return sum; } int cheng(int a,int b){ int sum =0; f.a = a; sum*=f.a; f.b =b; sum*=f.b printf("%d\n",sum); return sum; } int chu(int a,int b){ int sum =0; f.a = a; sum/=f.a; f.b =b; sum*/=f.b printf("%d\n",sum); return sum; }
C语言-结构体与联合体相关推荐
- C语言结构体与联合体
c语言结构体与联合体 结构类型定义和结构变量说明 一.结构的定义 二.结构类型变量的说明 结构变量的赋值 结构变量的初始化 结构数组 结构指针变量 其访问的一般形式为: (*结构指针变量).成员名 结 ...
- 《C语言杂记》C语言结构体和联合体详解
1结构体概述 C 语言中有很多数据类型,数据类型决定了变量存储占用的空间,以及如何解释存储的位模式.像 int.float.char 等是由C语言本身提供的数据类型,不能再进行分拆,我们称之为基本数据 ...
- c语言结构体和联合体,C语言结构体和联合体
1.单链表插入 #include #include #define FALSE 0 #define TRUE 1 typedef struct NODE{ STRUCT NODE *link; int ...
- 初学C语言-结构体与联合体
结构体与联合体 一.结构体 1. 结构体类型的定义 2. 结构体类型变量的定义与使用 3. 结构体类型变量的赋值与初始化 4.结构体类型数组的定义与引用 5.结构体类型指针的定义与引用 6.结构体类型 ...
- c语言结构体和联合体例题
第一题: 要求你设计一个能够保存图书信息的结构.图书属性包括:书名(title).作者(author)和单价信息( price),并按照下面要求完成对于各种图书的相关操作. /* struct bo ...
- 自己实现了一个C语言例程,加深对宏、大小端、typeof宏、offsetof宏、指针变量、结构体、联合体的理解
如题所述,最近时间在复习C语言.自己实现了一个C语言例程,以加深对宏.大小端.typeof宏.offsetof宏.指针变量.结构体.联合体的理解.关于细分知识后续有空再填充- swap宏的使用: 对应 ...
- c语言shengchen图像,430编程C语言常识(IAR)(二)结构体与联合体
回复: 107 430编程C语言常识(IAR)(二)结构体与联合体 (288035412) 出0入0汤圆 电梯直达 发表于 2012-4-7 18:36:29 | 只看该作者 |倒序浏览 |阅读模式 ...
- c语言结构体加联合,C语言:结构体和联合体(共用体)
结构体:struct 1.结构体变量的首地址能够被其最宽基本类型成员的大小所整除. 2.结构体每个成员相对于结构体首地址的偏移量(offset)都是成员的整数倍. 3.结构体的总大小为结构体最宽基本类 ...
- C语言 结构体 联合体 | 嵌套使用
一.简单的实例分析 题目:获取0x12345678各个字节. 答案: //方法一: #include <stdio.h>typedef unsigned int uint32_t; typ ...
最新文章
- java如何对图片去除图片的白色的背景
- 信息安全研究之数据安全专题
- 【机器学习】libsvm使用的数据格式
- 在图片中如何生成带有文字边缘空心字体?
- java 判断今天_Java 判断某个具体时间是否属于当天范围(24H)
- Hadoop平台作业参数设置关于mapreduce.job.split.metainfo.maxsize的说明
- C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件
- 【已解决】scanf语句中%d后面多加一个空格,为什么数据需要多输入一个?
- SVN安装和使用总结
- 安装head插件依赖包grunt-cli
- 几大经典算法c语言cnds,各种算法的性能分析.docx
- 字体主题宝库:25款很好看的液晶数字字体下载
- iphone html复制粘贴,如何在iPhone上复制内容快速粘贴到iPad上?
- 多媒体——视频——利用视频视图VideoView播放视频
- UGP VR眼镜测评!测试一下可玩性如何!
- acw_sc__v2算法的两种方式
- “全国名中医学术经验传承暨经方临证与五运六气经典”高级研修班
- Android把文字写到图片上生成图片+文字的新图片
- 医疗数字化转型|浪潮信息为智慧医疗打造坚实的存储基座
- 数学建模学习笔记(十三)——主成分分析