结构体Word格式.docx

上传人:b****2 文档编号:4774736 上传时间:2023-05-04 格式:DOCX 页数:33 大小:39.87KB
下载 相关 举报
结构体Word格式.docx_第1页
第1页 / 共33页
结构体Word格式.docx_第2页
第2页 / 共33页
结构体Word格式.docx_第3页
第3页 / 共33页
结构体Word格式.docx_第4页
第4页 / 共33页
结构体Word格式.docx_第5页
第5页 / 共33页
结构体Word格式.docx_第6页
第6页 / 共33页
结构体Word格式.docx_第7页
第7页 / 共33页
结构体Word格式.docx_第8页
第8页 / 共33页
结构体Word格式.docx_第9页
第9页 / 共33页
结构体Word格式.docx_第10页
第10页 / 共33页
结构体Word格式.docx_第11页
第11页 / 共33页
结构体Word格式.docx_第12页
第12页 / 共33页
结构体Word格式.docx_第13页
第13页 / 共33页
结构体Word格式.docx_第14页
第14页 / 共33页
结构体Word格式.docx_第15页
第15页 / 共33页
结构体Word格式.docx_第16页
第16页 / 共33页
结构体Word格式.docx_第17页
第17页 / 共33页
结构体Word格式.docx_第18页
第18页 / 共33页
结构体Word格式.docx_第19页
第19页 / 共33页
结构体Word格式.docx_第20页
第20页 / 共33页
亲,该文档总共33页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

结构体Word格式.docx

《结构体Word格式.docx》由会员分享,可在线阅读,更多相关《结构体Word格式.docx(33页珍藏版)》请在冰点文库上搜索。

结构体Word格式.docx

 

例题2:

下列声明结构体变量错误的是()。

A.structstudentB.structstudent

{{

   intno;

charname[16];

charname[16];

}st1,st2;

};

structstudentst1,st2;

