结构体习题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

结构体习题
49、以下程序运行结果是______。

#include <string.h>
typedefstruct student{
char name[10];
longsno;
float score;
}STU;
main()
{ STU
a={"Zhangsan",2001,95},b={"Shangxian",2002,90},
c={"Anhua",2003,95},d,*p=&d;
d=a;
if(strcmp(,)>0) d=b;
if(strcmp(,)>0) d=c;
printf("%ld %s\n",d.sno,p->name);
}
标准答案为:2002 Shangxian
1. 填空题
人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。

函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若指定编号不存在,返回数据中的编号为空串。

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

注意:源程序存放在考生文件夹下的BLANK1.C中。

不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#include <string.h>
#define N 8
typedefstruct
{ charnum[10];
intyear,month,day ;
}STU;
/**********found**********/
STU fun(STU *std, char *num)
{ int i; STU a={"",9999,99,99};
for (i=0; i<N; i++)
/**********found**********/
if(strcmp(std[i].num,num)==0 )
/**********found**********/
return (std[i]);
return a;
}
main()
{ STU std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1},
{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},
{"777777",1983,6,22},{"888888",1984,8,19}};
“”
STU p;
char n[10]="666666";
p=fun(std,n);
if(p.num[0]==0)
printf("\nNot found !\n");
else
{ printf("\nSucceed !\n ");
printf("%s %d-%d-%d\n",p.num,p.year,p.month,p.day);
}
}
第1 处:STU fun(STU *std, char *num)
第2 处:if(strcmp(std[i].num,num)==0 )
第3 处:return (std[i]);
2. 填空题
人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中。

函数fun 的功能是:找出指定出生年份的人员,将其数据放在形参k所指的数组中,由主函数输出,同时由函数值返回满足指定条件的人数。

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结
果。

注意:源程序存放在考生文件夹下的BLANK1.C中。

不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#define N 8
typedefstruct
{ intnum;
intyear,month,day ;
}STU;
int fun(STU *std, STU *k, int year)
{ int i,n=0;
for (i=0; i<N; i++)
/**********found**********/
if(___1___)
/**********found**********/
k[___2___]= std[i];
/**********found**********/
return (___3___);
}
main()
{ STU std[N]={ {1,1984,2,15},{2,1983,9,21},{3,1984,9,1},{4,1983,7,15},
{5,1985,9,28},{6,1982,11,15},{7,1982,6,22},{8,1984,8,19}};
STU k[N]; inti,n,year;
printf("Enter a year : "); scanf("%d",&year);
n=fun(std,k,year);
if(n==0)
printf("\nNo person was born in %d \n",year);
else
{ printf("\nThese persons were born in %d \n",year);
for(i=0; i<n; i++)
printf("%d %d-%d-%d\n",k[i].num,k[i].year,k[i].month,k[i].day);
}
}
第1 处:if(std[i].year==year)
第2 处:k[n++]= std[i];
第3 处:return (n);
3. 程序设计
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把指定分数范围内的学生数据放在h所指的数组中,分数范围内的学生人数由函数值返回。

例如,输入的分数是60 69,则应当把分数在60到69的学生数据进行输出,包含60分和69分的学生数据。

主函数中将把60放在low中,把69放在heigh中。

注意:部分源程序存在文件prog.c中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
#define N 16
typedefstruct
{ charnum[10];
int s;
} STREC;
int fun(STREC *a,STREC *b,int l,int h)
{ int i,j=0,k,f=0;
for (i=0;i<N;i++)
if ((a[i].s>=l) && (a[i].s<=h))
{for (k=0;k<10;k++)
b[j].num[k]=a[i].num[k];
b[j].s=a[i].s;
j++;
f++;
}
return(f);
}
main()
{ STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},{"GA01",91},{"GA07",72},{"GA 08",64},{"GA06",87},{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},{"GA011",91 },{"GA017",64},{"GA018",64},{"GA016",72}};
STREC h[N];FILE *out;
inti,n,low,heigh,t;
printf("Enter 2 interger number low &heigh:");
scanf("%d%d",&low,&heigh);
if (heigh<low) {t=heigh;heigh=low;low=t;}
n=fun(s,h,low,heigh);
printf("The student's data between %d--%d:\n",low,heigh);
for(i=0;i<n;i++)
printf("%s %4d\n",h[i].num,h[i].s);
printf("\n");
out=fopen("out18.dat","w");
n=fun(s,h,80,98);
fprintf(out,"%d\n",n);for(i=0;i<n;i++)
fprintf(out,"%s %4d\n",h[i].num,h[i].s);
fclose(out);
}。

相关文档
最新文档