程序设计基础(人民邮电)答案第9章结构体_第1页
程序设计基础(人民邮电)答案第9章结构体_第2页
程序设计基础(人民邮电)答案第9章结构体_第3页
程序设计基础(人民邮电)答案第9章结构体_第4页
程序设计基础(人民邮电)答案第9章结构体_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、选择题1. 根据下面的定义,能打印出字母M的语句是( D )。struct personchar name9;int age; ;struct person class10=John,17,Paul,19,Mary,18,Adam,16;A.printf(%cn,);B.printf(%cn,1);C.printf(%cn,1);D.printf(%cn,0);2. 若有以下结构体定义:struct exampleint x1;int y1;则 B 是正确的使用方式。Astruct example.x1

2、=100; Bstruct example xy;xy.x1=100;Cstruct xy;xy.x1=100; Dstruct example xy=100;3. 下面结构体数组的定义,错误的是( D )。Astruct student int num; char name10; float score;struct student stu30;B struct int num; char name10; float score;stu30;C struct student int num; char name10; float score;stu30;D struct stu30 int

3、num; char name10; float score;二、写出以下程序的运行结果(1)#include void main() struct cmplx int x; int y; cnum2=1,3,2,7; printf(%dn,cnum0.y/cnum0.x*cnum1.x);答案:6(2)#includevoid main() struct date int year,month,dat; today; printf(%dn,sizeof(struct date);答案:6(3)#includevoid main() struct MING struct int x; int y

4、; in; int a;int b;e; e.a=1;e.b=2; e.in.x=e.a*e.b; e.in.y=e.a+e.b; printf(%d,%d,e.in.x,e.in.y);答案:2,3(4)#include void main() struct abc int a,b,c; ; struct abc s2=1,2,3,4,5,6;int t; t=s0.a+s1.b; printf(%dn,t); 答案:6(5)#include#includestruct studentchar name9; char sex; float score2;struct student f(st

5、ruct student a)struct student b=Zhao,m,85.0,90.0; int i;strcpy(,);a.sex=b.sex;for(i=0;i2;i+) a.scorei=b.scorei;return a;void main()struct student c=Qian,f,95.0,92.0,d;d=f(c); printf(%s,%c,%2.0f,%2.0fn,,d.sex,d.score0, d.score1);答案: Zhao,m,85,903、 改错题(1) 定义一个二维平面上的点结构体,从键盘输入一个点的坐标并输

6、出到屏幕上。请找出以下程序的错误并修改。 #include struct point/声明点的结构体point int x;/x坐标 int y;/y坐标 (1)void main()/主函数 struct point pt;/定义结构体变量pt printf(Please input the point:); scanf(%d %d,pt.x,pt.y); (2) printf(The point you input is:x=%d,y=%d,&pt.x,&pt.y); (3)答案:(1) 最后加分号“;”。(2) 改为scanf(%d %d,&pt.x,&pt.y);(3) 改为print

7、f(The point you input is:x=%d,y=%d,pt.x,pt.y);(2)定义一结构体People,包括姓名,性别,职位,年龄,要求从键盘输入3名人员的相应信息并显示,请找出下面程序的错误并修改。#include struct People/结构体People char name20;/姓名 char sex;/性别 char position20;/职位 int age; /年龄; void main()struct People peo;/定义结构体数组,此处请修改! ( 1)int i;for(i=0;i3;i+)/输入 printf(Please input t

8、he %d Peoples namen,i+1); scanf(%c,);/输入姓名,此处请修改!(2) getchar(); printf(Please input the persons sexn); scanf(%c,peoi.sex);/输入性别,此处请修改! (3) getchar(); printf(Please input the Peoples positionn);/输入职位 scanf(%s,peoi.position); (4) getchar(); printf(Please input the persons agen);/输入年龄 scanf(%d

9、 ,&peoi.age);for(i=0;i3;i+)/输出 printf(The %d Peoples name is:%s,sex:%c,position:%s,age:%d n,i+1,,peoi.sex,peoi.position,&peoi.age);/此处请修改!(5)答案:( 1) 改为struct People peo;(2)改为scanf(%c,&);(3)改为scanf(%c&,peoi.sex);(4)改为scanf(%s,&peoi.position); (5) 改为:printf(The %d Peoples name is:%s,

10、sex:%c,position:%s,age:%d n,i+1,,peoi.sex,peoi.position,peoi.age);四、编程题(1) 设计一个通信录的结构体类型,并画出该结构体变量在内存的存储形式。struct photonote char name20; char number20;(2)用结构体变量表示矩形,编写矩形面积函数,矩形周长函数,输入矩形长宽函数,输出矩形长宽函数,输入矩形的长宽并进行面积和周长的计算。#include struct juxing float chang; float kuan;struct juxing shuru() stru

