C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx

上传人:b****1 文档编号:14190982 上传时间:2023-06-21 格式:DOCX 页数:18 大小:19.36KB
下载 相关 举报
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第1页
第1页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第2页
第2页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第3页
第3页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第4页
第4页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第5页
第5页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第6页
第6页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第7页
第7页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第8页
第8页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第9页
第9页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第10页
第10页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第11页
第11页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第12页
第12页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第13页
第13页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第14页
第14页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第15页
第15页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第16页
第16页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第17页
第17页 / 共18页
C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx

《C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx》由会员分享,可在线阅读,更多相关《C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx(18页珍藏版)》请在冰点文库上搜索。

C语言程序设计第3版何钦铭 颜 晖 第9章结构.docx

C语言程序设计第3版何钦铭颜晖第9章结构

第9章结构

【练习9-1】定义一个能够表示复数的结构类型,一个复数包括实数与虚数两个部分。

解答:

structcomplex{

floatreal;

floatimaginary;

};

【练习9-2】人的出生日期由年、月、日组成,请在例9-1中的通讯录结构中增加一个成员:

出生日期,用嵌套定义的方式重新定义该结构类型。

解答:

structdate{

intyear;

intmonth;

intday;

};

structstudent{

intnum;

charname[10];

structdatebirthday;

intcomputer,english,math;

doubleaverage;

};

【练习9-3】例9-1中,如果要计算的是三门课程的课程平均成绩,应该如何改写程序?

解答:

#include

structstudent{

intnum;

charname[10];

intcomputer,english,math;

doubleaverage;

};

intmain(void)

