新概念C语言能力教程练习10答案.docx
- 文档编号:27814100
- 上传时间:2023-07-05
- 格式:DOCX
- 页数:24
- 大小:20.36KB
新概念C语言能力教程练习10答案.docx
《新概念C语言能力教程练习10答案.docx》由会员分享,可在线阅读,更多相关《新概念C语言能力教程练习10答案.docx(24页珍藏版)》请在冰豆网上搜索。
新概念C语言能力教程练习10答案
10.1答案:
结构型是一种数据类型,与int,float类似,用于规定相关存储单元的类型,但结构型是用户自己定义的数据类型。
结构型变量与整型变量类似是相关存储单元的标识。
结构型用于定义结构型变量。
10.2答案:
结构型中成员的类型可以是其它结构型。
测试程序代码:
#include
#include
structdate
{
intyear,month,day;
};
structstudent
{
intno;
charname[10];
structdatebirthday;
floatfm,fe;
};
intmain()
{
structstudentstu1;
stu1.no=8;
strcpy(stu1.name,"张三");
stu1.fm=stu1.fe=90.0;
stu1.birthday.day=1;stu1.birthday.month=1;stu1.birthday.year=2012;
printf("学号:
%d\n",stu1.no);
printf("姓名:
%s\n",stu1.name);
printf("生日:
%d年%d月%d日\n",stu1.birthday.year,stu1.birthday.month,stu1.birthday.day);
printf("数学成绩:
%.1f\n",stu1.fm);
printf("英语成绩:
%.1f\n",stu1.fe);
return0;
}
10.3答案:
#include
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
};
intmain()
{
structstudentstu1,stu2;
printf("请输入两个学生的学号,姓名,数学和英语成绩\n");
scanf("%d%s%f%f",&stu1.no,&stu1.name,&stu1.fm,&stu1.fe);
scanf("%d%s%f%f",&stu2.no,&stu2.name,&stu2.fm,&stu2.fe);
if(strcmp(stu1.name,stu2.name)<0)
{
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",stu1.no,stu1.name,stu1.fm,stu1.fe);
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",stu2.no,stu2.name,stu2.fm,stu2.fe);
}
else
{
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",stu2.no,stu2.name,stu2.fm,stu2.fe);
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",stu1.no,stu1.name,stu1.fm,stu1.fe);
}
return0;
}
10.4答案:
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
};
intmain()
{
structstudentstu[5],temp;
inti=0,j=0;
printf("请输入5个学生的学号,姓名,数学和英语成绩\n");
for(i=0;i<5;i++)
scanf("%d%s%f%f",&stu[i].no,&stu[i].name,&stu[i].fm,&stu[i].fe);
for(i=0;i<5;++i)
{
for(j=i+1;j<5;++j)
{
if(stu[i].fm>stu[j].fm)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
printf("按照数学成绩升序排列:
\n");
for(i=0;i<5;++i)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",stu[i].no,stu[i].name,stu[i].fm,stu[i].fe);
return0;
}
10.5答案:
如有structstudentstu1,stu2;,赋值时stu1=stu2可转换成函数调用assign(&stu1,&stu2,sizeof(structstudent))。
函数assign定义如下。
voidassign(void*x,void*y,unsignedsize)
{
char*px,*py;
px=(char*)x;
py=(char*)y;
if(px!
=py)
while(size--)
*px++=*py++;
}
10.6答案:
通过指针变量以间接方式使用指向的结构型存储单元的成员变量时,使用指向成员操作符->更简便。
通过变量直接使用结构型存储单元的成员变量时使用成员操作符。
10.7答案:
例10-2中construct()函数将两个double型小数“组合”成一个结构型的复数变量。
#include
structdate
{
intyear,month,day;
};
structdateconstruct(intyear,intmonth,intday)
{
structdatetemp;
//仅进行了简单的检测
if(year>0&&month>0&&month<=12&&day>0&&day<31)
{
temp.day=day;
temp.month=month;
temp.year=year;
}
else
{
temp.day=temp.month=temp.year=1;
}
returntemp;
}
intmain()
{
intyear,month,day;
structdatedate1;
printf("请输入年,月,日\n");
scanf("%d%d%d",&year,&month,&day);
date1=construct(year,month,day);
printf("%d年%d月%d日\n",date1.year,date1.month,date1.day);
return0;
}
10.8答案:
//函数中没有必要检测参数的合法性。
structdateaddDay(structdated)
{
structdated2;
intflag=0;
d2=d;
++d2.day;
switch(d2.month)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
if(d2.day>31)
{
flag=1;
break;
}
case2:
if(d2.year%4==0&&d2.year%100!
=0||d2.year%400==0)
{
if(d2.day>29)
{
flag=1;
break;
}
}
elseif(d2.day>28)
{
flag=1;
break;
}
default:
if(d2.day>30)
flag=1;
}
if(flag==1)
{
d2.day=1;
++d2.month;
if(d2.month==13)
{
d2.month=1;
++d2.year;
}
}
returnd2;
}
10.9答案:
测试程序如下:
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
};
intmain()
{
structstudentstu1;
printf("%d\n",sizeof(stu1));
return0;
}
程序的输出
结构型中成员分别占4个、10个、4个、4个,原本为22字节,但由输出结果可知,实际上占了24个字节。
结构型变量所占存储单元空间之和有时并不等于各成员变量所占存储单元之和。
在缺省情况下,C编译器为每一个变量或是数据单元按其自然对界条件分配空间。
在结构中,编译器为结构的每个成员按其自然对界(alignment)条件分配空间。
各个成员按照它们被声明的顺序在内存中顺序存储(成员之间可能有插入的空字节),第一个成员的地址和整个结构的地址相同。
C编译器缺省的结构成员自然对界条件为“N字节对齐”,N即该成员数据类型的长度。
如int型成员的自然对界条件为4字节对齐,而double类型的结构成员的自然对界条件为8字节对齐。
若该成员的起始偏移不位于该成员的“默认自然对界条件”上,则在前一个节面后面添加适当个数的空字节。
C编译器缺省的结构整体的自然对界条件为:
该结构所有成员中要求的最大自然对界条件。
若结构体各成员长度之和不为“结构整体自然对界条件的整数倍,则在最后一个成员后填充空字节
10.10答案:
代码如下:
#include
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
};
intstudentcmp(constvoid*p1,constvoid*p2)
{
return((structstudent*)p1)->fm-((structstudent*)p2)->fm;
}
intmain()
{
structstudentstu[5];
inti=0,j=0;
printf("请输入5个学生的学号,姓名,数学和英语成绩\n");
for(i=0;i<5;++i)
scanf("%d%s%f%f",&stu[i].no,&stu[i].name,&stu[i].fm,&stu[i].fe);
qsort(stu,5,sizeof(structstudent),studentcmp);
printf("按照数学成绩升序排列后:
\n");
for(i=0;i<5;++i)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",stu[i].no,stu[i].name,stu[i].fm,stu[i].fe);
return0;
}
10.11答案:
#include
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
structstudent*next;
};
intmain()
{
structstudent*phead,*ptemp,*stu;
inti,n;
phead=stu=NULL;
printf("请输入学生人数:
\n");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
ptemp=(structstudent*)malloc(sizeof(structstudent));
if(ptemp==NULL)
{
printf("内存分配失败!
\n");
return1;
}
printf("请输入第%d个学生的信息:
\n",i);
scanf("%d%s%f%f",&ptemp->no,ptemp->name,&ptemp->fm,&ptemp->fe);
if(i==1)
phead=stu=ptemp;
else
{
stu->next=ptemp;
stu=stu->next;
}
}
if(stu!
=NULL //防止一个学生也没有
stu->next=NULL;
printf("\n\n");
for(ptemp=phead;ptemp!
=NULL;ptemp=ptemp->next)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩:
%.1f\n",ptemp->no,ptemp->name,ptemp->fm,ptemp->fe);
return0;
}
10.12答案:
#include
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
structstudent*next;
};
//假设头结点head为链表中第0个结点。
intinsert(structstudent*phead,intn,structstudent*pnew)
{
inti=0;
structstudent*p1,*p2;
if(pnew==NULL)
return2;
for(p1=phead;p1!
=NULL&&i
++i;
if(p1==NULL)
return1;
p2=p1->next;
p1->next=pnew;
pnew->next=p2;
return0;
}
intmain()
{
structstudenthead,*ptail=&head,*ptemp;
inti,n;
printf("请输入学生人数:
\n");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
ptemp=(structstudent*)malloc(sizeof(structstudent));
if(ptemp==NULL)
{
printf("内存分配失败!
\n");
return-1;
}
printf("请输入第%d个学生的信息:
\n",i);
scanf("%d%s%f%f",&ptemp->no,ptemp->name,&ptemp->fm,&ptemp->fe);
ptail->next=ptemp;
ptail=ptail->next;
}
ptail->next=NULL;
ptemp=(structstudent*)malloc(sizeof(structstudent));
if(ptemp==NULL)
{
printf("内存分配失败!
\n");
return-1;
}
printf("请输入新加学生的位置:
\n");
scanf("%d",&i);
printf("请输入新加学生的信息:
\n");
scanf("%d%s%f%f",&ptemp->no,ptemp->name,&ptemp->fm,&ptemp->fe);
printf("%d\n",insert(&head,i,ptemp));
for(ptemp=head.next;ptemp!
=NULL;ptemp=ptemp->next)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩%.1f\n",ptemp->no,ptemp->name,ptemp->fm,ptemp->fe);
return0;
}
10.13答案:
intdel(structstudent*phead,intn)
{
inti=0;
structstudent*p1,*p2;
for(p1=phead;p1!
=NULL&&i!
=n-1;p1=p1->next)
++i;
if(p1==NULL||n<=0)
return1;
p2=p1->next;
if(p2!
=NULL)
{
p1->next=p2->next;
deletep2;
return0;
}
return2;
}
10.14答案:
#include
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
structstudent*next;
};
intmain()
{
structstudenthead,*ptail=&head,*ptemp;
inti,n;
printf("请输入学生人数:
\n");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
ptemp=(structstudent*)malloc(sizeof(structstudent));
if(ptemp==NULL)
{
printf("内存分配失败!
\n");
return-1;
}
printf("请输入第%d个学生的信息:
\n",i);
scanf("%d%s%f%f",&ptemp->no,ptemp->name,&ptemp->fm,&ptemp->fe);
ptail->next=ptemp;
ptail=ptail->next;
}
ptail->next=NULL;
for(ptemp=head.next;ptemp!
=NULL;ptemp=ptemp->next)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩%.1f\n",ptemp->no,ptemp->name,ptemp->fm,ptemp->fe);
ptail=head.next;
ptemp=ptail->next;
ptail->next=NULL;
while(ptemp!
=NULL)
{
head.next=ptemp;
ptemp=ptemp->next;
head.next->next=ptail;
ptail=head.next;
}
printf("转置后的链表:
\n");
for(ptemp=head.next;ptemp!
=NULL;ptemp=ptemp->next)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩%.1f\n",ptemp->no,ptemp->name,ptemp->fm,ptemp->fe);
return0;
}
10.15答案:
#include
#include
structstudent
{
intno;
charname[10];
floatfm,fe;
structstudent*next;
};
intmain()
{
structstudenthead,*ptail=&head,*ptemp,*p1,*p2;
inti,n;
printf("请输入学生人数:
\n");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
ptemp=(structstudent*)malloc(sizeof(structstudent));
if(ptemp==NULL)
{
printf("内存分配失败!
\n");
return-1;
}
printf("请输入第%d个学生的信息:
\n",i);
scanf("%d%s%f%f",&ptemp->no,ptemp->name,&ptemp->fm,&ptemp->fe);
ptail->next=ptemp;
ptail=ptail->next;
}
ptail->next=NULL;
for(ptemp=head.next;ptemp!
=NULL;ptemp=ptemp->next)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩%.1f\n",ptemp->no,ptemp->name,ptemp->fm,ptemp->fe);
ptail=head.next->next;
head.next->next=NULL;
while(ptail!
=NULL)
{
p1=ptail;
ptail=ptail->next;
for(p2=&head;p2->next!
=NULL&&(p2->next)->fm
;
if(p2->next==NULL)
{
p1->next=NULL;
p2->next=p1;
}
else
{
p1->next=p2->next;
p2->next=p1;
}
}
printf("按数学成绩排序后的学生信息如下:
\n");
for(ptemp=head.next;ptemp!
=NULL;ptemp=ptemp->next)
printf("学号:
%d姓名:
%s数学成绩:
%.1f英语成绩%.1f\n",ptemp->no,ptemp->name,ptemp->fm,ptemp->fe);
return0;
}
10.16答案:
函数construct(intn)是例10-4中construct函数的另一种形式,它通过函数的返回值给出了循环链表的一个结点的地址。
10.17答案:
函数play同样模拟了例10-4中的报数过程。
10.18答
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 新概念 语言 能力 教程 练习 10 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)