文档库 最新最全的文档下载
当前位置:文档库 › 2016广工Anyview试题答案 第九章

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{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);
}

/**********
【习题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;iif(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;iif(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;iif(c[i].semester==s)
sum+=c[i].credit;
return sum;
}

/**********
【习题9.073】课程链表结点的结构体类型定义如下:
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
struct courseNode *next;
};
结构体链表Lc存储了各学期多门课程的信息。写一函数,求学
期s的总学分。
**********/
float creditSum(struct courseNode *Lc, int s)
/* 若Lc是空表,则返回0;
否则返回学期s的总学分
*/
{
struct courseNode *p; float sum=0.0;
if(Lc==NULL) return 0.0;
for(p=Lc->next;p!=NULL;p=p->next)
if((*p).se

mester==s)
sum+=(*p).credit;
if(s==1)
sum+=(*Lc).credit;
return sum;
}

/**********
【习题9.133】日期和结构体类型定义如下:
struct date{int year; int month; int day;}; //日期结构体类型
struct student //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
};
结构体数组s存储了n个人的名字和出生日期。写一函数,由数组s中n个人
的信息及其顺序构造相应的链表。链表的结点的结构体类型定义如下:
struct studentNode //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
};
**********/

struct studentNode *CreateLinkList(struct student s[], int n)
{
struct studentNode *head,*p1,*p2;int i=0;
p1=p2=(struct studentNode *) malloc(sizeof(struct studentNode)); head=NULL;
while(i{ strcpy (p1->name,s[i].name); p1->birth=s[i].birth;
if(i==0) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct studentNode *) malloc(sizeof(struct studentNode));
++i;
}
p2->next=NULL; return (head);

}

/**********
【习题9.173】课程链表结点的结构体类型定义如下:
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
struct courseNode *next;
};
结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程的学分修改为t。
**********/

struct courseNode *creditChange(struct courseNode *Lc, int c, float t)
/* 若课程c不存在,则修改不成功,返回null;
否则修改该课程的学分为t,返回指向该课程结点的指针。
*/
{
struct courseNode *p;
for(p=Lc->next;p!=NULL;p=p->next)
if((*p).cID==c)
{(*p).credit=t; return p;}
if((*Lc).cID==c)
{(*Lc).credit=t;return Lc;}
return NULL;
}

/**********
【习题9.183】课程链表结点的结构体类型定义如下:
struct courseNode //课程链表结点的结构体类型
{ int cID; //课程号,取值0~99
char name[10]; //课程名
float credit; //学分,取值0~5
int semester; //学期,取值1~8
struct courseNode *next;
};
结构体链表Lc存储了多门课程的信息。写一函数,将课程号为c的
课程结点删除。
**********/

struct courseNode *deleteCourse(struct courseNode **Lc, int c)
/* 若在链表Lc中课程c不存在,则删除不成功,返回null;
否则从链表Lc中删除该课程结点,并返回指向该课程结点的指针。
*/
{
struct courseNode *p,*q,*t;
p=*Lc;
while(c!=p->cID&&p->next!=NULL)
{q=p;
p=p->next;
}
if(c==p->cID)
{

if(p==*Lc)
{ t=p;
*Lc=p->next;
}
else
{t=p;
q->next=p->next;
}
}
return(t);
}

/**********
【习题9.302】单向链表的结点类型定义如下:
struct node{
char ch;
struct node *next;
};
编写函数,对单向链表L实现就地逆置,即将所有结点
的指针反向,原链头当作链尾,原链尾当作链头,并返
回逆置后链表的头指针。
**********/
struct node *inverse(struct node *L)
{
struct node *p=L,*q;int n=0,i=0; char t;
while(p!=NULL)
{p=p->next;
++n;
}
p=L;
for(;i{ while(p!=NULL)
{ q=p->next;
if((p->ch)<(q->ch))
{t=p->ch,p->ch=q->ch,q->ch=t;}
if(q->next!=NULL)
p=p->next;
else p=NULL;
}
p=L;
}
return L;
}


/**********
【习题9.352】单向链表的结点类型定义如下:
struct node{
char ch;
struct node *next;
};
编写函数,对单向链表L实现排序,即按结点的ch值,
从小到大重构链表L,并返回排序后的链表的头指针。
**********/
struct node *sorting(struct node *L)
/* 对单向链表L实现从小到大排序,
并返回重构后的链表的头指针。
*/
{
struct node *p=L,*q;int n=0,i=0; char t;
while(p!=NULL)
{p=p->next;
++n;
}
p=L;
for(;i{ while(p!=NULL)
{ q=p->next;
if((p->ch)>(q->ch))
{t=p->ch,p->ch=q->ch,q->ch=t;}
if(q->next!=NULL)
p=p->next;
else p=NULL;
}
p=L;
}
return L;
}

相关文档
相关文档 最新文档