第一范文网 - 专业文章范例文档资料分享平台

第9章结构体类型与共用体类型习题及答案 (4)

来源:用户分享 时间:2020-06-21 本文由梦离醒悟 分享 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

for(i=0;i

if(cs[i].sex=='m'||cs[i].sex=='M') {

msun=msun+1;

mav.event1=mav.event1+cs[i].event1; mav.event2=mav.event2+cs[i].event2; } else {

wsun=wsun+1 ;

mav.event1=mav.event1+cs[i].event1; mav.event2=mav.event2+cs[i].event2; } }

mav.event1/=msun;mav.event2/=msun; wav.event1/=wsun;wav.event2/=wsun; output(cs) ;

printf(\平均成绩:\\n 长跑=%6.2f, 登山=%6.2f,短跑=%6.2f,跳绳=%6.2f\\n \}

9-13 编写程序,用结构体类型实现复数的加、减、乘、除运算,每种运算用函数完成。 解:#include \struct complex {

float real ; float imag ; };

struct complex add(struct complex a ,struct complex b) {

struct complex temp ;

temp.real=a.real+b.real ; temp.imag=a.imag+b.imag ;

return temp ; }

struct complex sub(struct complex a ,struct complex b) {

struct complex temp ;

temp.real=a.real-b.real ; temp.imag=a.imag-b.imag ; return temp ; }

struct complex mul(struct complex a ,struct complex b) {

struct complex temp ;

temp.real=a.real*b.real-a.imag*b.imag; temp.imag=a.real*b.imag+a.imag*b.real; return temp ; }

struct complex div(struct complex a ,struct complex b) {

struct complex temp ; float t ;

t=(b.real*b.real+b.imag*b.imag);

temp.real=(a.real*b.real+a.imag*b.imag)/t; temp.imag=(b.real*a.imag-a.real*b.imag)/t; return temp ; }

struct complex disp(struct complex t) {

printf(\ if(t.imag>0) printf(\ if(t.imag!=0)

printf(\ return t ; }

void main()

{

struct complex x,y ;

struct complex s1,s2,s3,s4;

printf(\请输入第一个数的实部,虚部:\ scanf(\

printf(\请输入第二个数的实部,虚部:\ scanf(\ s1=add(x,y); s2=sub(x,y); s3=mul(x,y); s4=div(x,y);

printf(\所求复数的和=\ printf(\所求复数的差=\ printf(\所求复数的积=\ printf(\所求复数的商=\ }

思考: 函数在返回时,传给调用函数的值是一个吗?为什么?结构体类型也可以作为返回类型吗?

9-14 编写程序,计算四边形的面积和周长,要求用结构体类型描述四边形。

解:#include \#include \

struct point //点的结构体 {

float x; float y ; };

struct sbx //四边形结构体用四个顶点表示 {

struct point p[4] ; };

float dist(struct point p1 ,struct point p2) //求两点距离 {

float x=0;

x=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)) ; return x ; }

void main() {

struct sbx a ; float zc,mj ; int i ;

float d01,d12,d23,d30,d02; float c1,c2;

for( i=0;i<4;i++) {

printf(\请输入第%d个点的坐标:\\n\ printf(\(x,y)\

scanf(\ }

/*计算四边形的周长*/ d01=dist(a.p[0],a.p[1]); d12=dist(a.p[1],a.p[2]); d23=dist(a.p[2],a.p[3]); d30=dist(a.p[3],a.p[0]); zc=d01+d12+d23+d30;

/*计算面积(将四边形分割为俩个三角形)*/ d02=dist(a.p[0],a.p[2]) ; c1=(d01+d12+d02)/2; c2=(d30+d23+d02)/2;

mj=sqrt(c1*(c1-d01)*(c1-d12)*(c1-d02))+sqrt(c2*(c2-d30)*(c2-d23)*(c2-d02));

/*输出结果*/

printf(\周长=%7.2f,面积=%7.2f\\n\}

思考:如果把此题中的四边形改为平行四边形,程序该如何改写? 9-15 编写程序,用枚举类型实现俩个数的加、减、乘、除运算,每

种运算用函数完成,并请考虑多个数的运算如何实现。 解:#include \float add(float a,float b) {

float temp ; temp=a+b; return temp ; }

float sub(float a,float b) {

float temp ; temp=a-b; return temp ; }

float mul(float a,float b) {

float temp ; temp=a*b; return temp ; }

float div(float a,float b) {

float temp ; temp=a/b; return temp ; }

void main() {

enum yunsuanfu{a,b,c,d}js; //用a,b,c,d分别表示加、减、乘、除 float x,y,s;

printf(\输入第一个数据:\ scanf(\

printf(\输入第二个数据:\ scanf(\

搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新小学教育第9章结构体类型与共用体类型习题及答案 (4)全文阅读和word下载服务。

第9章结构体类型与共用体类型习题及答案 (4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/wenku/1089508.html(转载请注明文章来源)
热门推荐
Copyright © 2018-2022 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top