{

inti,n;

doublemath_sum,english_sum,computer_sum;

structstudents1;

printf("Inputn:

");

scanf("%d",&n);

printf("Inputthestudent'snumber,nameandcoursescores:

\n");

math_sum=english_sum=computer_sum=0;

for(i=1;i<=n;i++){

printf("No.%d:

",i);scanf("%d%s%d%d%d",&s1.num,s1.name,&s1.math,&s1.english,&puter);

math_sum+=s1.math;

english_sum+=s1.english;

computer_sum+=puter;

}

printf("math_ave:

%.2lf\nenglish_ave:

%.2lf\ncomputer_ave:

%.2lf\n",math_sum/n,english_sum/n,computer_sum/n);

return0;

}

【练习9-4】定义一个包含5名学生信息的结构数组,并对该结构数组的所有元素进行初始化。

解答:

structstudent{

intnum;

charname[10];

intcomputer,english,math;

};

structstudents[5]={{30101,"张一",69,75,84},{30132,"李二",80,85,90},{40231,"王三",71,91,74},{40754,"赵四",65,76,87},{50426,"刘五",81,92,73}};

【练习9-5】参考例9-2,输入并保存10个学生的成绩信息,分别输出平均成绩最高和最低的学生信息。

解答:

#include

structstudent{

intnum;

charname[10];

intcomputer,english,math;

doubleaverage;

};

intmain(void)

{

inti,n,max,min;

structstudentstudents[50];

printf("Inputn:

");

scanf("%d",&n);

for(i=0;i

printf("InputtheinfoofNo.%d:

\n",i+1);

printf("number:

");

scanf("%d",&students[i].num);

printf("name:

");

scanf("%s",&students[i].name);

printf("mathscore:

");

scanf("%d",&students[i].math);

printf("englishscore:

");

scanf("%d",&students[i].english);

printf("computerscore:

");

scanf("%d",&students[i].computer);

students[i].average=(students[i].math+students[i].english+students[i].computer)/3.0;

}

max=min=0;

for(i=1;i

if(students[max].average

if(students[min].average>students[i].average)min=i;

}

printf("max_average_infortmation:

\n");

printf("number:

%d,name:

%s,math:

%d,english:

%d,computer:

%d,average:

%.2lf\n",students[max].num,students[max].name,students[max].math,students[max].english,students[max].computer,students[max].average);

printf("min_average_information:

\n");

printf("number:

%d,name:

%s,math:

%d,english:

%d,computer:

%d,average:

%.2lf\n",students[min].num,students[min].name,students[min].math,students[min].english,students[min].computer,students[min].average);

return0;

}

【练习9-6】定义一个structstudent类型的结构指针,用其实现一个学生信息的输入和输出。

解答:

structstudent{/*学生信息结构定义*/

intnum;/*学号*/

charname[10];/*姓名*/

intcomputer,english,math;/*三门课程成绩*/

doubleaverage;/*个人平均成绩*/

}s,*p;

p=&s;

scanf("%d%s%d%d%d",&p->num,p->name,&p->math,&p->english,&p->computer);

【练习9-7】改写例9-3中的函数update_score(),将第一个形参改为结构数组形式。

解答:

intupdate_score(structstudents[],intn,intnum,intcourse,intscore)

{

inti,pos;

for(i=0;i

if(s[i].num==num)

break;

if(i

switch(course){

case1:

s[i].math=score;break;

case2:

s[i].english=score;break;

case3:

s[i].computer=score;break;

}

pos=i;

}

else

pos=-1;

returnpos;

}

习题9

一、选择题

1.下面定义结构变量的语句中错误的是D。

A.structstudent{intnum;charname[20];}s;

B.struct{intnum;charname[20];}s;

C.structstudent{intnum;charname[20];};structstudents;

D.structstudent{intnum;charname[20];};students;

2.如果有定义语句:

struct{

intx,y;}s[2]={{1,3},{2,7}};

则语句:

printf(“%d\n”,s[0].y/s[1].x);输出结果为B。

A.0B.1C.2D.3

3.根据下面的定义,能打印出字母M的语句是C。

structperson{

charname[10];

intage;

}c[10]={“John”,17,“Paul”,19,“Mary”,18,“Adam”,16};

A.printf(“%c”,c[3].name);B.printf(“%c”,c[3].name[1]);

C.printf(“%c”,c[2].name[0]);D.printf(“%c”,c[2].name[1]);

4.设有如下定义,则对data中的a成员的正确引用是B。

structsk{inta;floatb;}data,*p=&data;

A.(*p).data.aB.(*p).a

C.p->data.aD.p.data.a

5.对于以下结构定义,(*p)->str++中的++加在D。

struct{intlen;char*str;}*p;

A.指针str上B.指针p上

C.str指向的内容上D.语法错误

二、填空题

1.“.”称为成员_(分量)_运算符,“->”称为_指向_运算符。

2.完成下列程序,该程序计算10名学生的平均成绩。

#include

#include

structstudent{

intnum;

charname[20];

intscore;

};

structstudentstud[10];

intmain(void)

{

inti,sum=0;

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

scanf("%d%s%d",&stud[i].num,_stud[i].name_,&stud[i].score);

sum+=stud[i].score;

}

printf("aver=%d\n",sum/10);

return0;

}

3.下列程序读入时间数值,将其加1秒后输出,时间格式为:

hh:

mm:

ss,即小时:

分钟:

秒,当小时等于24小时,置为0。

#include

struct{

inthour,minute,second;

}time;

intmain(void)

{

scanf("%d:

%d:

%d",_&time.hour,&time.minute,&time.second_);

time.second++;

if(_time.second_==60){

_time.minute++_;

time.second=0;

if(time.minute==60){

time.hour++;

time.minute=0;

if(_time.hour==24_)

time.hour=0;

}

}

printf("%d:

%d:

%d\n",time.hour,time.minute,time.second);

return0;

}

4.写出下面程序的运行结果12AB。

structs1{

charc1,c2;

intn;

};

structs2{

intn;

structs1m;

}m={1,{‘A’,’B’,2}};

intmain(void)

{

printf(“%d\t%d\t%c\t%c\n”,m.n,m.m.n,m.m.c1,m.m.c2);

return0;

}

5.写出下面程序的运行结果23,wang,98.5,wang。

structabc{

inta;

floatb;

char*c;};

intmain(void)

{

structabcx={23,98.5,"wang"};

structabc*px=&x;

printf("%d,%s,%.1f,%s\n",x.a,x.c,(*px).b,px->c);

return0;

}

三、程序设计题

1.时间换算:

用结构类型表示时间内容(时间以时、分、秒表示),输入一个时间数值,再输入一个秒数n(n<60),以h:

m:

s的格式输出该时间再过n秒后的时间值(超过24点就从0开始计时)。

试编写相应程序。

解答:

#include

structtime{

inthour;

intminute;

intsecond;

};

intmain(void)

{

intn;

structtimetim;

printf("Entertime:

");

scanf("%d%d%d",&tim.hour,&tim.minute,&tim.second);

printf("Enterseconds:

");

scanf("%d",&n);

tim.hour=(tim.hour*60*60+tim.minute*60+tim.second+n)/3600%24;

tim.minute=(tim.minute*60+tim.second+n)/60%60;

tim.second=(n+tim.second)%60;

printf("%d:

%d:

%d",tim.hour,tim.minute,tim.second);

return0;

}

2.计算两个复数之积:

编写程序,利用结构变量求解两个复数之积:

(3+4i)×(5+6i)。

提示:

求解(a1+a2i)×(b1+b2i),乘积的实部为:

a1×b1-a2×b2,虚部为:

a1×b2+a2×b1。

解答:

#include

structcomplex{

intreal,im;

};

structcomplexcmult(structcomplexa,structcomplexb);

intmain(void)

{

structcomplexa={3,4},b={5,6},c;

c=cmult(a,b);

printf("(%d+%di)(%d+%di)=%d+%di\n",a.real,a.im,b.real,b.im,c.real,c.im);

return0;

}

structcomplexcmult(structcomplexa,structcomplexb)

{

structcomplexc;

c.real=a.real*b.real-a.im*b.im;

c.im=a.real*b.im+a.im*b.real;

returnc;

}

3.平面向量加法:

输入两个二维平面向量V1=(x1,y1)和V2=(x2,y2)的分量,计算并输出两个向量的和向量。

试编写相应程序。

解答:

#include

structvector{

floatx,y;

};

structvectorvec(structvectorv1,structvectorv2);

intmain(void)

{

structvectorv1,v2,v;

printf("x1andy1:

");

scanf("%f%f",&v1.x,&v1.y);

printf("x2andy2:

");

scanf("%f%f",&v2.x,&v2.y);

v=vec(v1,v2);

printf("v=(%f,%f)",v.x,v.y);

return0;

}

structvectorvec(structvectorv1,structvectorv2)

{

structvectorv;

v.x=v1.x+v2.x;

v.y=v1.y+v2.y;

returnv;

}

4.查找书籍:

从键盘输入10本书的名称和定价并存入结构数组中,从中查找定价最高和最低的书的名称和定价,并输出。

试编写相应程序。

解答:

#include

#defineNUMBER10

structbook{

charname[10];

floatprice;

};

intmain(void)

{

inti,maxl,minl,n;

structbooktest[NUMBER];

for(i=0;i

scanf("%s%f",test[i].name,&test[i].price);

maxl=minl=0;

for(i=1;i

if(test[maxl].price

if(test[minl].price>test[i].price)minl=i;

}

printf("MaxPrice:

%f,%s\n",test[maxl].price,test[maxl].name);

printf("MinPrice:

%f,%s\n",test[minl].price,test[minl].name);

return0;

}

5.通讯录排序:

建立一个通讯录,通讯录的结构包括:

姓名、生日、电话号码;其中生日又包括三项:

年、月、日。

编写程序,定一个嵌套的结构类型,输入n(n≤10)个联系人信息,再按照他们的年龄从大到小依次输出其信息。

试编写相应程序。

解答:

#include

structdate{

intyear;

intmonth;

intday;

};

structfriends_list{

charname[10];

structdatebirthday;

charphone[15];

};

voidsort(structfriends_lists[],intn);

intmain(void)

{

inti,n;

structfriends_listfriends[10];

printf("Inputn:

");

scanf("%d",&n);

printf("Input%dfriends'name,birthdayandphone:

",n);

for(i=0;i

scanf("%s%d%d%d%s",friends[i].name,&friends[i].birthday.year,&friends[i].birthday.month,&friends[i].birthday.day,&friends[i].phone);

sort(friends,n);

for(i=0;i

printf("%s%d/%d/%d%s\n",friends[i].name,friends[i].birthday.year,friends[i].birthday.month,friends[i].birthday.day,friends[i].phone);

return0;

}

voidsort(structfriends_lists[],intn)

{

inti,j,index;

structfriends_listtemp;

for(i=0;i

index=i;

for(j=i+1;j

if(s[index].birthday.year>s[j].birthday.year)

index=j;

elseif((s[index].birthday.year==s[j].birthday.year)

&&(s[index].birthday.month>s[j].birthday.month))

index=j;

elseif((s[index].birthday.year==s[j].birthday.year)

&&(s[index].birthday.month==s[j].birthday.month)&&(s[index].birthday.day>s[j].birthday.day)){

index=j;

temp=s[i];

s[i]=s[index];

s[index]=temp;

}

}

}

6.按等级统计学生成绩:

输入10个学生的学号、姓名和成绩,输出学生的成绩等级和不及格人数。

每个学生的记录包括学号、姓名、成绩和等级,要求定义和调用函数set_grade()根据学生成绩设置其等级,并统计不及格人数,等级设置:

85-100为A,70-84为B,60-69为C,0-59为D。

试编写相应程序。

解答:

#include

#defineN10

structstudents{

intnum;

charname[20];

intscore;

chargrade;

};

intset_grade(structstudents*p);

intmain(void)

{

structstudentsstudent[N];

inti,count;

printf("Input10students'number,nameandscore:

\n");

for(i=0;i

printf("No.%d:

",i+1);

scanf("%d%s%d",&stud

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2