广工Anyview试题答案

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

/**********

【习题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

{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

{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

{if(t

{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

{

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存储了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);

}

相关文档
最新文档