11、ct juxing r; printf( shu ru ju xing chang kuan :n); scanf(%f %f,&r.chang,&r.kuan); return r;void shuchu(struct juxing r) printf( chang shi : %fn,r.chang); printf( kuan shi : %fn,r.kuan);float mianji(struct juxing r) return r.chang*r.kuan;float zhouchang(struct juxing r) return 2*r.chang+2*r.kuan;voi

12、d main() struct juxing a; a=shuru(); shuchu(a); printf( mian ji wei : %fn,mianji(a); printf( zhou chang wei : %fn,zhouchang(a);(3)设有学生情况登记表如表9.3所示,用选择法对该表按成绩从小到大排序。表9.3学生情况登记表学号(num)姓名(name8)性别(sex)年龄(age)成绩(score)101Zhang M1998.3102Wang F1887.2103LiM2073.6104ZhaoF2034.6105MiaoM1899.4106GuoM1768.410

13、7WuF1956.9108XuF1845.0109LinM1976.5110Ma F1985.3#include struct Studentint num;char name8;char sex;int age;float score;void main()int i,j;int min;struct Student temp;struct Student stu10=101,zhang,M,19,98.3,102,wang,F,18,87.2,103,li,M,20,73.6,104,zhao,F,20,34.6,105,miao,M,18,99.4,106,guo,M,17,68.4,1

14、07,wu,F,19,56.9,108,xu,F,18,45.0,109,lin,M,19,76.5,110,ma,F,19,85.3;for(i=0;i9;i+) min = i ;for(j=i;j9;j+) if(stui.scorestuj+1.score) min=j+1; temp=stumin; stumin=stui; stui=temp;for(i=0;i=9;i+) printf(num=%d,name=%s,sex=%c,age=%d,score=%fn,stui.num,,stui.sex,stui.age,stui.score); getchar()

15、;(4)某班有20名学生,每名学生的数据包括学号、姓名、3门课的成绩,从键盘输入20名学生的数据,要求打印出每门课的平均成绩,以及每名学生的平均成绩并输出最高分的学生的数据(学号、姓名、3门课、平均成绩)。#include struct studentchar num4; char name8; float score3; float ave;void main() struct student s120; int i,j; float avg1=0,avg2=0,avg3=0,sum=0; for(i=0;i20;i+)scanf(%s%s%f%f%f,s1i.num,,&s

16、1i.score0,&s1i.score1,&s1i.score2); for(i=0;i20;i+) avg1+=s1i.score0; avg2+=s1i.score1; avg3+=s1i.score2; s1i.ave=(s1i.score0+s1i.score1+s1i.score2)/3; avg1/=20; avg2/=20; avg3/=20; printf(2 men ke de ping jun cheng ji : %f ,%f ,%fn,avg1,avg2,avg3); sum=s10.ave; j=0; for(i=1;i20;i+) if(s1j.aves1i.av

17、e) j=i; for(i=0;i20;i+) printf( ping jun cheng ji : %f n,s1i.ave); printf(ZuiGaoFen-XueHao:%s, XingMing:%s, ChengJi :%f %f %f, PingJun:%fn,s1j.num,,s1j.score0,s1j.score1,s1j.score2,s1j.ave);(5)统计候选人总得票数。假设有5名候选人,每次输入一个得票候选人的名字,要求最后按从大到小输出每个人的得票数排名。#include #include struct note char name20; i

18、nt count;void main() struct note notes5=zhao,0,qian,0,li,0,sun,0,wang,0;char tou20; int i,j; for(i=0;i20;i+) scanf(%s,tou); for(j=0;j20;j+) if(!strcmp(tou,) notesj.count+; for(i=0;i4;i+)for(j=i;j4;j+)if(notesi.countnotesj+1.count) struct note temp=notesi; notesi=notesj+1; notesj+1=temp; for(i=0;i5;i+) printf(XingMing : %s, PiaoShu: %dn,,notesi.count);(6)定义一个结构体变量(包括年、月、日)。计算该日在本年中是第

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论