2016广工Anyview试题答案 第十一章

/* 11.023 数组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;
}


/* 11.033 链表L存储了多个人的信息。写一函数,求这些人中年龄最大
(即出生日期最小)者的名字。
结构体类型定义如下:
struct date{int year; int month; int day;}; //日期结构体类型
struct studentNode //链表结点的结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
};
*/
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);
}

/* 11.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)
{
int i; float sum=0.0;
for(i=0;i<n;i++)
if(c[i].semester==s) sum+=c[i].credit;
return sum;
}

/* 11.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).semester==s) sum+=(*p).credit;
if(s==1)sum+=(*Lc).credit;
return sum;
}

/* 11.133 日期和结构体类型定义如下:
struct date{int year; int month; int day;}; //日期结构体类型
struct student //结构体类型
{ ch
ar name[10]; //人名
struct date birth; //出生日期
};
结构体数组s存储了n个人的名字和出生日期。写一函数,由数组s中n个人
的信息及其

顺序构造相应的链表。链表的结点的结构体类型定义如下:
struct studentNode //结构体类型
{ char name[10]; //人名
struct date birth; //出生日期
struct studentNode *next
};
*/
#define N sizeof(struct studentNode)
struct studentNode *CreateLinkList(struct student s[], int n)
{ struct studentNode *head,*p1,*p2;int i=0;
p1=p2=(struct studentNode *) malloc(N); head=NULL;
while(i<n)
{ 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(N);
++i;
}
p2->next=NULL; return (head);
}

/* 11.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 *Lc2;
while(Lc!=NULL)
{if(Lc->cID==c)
Lc->credit=t,Lc2=Lc;
Lc=Lc->next;
}

return(Lc2);
}

//11.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);
}

/**********
11.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<n-1;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;
}


/**********
11.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<n-1;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;
}



相关主题
相关文档
最新文档