中国石油大学华东C语言在线测评答案 第12章结构体和共用体级亲测正确.docx
《中国石油大学华东C语言在线测评答案 第12章结构体和共用体级亲测正确.docx》由会员分享,可在线阅读,更多相关《中国石油大学华东C语言在线测评答案 第12章结构体和共用体级亲测正确.docx(15页珍藏版)》请在冰点文库上搜索。
中国石油大学华东C语言在线测评答案第12章结构体和共用体级亲测正确
12.1修改学生的各科成绩
给定程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和3门课的成绩。
函数fun的功能是将该学生的各科成绩都乘以一个系数a。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:
只需提交补充完整的fun函数和main函数
预设代码
/**********found**********/
voidfun(___1___*ss,floata)
{
inti;
for(i=0;i<3;i++)
/**********found**********/
ss->___2___*=a;
}
intmain( )
{
STUstd={1,"Zhanghua",76.5,78.0,82.0};
floata;
scanf("%f",&a);
/**********found**********/
fun(___3___,a);
show(std);
return0;
}
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
#include
#include
#include
typedefstruct
{
intnum;
charname[9];
floatscore[3];
}STU;
voidshow(STUtt)
{
inti;
printf("%d%s:
",tt.num,tt.name);
for(i=0;i<3;i++)
printf("%5.1f",tt.score[i]);
printf("\n");
}
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
/**********found**********/
voidfun(STU*ss,floata)
{
inti;
for(i=0;i<3;i++)
/**********found**********/
ss->score[i]*=a;
}
intmain()
{
STUstd={1,"Zhanghua",76.5,78.0,82.0};
floata;
scanf("%f",&a);
/**********found**********/
fun(&std,a);
show(std);
return0;
}
12.2修改学生信息
程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。
函数fun的功能是对形参b所指结构体变量中的数据进行修改,最后在主函数中输出修改后的数据。
例如:
b所指变量t中的学号、姓名、和三门课的成绩依次是:
*****、"ZhangQi"、93、85、87,修改后输出t中的数据应为:
*****、"LiJie"、93、85、87。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
不得增行或删行,也不得更改程序的结构!
注意:
只需提交补充完整的fun函数和main主函数
预设代码
voidfun(structstudent *b)
{
/**********found**********/
b__1__=10004;
/**********found**********/
strcpy(b__2__,"LiJie");
}
intmain()
{
structstudent t={10002,"ZhangQi",{93,85,87}};
int i;
/**********found**********/
fun(__3__);
printf("No:
%ld Name:
%s\nScores:
",t.sno,t.name);
for(i=0;i<3;i++) printf("%6.2f",t.score[i]);
printf("\n");
return0;
}
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
#include
#include
structstudent
{
longsno;
charname[10];
floatscore[3];
};
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
voidfun(structstudent*b)
{
/**********found**********/
b->sno=10004;
/**********found**********/
strcpy(b->name,"LiJie");
}
intmain()
{
structstudentt={10002,"ZhangQi",{93,85,87}};
inti;
/**********found**********/
fun(&t);
printf("No:
%ldName:
%s\nScores:
",t.sno,t.name);
for(i=0;i<3;i++)printf("%6.2f",t.score[i]);
printf("\n");
return0;
}
12.3学生信息的排序
程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩。
函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:
只需提交补充完整的fun函数。
预设代码
voidfun(structstudent a[],int n)
{
/**********found**********/
__1__t;
int i,j;
/**********found**********/
for(i=0;i<__2__;i++)
for(j=i+1;j/**********found**********/
if(strcmp(__3__)>0)
{ t=a[i]; a[i]=a[j]; a[j]=t; }
}
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
#include
#include
structstudent
{
longsno;
charname[10];
floatscore[3];
};
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
voidfun(structstudenta[],intn)
{
/**********found**********/
structstudentt;
inti,j;
/**********found**********/
for(i=0;ifor(j=i+1;j/**********found**********/
if(strcmp(a[i].name,a[j].name)>0)
{t=a[i];a[i]=a[j];a[j]=t;}
}
12.4找出指定分数范围内的学生
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:
把指定分数范围内的学生数据放在h所指的数组中,分数范围内的学生人数由函数值返回。
例如,输入的分数是6069,则应当把分数在60到69的学生数据进行输出,包含60分和69分的学生数据。
主函数中将把60放在low中,把69放在heigh中。
注意:
只需提交fun函数
STRECs[N]={{"GA005",85},{"GA003",76},{"GA002",69},{"GA004",85},{"GA001",96},{"GA007",72},{"GA008",64},{"GA006",87},{"GA015",85},{"GA013",94},{"GA012",64},{"GA014",91},{"GA011",90},{"GA017",64},{"GA018",64},{"GA016",72}};
12.5找出分数最低的学生
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:
把分数最低的学生数据放在b所指的数组中,注意:
分数最低的学生可能不止一个,函数返回分数最低的学生的人数。
注意:
只需提交fun函数
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
#include
#defineN16
typedefstruct
{
charnum[10];
ints;
}STREC;
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
intfun(STRECs[],STRECh[])
{
intj,min,count=0;
min=s[0].s;
for(j=1;j{
if(s[j].s{
min=s[j].s;
}
}
for(j=0;j{
if(s[j].s==min)
{
h[count]=s[j];
count++;
}
}
returncount;
}
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
intmain()
{
STRECs[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},{"GA011",91},{"GA017",64},{"GA018",64},{"GA016",72}};
STRECh[N];
inti,n;
n=fun(s,h);
for(i=0;iprintf("%s%4d\n",h[i].num,h[i].s);
return0;
}
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
12.6求平面上两点距离
编写程序,计算二维平面上两点p1和p2间的直线距离。
输入:
两点的坐标值输出:
两点的距离注意:
只需提交fun函数
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
#include
#include"math.h"
typedefstruct
{
doublex,y;
}POINT2D;
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
doublefun(POINT2Dp1,POINT2Dp2)
{
doublem=0;
m=(p2.x-p1.x)*(p2.x-p1.x);
m+=(p2.y-p1.y)*(p2.y-p1.y);
m=(double)(sqrt(m));
returnm;
}
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
intmain()
{
POINT2Dp1,p2;
doublee;
scanf("%lf%lf",&p1.x,&p1.y);
scanf("%lf%lf",&p2.x,&p2.y);
e=fun(p1,p2);
printf("%lf\n",e);
return0;
}
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
12.7加密
编写程序,按下表所示的码表将明文中指定的明码转换为暗码(其他字符不变),形成密文并输出。
输入:
明文输出:
与输入相对应的密文注意:
只需提交fun函数
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
#include
#defineN80
typedefstruct
{
charreal;/*明码*/
charcode;/*密码*/
}ENCODE;
/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/
voidfun(chars[],chart[],ENCODEtab[])
{
inti=0,j,find;
while(s[i]!
='\0')
{
find=0;
for(j=0;j<4;j++)
{
if(s[i]==tab[j].real)
{
t[i]=tab[j].code;
find=1;
break;
}
}
if(find==0)t[i]=s[i];
i++;
}
t[i]='\0';
}
/*PRESETCODEBEGIN-NEVERTOUCHCODEBELOW*/
intmain()
{
chars[N],t[N];
ENCODEtab[]={{'a','d'},{'b','z'},{'z','a'},{'d','b'},{'\0'}};/*定义码表,供转换时查阅*/
gets(s);
fun(s,t,tab);
printf("%s\n",t);
return0;
}/*PRESETCODEEND-NEVERTOUCHCODEABOVE*/