第九章课后习题答案.docx
《第九章课后习题答案.docx》由会员分享,可在线阅读,更多相关《第九章课后习题答案.docx(17页珍藏版)》请在冰点文库上搜索。
第九章课后习题答案
9.1
结构体可以把不同类型的数据组合成一个整体。
与数组相比,数组中的元素是按照下标来进行访问的,而结构体是按照用户定义的结构体变量名来访问其成员的。
9.2
因为在定义结构体时系统并没有为其中的成员分配任何存储空间,而只有定义了结构体变量后,系统才为其中的各成员分配了相应的空间,所以先定义结构体变量,然后才能为结构体变量的成员进行赋值操作,否则,没有任何意义。
9.3
#include
#defineN10
typedefstruct{
charnum[N];//学号
charname[N];//姓名
intage;//年龄
intscore;//成绩,若有多门成绩,则可将该变量定义成数组形式
}ST;
voidinput_fun(ST*);//输入函数
voidoutput_fun(ST*);//输出函数
voidmain()
{
charchoice='Y';
STstu;
while(choice=='Y'||choice=='y')//是否继续输入
{
input_fun(&stu);
getchar();
output_fun(&stu);
printf("Continue?
");
choice=getchar();
}
}
voidinput_fun(ST*p)
{
printf("Thenum:
");
scanf("%s",p->num);
printf("Thename:
");
scanf("%s",p->name);
printf("Theage:
");
scanf("%d",&p->age);
printf("Thescore:
");
scanf("%d",&p->score);
}
voidoutput_fun(ST*q)
{
printf("Thenum:
");
puts((*q).num);
printf("Thename:
");
puts((*q).name);
printf("Theage:
%d\n",(*q).age);
printf("Thescore:
%d\n",(*q).score);
}
9.4
#include
#defineN10
structsdata{
intyear;
intmonth;
intday;
};
typedefstruct{
charnum[N];//学号
charname[N];//姓名
structsdatabirthday;//生日
intscore;//成绩,若有多门成绩,则可将该变量定义成数组形式
}ST;
voidinput_fun(ST*);//输入函数
voidoutput_fun(ST*);//输出函数
voidmain()
{
charchoice='Y';
STstu;
while(choice=='Y'||choice=='y')//是否继续输入
{
input_fun(&stu);
getchar();
output_fun(&stu);
printf("Continue?
");
choice=getchar();
}
}
voidinput_fun(ST*p)
{
printf("Thenum:
");
scanf("%s",p->num);
printf("Thename:
");
scanf("%s",p->name);
printf("Thebirthday:
\n");
printf("theyear:
");
scanf("%d",&p->birthday.year);
printf("themonth:
");
scanf("%d",&p->birthday.month);
printf("theday:
");
scanf("%d",&p->birthday.day);
printf("Thescore:
");
scanf("%d",&p->score);
}
voidoutput_fun(ST*q)
{
printf("Thenum:
");
puts((*q).num);
printf("Thename:
");
puts((*q).name);
printf("Thedata:
%d.%d.%d\n",(*q).data.year,(*q).data.month,(*q).data.day);
printf("Thescore:
%d\n",(*q).score);
}
9.5
#include
#defineN5
#defineM10
typedefstruct{
charnum[M];
charname[M];
intprice;
intamount;
}GOODS;
voidinitialize(GOODS*);
voidpriarr(GOODS*);
voidpriprice(GOODS*);
voidmain()
{
GOODSgoods[N];
initialize(goods);
priarr(goods);
priprice(goods);
}
voidinitialize(GOODSgoods[])
{
inti;
for(i=0;i{
printf("pleaseinputthegoods'num:
");
scanf("%s",goods[i].num);
printf("pleaseinputthegoods'name:
");
scanf("%s",goods[i].name);
printf("pleaseinputthegoods'price:
");
scanf("%d",&goods[i].price);
printf("pleaseinputthegoods'amount:
");
scanf("%d",&goods[i].amount);
}
}
voidpriarr(GOODSgoods[])
{
inti;
printf("Thelistis:
\n");
printf("numnamepriceamount\n");
for(i=0;iprintf("%4s%8s%8d%8d\n",goods[i].num,goods[i].name,goods[i].price,goods[i].amount);
}
voidpriprice(GOODSgoods[])
{
intsum=0;
intsumi=1;
inti,j;
for(i=0;i{
sumi=1;
sumi=goods[i].amount*goods[i].price;
sum=sum+sumi;
}
printf("Theallgrossis:
%d\n",sum);
}
9.6
#include
#defineN10
#defineM3
typedefstruct{
charnum[N];//学号
charname[N];//姓名
intage;//年龄
intscore;//成绩,若有多门成绩,则可将该变量定义成数组形式
}ST;
voidinput_fun(ST*);//输入函数
voidoutput_fun(ST*);//输出函数
voidorder(ST*);
intaverage(ST*);
voidmain()
{
charchoice='Y';
STstu[M];
inti;
intave;
for(i=0;iinput_fun(&stu[i]);
getchar();
printf("TheInitialdata:
\n");
output_fun(stu);
order(stu);
printf("Theorderdata:
\n");
output_fun(stu);
putchar('\n');
ave=average(stu);
printf("Theaveragescore:
%d\n",ave);
}
voidinput_fun(ST*p)
{
printf("Thenum:
");
scanf("%s",p->num);
printf("Thename:
");
scanf("%s",p->name);
printf("Theage:
");
scanf("%d",&p->age);
printf("Thescore:
");
scanf("%d",&p->score);
}
voidoutput_fun(ST*q)
{
printf("\nnumnameagescore\n");
for(inti=0;iprintf("%7s%8s%8d%8d\n",q[i].num,q[i].name,q[i].age,q[i].score);
}
voidorder(STt[])
{
STtemp;
inti;
for(i=0;i{
for(intj=i+1;j{
if(t[i].score{
temp=t[i];
t[i]=t[j];
t[j]=temp;
}
}
}
}
intaverage(STt[])
{
inti;
intsum=0;
intave;
for(i=0;isum+=t[i].score;
ave=sum/M;
returnave;
}
9.7
#include
#include
#defineN10
structstudent{
charnum[N];
intscore;
structstudent*next;
};
voidreadlist(structstudent*);
voidmain()
{
structstudent*head,*q,*m;
charchoice='Y';
if(m=(structstudent*)malloc(sizeof(structstudent)))
m->next=NULL;
head=m;
while(choice=='Y'||choice=='y')
{
if(q=(structstudent*)malloc(sizeof(structstudent)))
{
printf("Inputthenum:
");
scanf("%s",q->num);
printf("Inputthescore:
");
scanf("%d",&q->score);
q->next=NULL;
getchar();
m->next=q;
m=m->next;
}
printf("Continue?
");
choice=getchar();
}
readlist(head);
}
voidreadlist(structstudent*stu)
{
structstudent*p;
p=stu->next;
do
{
printf("thenum:
");
printf("%s\n",p->num);
printf("thescore:
");
printf("%d\n",p->score);
p=p->next;
}while(p!
=NULL);
}
9.8
#include
#include
#defineN10
structstudent{
charnum[N];
intscore;
structstudent*next;
};
voidreadlist(structstudent*);
voidmain()
{
structstudent*head,*q,*m,*k,*n;
charchoice='Y';
if(m=(structstudent*)malloc(sizeof(structstudent)))
m->next=NULL;
head=m;
while(choice=='Y'||choice=='y')
{
if(q=(structstudent*)malloc(sizeof(structstudent)))
{
printf("Inputthenum:
");
scanf("%s",q->num);
printf("Inputthescore:
");
scanf("%d",&q->score);
q->next=NULL;
getchar();
}
if(head->next==NULL)
{
m->next=q;
m=m->next;
}
elseif(m->score>=q->score)
{
m->next=q;
m=m->next;
}
else
{
n=head;
k=n->next;
while(k->next!
=NULL&&k->score>q->score)
{
n=k;
k=k->next;
}
q->next=k;
n->next=q;
}
printf("Continue?
");
choice=getchar();
}
readlist(head);
}
voidreadlist(structstudent*stu)
{
structstudent*p;
p=stu->next;
printf("numscore\n");
do
{
printf("%5s%5d\n",p->num,p->score);
p=p->next;
}while(p!
=NULL);
}
9.9
#include
#include
typedefstruct{
intyear;
intmonth;
intday;
}DATA;
typedefstructdocument{
charnum[10];
DATAdata;
structdocument*next;
}DOC;
voidinqueue(DOC*,DOC*,DOC*);
voidoutqueue(DOC*,DOC*);
voidmain()
{
charchoice='Y';
charadd='Y';
chardel;
DOC*front,*rear,*p;
front=(DOC*)malloc(sizeof(DOC));
front->next=NULL;
rear=front;
p=rear;
while(add=='Y'||add=='y')
{
inqueue(front,rear,p);
getchar();
printf("Continueadddocument?
");
add=getchar();
p=(DOC*)malloc(sizeof(DOC));
}
getchar();
printf("dealwithdocument?
");
del=getchar();
while(del=='Y'||del=='y')
{
outqueue(front,rear);
front=front->next;
getchar();
printf("Continuedealwithdocument?
");
del=getchar();
if(del=='N'||del=='n')
break;
}
}
voidinqueue(DOC*front,DOC*rear,DOC*q)
{
printf("enterthenum:
");
scanf("%s",q->num);
printf("entertheyear:
");
scanf("%d",&q->data.year);
printf("enterthemonth:
");
scanf("%d",&q->data.month);
printf("entertheday:
");
scanf("%d",&q->data.day);
rear->next=q;
rear=q;
rear->next=NULL;
}
}
voidoutqueue(DOC*front,DOC*rear)
{
DOC*p;
if(front==NULL&&rear==NULL)
printf("It'saemptyqueue!
\n");
else
{
p=front;
printf("Thedealwithdocument:
\n");
printf("thenum:
%5sthedate:
%4d.%d.%d\n",p->num,p->data.year,p->data.month,p->data.day);
}
}