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

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

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

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

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

snumber,nameandcoursescores:

\n"

math_sum=english_sum=computer_sum=0;

for(i=1;

i<

=n;

i++){

No、%d:

i);

%d%s%d%d%d"

&

s1、num,s1、name,&

s1、math,&

s1、english,&

s1、computer);

math_sum+=s1、math;

english_sum+=s1、english;

computer_sum+=s1、computer;

}

math_ave:

%、2lf\nenglish_ave:

%、2lf\ncomputer_ave:

%、2lf\n"

math_sum/n,english_sum/n,computer_sum/n);

return0;

}

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

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个学生的成绩信息,分别输出平均成绩最高与最低的学生信息。

{

inti,n,max,min;

structstudentstudents[50];

printf("

scanf("

for(i=0;

n;

printf("

InputtheinfoofNo、%d:

i+1);

number:

scanf("

students[i]、num);

name:

%s"

students[i]、name);

mathscore:

students[i]、math);

englishscore:

students[i]、english);

computerscore:

students[i]、computer);

students[i]、average=(students[i]、math+students[i]、english+students[i]、computer)/3、0;

}

max=min=0;

for(i=1;

if(students[max]、average<

students[i]、average)max=i;

if(students[min]、average>

students[i]、average)min=i;

max_average_infortmation:

printf("

%d,name:

%s,math:

%d,english:

%d,computer:

%d,average:

students[max]、num,students[max]、name,students[max]、math,students[max]、english,students[max]、computer,students[max]、average);

min_average_information:

students[min]、num,students[min]、name,students[min]、math,students[min]、english,students[min]、computer,students[min]、average);

return0;

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

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

intnum;

/*学号*/

/*姓名*/

intcomputer,english,math;

/*三门课程成绩*/

doubleaverage;

/*个人平均成绩*/

}s,*p;

p=&

s;

scanf("

p->

num,p->

name,&

math,&

english,&

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<

n){

switch(course){

case1:

s[i]、math=score;

case2:

s[i]、english=score;

case3:

s[i]、computer=score;

pos=i;

else

pos=-1;

returnpos;

习题9

一、选择题

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

A.structstudent{intnum;

charname[20];

}s;

B.struct{intnum;

C.structstudent{intnum;

};

structstudents;

D.structstudent{intnum;

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{

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<

string、h>

structstudent{

charname[20];

intscore;

structstudentstud[10];

inti,sum=0;

for(i=0;

i<

10;

i++){

%d%s%d"

&stud[i]、num,_stud[i]、name_,&stud[i]、score);

sum+=stud[i]、score;

aver=%d\n"

sum/10);

return0;

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

hh:

mm:

ss,即小时:

分钟:

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

inthour,minute,second;

}time;

%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\n"

time、hour,time、minute,time、second);

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

structs1{

charc1,c2;

intn;

structs2{

structs1m;

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

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

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

structabc{

inta;

char*c;

structabcx={23,98、5,"

wang"

structabc*px=&

x;

%d,%s,%、1f,%s\n"

x、a,x、c,(*px)、b,px->

c);

三、程序设计题

1、时间换算:

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

60),以h:

m:

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

试编写相应程序。

structtime{

inthour;

intminute;

intsecond;

intn;

structtimetim;

Entertime:

%d%d%d"

tim、hour,&

tim、minute,&

tim、second);

Enterseconds:

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;

tim、hour,tim、minute,tim、second);

2、计算两个复数之积:

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

(3+4i)×

(5+6i)。

提示:

求解(a1+a2i)×

(b1+b2i),乘积的实部为:

a1×

b1-a2×

b2,虚部为:

b2+a2×

b1。

intreal,im;

structcomplexcmult(structcomplexa,structcomplexb);

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

c=cmult(a,b);

(%d+%di)(%d+%di)=%d+%di\n"

a、real,a、im,b、real,b、im,c、real,c、im);

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)的分量,计算并输出两个向量的与向量。

structvector{

floatx,y;

structvectorvec(structvectorv1,structvectorv2);

structvectorv1,v2,v;

x1andy1:

%f%f"

v1、x,&

v1、y);

x2andy2:

v2、x,&

v2、y);

v=vec(v1,v2);

v=(%f,%f)"

v、x,v、y);

structvectorvec(structvectorv1,structvectorv2)

structvectorv;

v、x=v1、x+v2、x;

v、y=v1、y+v2、y;

returnv;

4、查找书籍:

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

#defineNUMBER10

structbook{

floatprice;

inti,maxl,minl,n;

structbooktest[NUMBER];

NUMBER;

i++)

%s%f"

test[i]、name,&

test[i]、price);

maxl=minl=0;

if(test[maxl]、price<

test[i]、price)maxl=i;

if(test[minl]、price>

test[i]、price)minl=i;

MaxPrice:

%f,%s\n"

test[maxl]、price,test[maxl]、name);

MinPrice:

test[minl]、price,test[minl]、name);

5.通讯录排序:

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

姓名、生日、电话号码;

其中生日又包括三项:

年、月、日。

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

structfriends_list{

structdatebirthday;

charphone[15];

voidsort(structfriends_lists[],intn);

inti,n;

structfriends_listfriends[10];

Input%dfriends'

name,birthdayandphone:

n);

%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);

%s%d/%d/%d%s\n"

friends[i]、name,friends[i]、birthday、year,friends[i]、birthday、month,friends[i]、birthday、day,friends[i]、phone);

}

voidsort(structfriends_lists[],intn)

inti,j,index;

structfriends_listtemp;

n-1;

index=i;

for(j=i+1;

j<

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))

(s[index]、birthday、month==s[j]、birthday、month)&

(s[index]、birthday、day>

s[j]、birthday、day)){

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。

#defineN10

structstudents{

chargrade;

intset_grade(structstudents*p);

structstudentsstudent[N];

inti,count;

Input10students'

number,nameandscore:

\n"

N;

%d%s%d"

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

当前位置:首页 > 医药卫生 > 基础医学

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

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