2016广工Anyview试题答案 第九章
时间:2026-01-16
时间:2026-01-16
2016广工Anyview试题答案 第九章
/**********
【习题9.023】结构体类型定义如下:
struct date{int year; int month; int day;}; //定义日期结构体类型
struct student
{ char name[20];
struct date birth; //出生日期
};
结构体数组s存储了n个人的名字和出生日期。写一函数,求这n个人中年龄最大
(即出生日期最小)者的姓名。
**********/
char *oldest(student s[], int n)
{
int j,k=0;
for(j=1;j<n-1;j++)
{if(s[k].birth.year>s[j].birth.year) k=j;
else if(s[k].birth.year==s[j].birth.year)
{if(s[k].birth.month>s[j].birth.month) k=j;
else if(s[k].birth.month==s[j].birth.month)
if(s[k].birth.day>s[j].birth.day) k=j;}
}
return s[k].name;
}
/**********
【习题9.025】结构体类型定义如下:
struct date{int year; int month; int day;}; //定义日期结构体类型
struct student
{ char id[10]; //学号
char name[20]; //姓名
struct date birth; //出生日期
};
结构体数组s存储了n个人的学号、名字和出生日期。写一函数,以结构体的形式
返回这n个人中年龄最大(即出生日期最小)者的信息。
**********/
struct student oldest(struct student s[], int n)
{
int j,k=0;
for(j=1;j<n-1;j++)
{if(s[k].birth.year>s[j].birth.year) k=j;
else if(s[k].birth.year==s[j].birth.year)
{if(s[k].birth.month>s[j].birth.month) k=j;
else if(s[k].birth.month==s[j].birth.month)
if(s[k].birth.day>s[j].birth.day) k=j;}
}
return s[k];
}
/**********
【习题9.027】结构体类型定义如下:
struct student
{ char id[10]; //学号
char name[10]; //姓名
int score[5]; //各门课成绩
};
结构体数组s存储了n个学生的学号、名字和各门课成绩。编写
函数,返回这n个人中第i门课成绩最高者的学号。
**********/
char *best(struct student s[], int n, int i)
{
int t=s[0].score[i],k;
for(int j=0;j<n;j++)
{if(t<s[j].score[i])
{t=s[j].score[i];k=j;}}
return s[k].id;
}
/**********
【习题9.029】结构体类型定义如下:
struct student
{ char id[10]; //学号
char name[10]; //姓名
int score[5]; //各门课成绩
};
结构体数组s存储了n个学生的学号、名字及其5门课成绩。编写
函数,返回这n个人中5门课成绩总分最高者的学号。
**********/
char *best(struct student s[], int n)
{
int sum=0,k,t=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
sum+=s[i].score[j];
if(sum>t){t=sum;sum=0;k=i;}
else sum=0;
}
return s[k].id;
}
/**********
【习题9.033】日期和链表结点的结构体类型定义如下:
struct date{int year; int month; int day;
}; //日期结构体类型
struct studentNode //链表结点的结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next;
};
结构体链表L存储
2016广工Anyview试题答案 第九章
了n个人的名字和出生日期。写一函数,求这n个人中
年龄最大(即出生日期最小)者的名字。
**********/
char *oldest(struct studentNode *L)
/* 若L是空表,则返回空指针null
否则返回表中年龄最大者的名字
*/
{
struct studentNode *p;
if(L==NULL) return NULL;
for(p=L->next;p!=NULL;p=p->next)
{
if((*p).birth.year>(*L).birth.year) continue;
if((*p).birth.year==(*L).birth.year)
{
if((*p).birth.month>(*L).birth.month) continue;
if((*p).birth.month==(*L).birth.month&&(*p).birth.day>=(*L).birth.day) continue;
}
L=p;
}
return((*L).name);
}
/**********
【习题9.053】结构体类型定义如下:
struct person
{ int id; //员工号
char name[10]; //姓名
int age; //年龄
char sex; //性别
};
结构体数组personnel[n]存储了n位员工的信息。
写一函数,返回年龄在a及以上的员工数。
**********/
int count(struct person personnel[], int n, int a)
{
int t=0;
for(int i=0;i<n;i++)
if(personnel[i].age>=a)
t++; return t;
}
/**********
【习题9.055】结构体类型定义如下:
struct person
{ int id; //员工号
char name[10]; //姓名
int age; //年龄
char sex; //性别
};
结构体数组personnel[n]存储了n位员工的信息。
写一函数,返回年龄在a及以上的x性别的员工数。
**********/
int count(struct person personnel[], int n, int a, char x)
{
int t=0;
for(int i=0;i<n;i++)
if(personnel[i].age>=a&&personnel[i].sex==x)
t++; return t;
}
/**********
【习题9.063】结构体类型定义如下:
struct course
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
};
结构体数组c存储了n门课程的信息。写一函数,求学期s的总学分。
**********/
float creditSum(struct course c[], int n, int s)
{
float sum=0;
for(int i=0;i<n;i++)
if(c[i].semester==s)
sum+=c[i].credit;
return sum;
}
/**********
【习题9.073】课程链表结点的结构体类型定义如下:
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值 …… 此处隐藏:4633字,全部文档内容请下载后查看。喜欢就下载吧 ……