structstudent{structstudent{

};

studentst1,st2;

structst1,st2;

D

结构体变量定义有两种方式,一是在定义结构体类型时说明结构体变量,如A,即A是正确的。

二是用结构体类型说明结构体变量。

如B和C,即B和C也是正确。

不能只用结构体关键字去说明结构体变量,即D不正确,只能选择D。

例题3:

设有以下说明,则正确的赋值表达式为(  )。

 structx

{intno;

charname[20];

floatscore;

}y;

A.y.no=10;

B.y->

no=10;

C.x.no=10;

D.x->

n0=10;

A

引用结构体成员是普通变量是:

变量.成员,引用结构体指针变量是:

变量->

成员。

例题4:

若已建立如下链表结构,指针p,q分别指向如下图所示的结点,用以将p所指向结点从链表中删除并释放该结点的正确程序段是(    )。

      

结点结构

A.q->

next=p->

next B.deletep;

q->

next=p->

next;

C.q->

deletep;

D.p=p->

q->

next=p;

A虽然在单链表中,把结点p跳过,但是结点p所占内存空间没有释放。

B是先释放结点p所占内容空间,以下操作均不正确了。

C在单链表中,把结点p跳过,也将结点p所占内存空间释放,所以正确。

D在单链表中,先把p从结点2移到下一个结点3,再将q->

next连到结点3,即跳过结点2,但是删除的是结点3而不是结点2,不正确。

例题5:

在定义一个共用体变量时,系统为其分配存储空间的原则是(  )。

A.A.按所有成员需要的存储空间总和分配。

B.B.按成员中占存储空间最大者分配。

C.C.按成员占存储空间最小者分配。

D.D.按第一个成员所需的存储空间分配。

B

根据共用体定义规则,系统为其分配存储空间是按其成员中占存储空间最大者分配。

例题6:

设有以下定义枚举型,则元素green值是(    )。

enumcolor{red=2,yellow,blue,green};

A.5   B.4    C.3 D.以上答案均不正确

由枚举定义知,enum是自定义枚举类型,它有元素red、yellow、blue和green,由枚举定义知,枚举类型的每个元素,都有一个在定义时按元素位置从左到右默认值,依次是0,1,2,……。

本题中red的起始值是2,所以以后各元素值依次为3,4,5……。

故green的值是5。

例题7:

写出下列程序运行结果:

 #include<

iostream.h>

structpiont

{intx;

 inty;

 } st[5]={1,2,3,4,5,6,7,8,9,10};

voidmain()

{inti;

  piont*p;

  p=st;

  for(i=0;

i<

5;

i++)

{cout<

<

p->

x<

'

\t'

y<

endl;

p++;

 }

}

运行结果是:

1 2

34

56

78

99 

10

st是一个结构体数组,p是结构体指针变量,并取得结构体数组首地址,p->

x和p->

y分别表示结构体两个成员,所以运行结果如上所示。

例题8:

下列函数average功能是计算10名学生3门课成绩的平均分,请填空。

#include<

constintm=3;

constintn=10;

structstud{

floatmark[m];

floatave;

};

voidaverage(studst[])

{inti,j;

floatsum;

for(i=0;

n;

{sum=

(1);

//1

for(j=0;

j<

m;

j++)

sum=

(2);

//2

(3)=sum/m;

//3

{studpreson[n];

.

average(preson);

(1)0.0

(2)sum+st[i].mark[j](3)st[i].ave

结构体数组定义和结构体成员的表示可得到上述结果。

例题9:

函数findbook的功能是:

在有n个元素的数组s中查找名为a的书若找到,函数返回数组下标,否则,函数返回-1,请填空。

structdata

{intid;

doubleprice;

}book[100];

intfindbook(datas[],intn,chara[])

{inti;

if(

(1))returni;

//1

(2);

(1)strcmp(s[i].name,a)==0

(2)return-1

根据结构体定义和字符串比较函数。

例题10:

学生姓名(name)和年龄(age)存于结构体数组person中,函数fun的功能是:

找出年龄最小的那名学生,找出程序错误,并改正。

intage;

fun(studperson[],intn)//1

{intmin;

min=0;

i++)//2

if(person[i]<

person[min])min=i;

return(person[min]);

{studa[]={{"

Zhao"

21},{"

Qian"

20},{"

Sun"

19},{"

LI"

22}};

intn=4;

studminpers;

minpers=fun(a,n);

cout<

"

年龄最小者是:

minpers.name<

"

minpers.age<

(1)studfun(studperson[],intn)

(2)for(inti=0;

(3)person[i].age<

person[min].age)

(1)

(1) 

函数fun的返回值是结构体类型,所示的返回类型也必须是结构体类型,即studfun(studperson[],intn)。

(2)

(2) 

变量i在函数fun中定义,在循环语句中必须说明。

(3)(3) 

因为是找年龄最小的记录,在比较时应该是比较成员年龄。

例题11:

利用第3题定义的结构体类型,编写一个小型的班级同学信息的管理系统。

要求至少设有以下实用功能:

录入学生信息,求某门或各门课程的总分、平均分,按姓名或学号寻找学生的记录并显示,浏览学生信息,按指定的若干门课程或按总分由高到低显示学生信息等。

string.h>

#include<

stdlib.h>

charch[80];

structstud

{intno;

intmath;

inteng;

intc;

intsum;

}st[10];

intn=-1;

voidinputst()//输入学生记录

{intx=1;

cout<

请输入学生学号、姓名、数学、英语、C++成绩,学号为0 结束"

while(x)

{cin>

>

x;

if(x){n++;

st[n].no=x;

cin>

st[n].name>

st[n].math>

st[n].eng>

st[n].c;

st[n].sum=st[n].math+st[n].eng+st[n].c;

elsebreak;

voidshow()     //显示学生记录

chary;

学号"

姓名"

数学"

英语"

;

C++"

总分"

=n;

{cout<

st[i].no<

st[i].name<

st[i].math<

st[i].eng<

st[i].c<

st[i].sum<

按任意键继续"

y;

voidlocat()       //查找

{inti,yn=1,x;

\t\t"

1.按姓名查找\n"

2.按学号查找\n"

请选择(1--2)"

yn;

if(yn==1)

\t\t输入姓名 "

name;

if(strcmp(st[i].name,name)==0)break;

if(i<

=n)

{cout<

elsecout<

没有找到\n"

else

输入查询学号"

if(st[i].no==x)break;

if(i<

elsecout<

voidaverage()

{inti,j=1,k;

inta[3]={0,0,0};

for(k=0;

k<

k++)

{a[0]+=st[k].math;

a[1]+=st[k].eng;

a[2]+=st[k].c;

while(j)

{

\t\t1.数学\n"

\t\t2.英语\n"

\t\t3.C++\n"

\t\t4.各门课\n"

\t\t0.退出\n"

请选择(0--4)"

i;

switch(i)

{case1:

数学总分:

a[0]<

平均分:

a[0]/(n+1)<

break;

case2:

英语总分:

a[1]<

a[1]/(n+1)<

case3:

C++总分:

a[2]<

a[2]/(n+1)<

case4:

case0:

j=0;

}

voidsortmath();

voidsorteng();

voidsortc();

voidsort()//输出

{intx,y=1;

while(y)

\t\t1.数学排序\n"

\t\t2.英语排序\n"

\t\t3.C++排序\n"

请选择(0--3)"

switch(x)

{case1:

sortmath();

case2:

sorteng();

case3:

sortc();

y=0;

voidmain()//主程序

{inti,j=1;

{system("

cls"

);

//清除屏幕

\n"

1.录入学生记录\n"

2.求课程总分和平均分\n"

3.显示学生信息\n"

4.查找学生信息\n"

5.排    序\n"

0.退    出\n"

请选择(0--5)"

switch(i)

inputst();

average();

show();

case4:

locat();

case5:

sort();

break;

default:

数字选择错误,请重新输入。

谢谢光临!

再见!

voidsortmath()

studk;

n-i;

if(st[j].math<

st[j+1].math)

{k=st[j];

st[j]=st[j+1];

st[j+1]=k;

{cout<

st[i].eng<

voidsorteng()

if(st[j].eng<

st[j+1].eng)

voidsortc()

if(st[j].c<

st[j+1].c)

st[i].math;

本题功能要求较多,程序比较长,采用模块求解,根据题目要求分解为若干个模块,即学生信息录入模块、显示模块、每门总分和平均分模块、查找模块、按成绩排序模块。

为了方便各个模块之间传递数据,定义了一个全局变量的记录数组,程序的运行采用菜单方式,方便操作。

7.3教材习题分析与解答

1.选择题

(1)下列关于结构体的说法错误的是()。

A.结构体是由用户自定义的一种数据类型

B.结构体中可设定若干个不同数据类型的成员

C.结构体中成员的数据类型可以是另一个已定义的结构体

D.在定义结构体时,可以为成员设置默认值

结构体是C++用户自定义的一种构造数据类型,它可以有若干个不同类型的成员,也可以有相同类型成员,在这些成员中可以前所有定义的数据类型,但是在定义结构类型时,成员函数不能设定默认值,所以A、B、C说法无正确,不能选择,只有D的说法是错误的。

(2)下列结构体定义,正确的是()。

recordstructrecord

{intno;

{intno;

structrecordstructrecord

{intno

见本章例1

(3)下列声明结构体变量错误的是()。

{intno;

structstudentstructstudent

见本章例2

(4)若有同下定义,下列说法错误的是()。

structem{

  chara;

  charb;

A.struct是结构体类型关键字B.em是结构体类型名

 C.em是用户声明的结构体变量D.a,b是结构体成员名

由结构体类型定义可知,em是定义的结构体类型名,所以C是错误的,其它选项都是正确的,所以选择C。

(5)若有下列定义,则对结构体变量的成员引用错误的是()。

structdate{intyear;

intmonth;

intday;

structstudent{

datebirthday;

}stud;

A.stud.noB.stud.name

 C.stud.birthdayD.stud.birthday.year

结构体成员表示是结构体变量名.成员名,所以stut.no、stud.name是正确的,A和B不能选择。

C中成员birthday类型还是结构体类型,故stud.birthday不正确,应该是stud.birthday.year或 stud.birthday.month 或 stud.birthday.day。

所以选择C。

(6)若有下列定义,则错误的赋值语句是()。

enumcolor{red,yellow,blue,green}col;

intk;

A.col=redB.col=3

C.k=red+2D.k=red+blue

由枚举定义知,enum是自定义枚举类型,它有元素red、yellow、blue和green,col是枚举类型的一个变量,它的值应该是枚举元素之一,不能够取整形数,所以A不

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

当前位置:首页 > 解决方案 > 学习计划

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

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