当前位置:文档之家› 《C语言程序设计教程》(第三版)李凤霞 主编——第十章习题答案

《C语言程序设计教程》(第三版)李凤霞 主编——第十章习题答案

习题十
一、单选题
1~5 DDDCC
6~10 AABDB
11~14 CADC
二、填空题
1、34 12
2、ARRAY a,b,c;
3、1 3
4、a c
5、(*b).day=? b->day=?
5、分析下列程序执行结果。

#include “stdio.h”
main()
{static struct s1
{char c[4],*s;
s1={“abc”,”def”};
static struct s2
{char *cp;struct s1 ss1;
}s2={“ghi”,{“jkl”,”mno”}};
printf(“%c%c\n”,s1.c[0],*s1.s); /*output ab */
printf(“%s%s\n”,s1.c,s1.s); /*output abcdef */
printf(“%s%s\n”,s2.cp,s2.ss1.s); /*output ghimno */
printf(“%s%s\n”,++s2.cp,++s2.ss1.s); /* output hino */
}
6、以下程序的功能是:读入一行字符(如:a,...,y,z),按输入时的逆序建立一个链式的结点序列,即
先输入的位于链表尾(如下图),然后再按输入的相反顺序输出,并释放全部结点。

#define getnode(type)_________malloc(sizeof(type)) ( (struct node *))
main()
{struct node
{char info;
struct node *link;
}*top,*p;
char c;
top=NULL;
while((c=getchar())______________) (!='\n')
{p=getnode(struct node);
p->info=c;
p->link=top;
top=p;
}
while(top)
{_________________; (p=top)
top=top->link;
putchar(p->info);
free(p);
}
}
7、下面的函数将指针p2所指向的线性链表链接到p1所指向的的链表的末端。

假定p1所指向的链表非空。

#define NULL 0
struct link
{float a;
struct link *next;
};
concatenate(p1,p2)
struct list *p1,*p2;
{if(p1->next==NULL)
p1->next=p2;
else
concatenate(___________,p2); (p->next)
}
8、以下函数create用来建立一个带头结点的单项链表。

新产生的结点总是插入再链表的末尾。

单向链表
的头指针作为函数的返回值。

#include <stdio.h>
struct list
{char data;
struct list *list;
};
struct list *cteate()
{struct list *h,*p,*q;
char ch;
h=___________malloc(sizeof(struct list)); (struct list *)
p=q=h;
ch=getchar();
while(ch!='\n')
{p=____________malloc(sizeof(struct list)); (struct list *)
p->date=ch;
q->next=p;
q=p;
ch=getchar();
}
p->next='\0';
______________; (rerturn h)
}
三、编程题
1、成绩排序。

按学生的序号输入学生的成绩,按照分数由低到高的顺序输出学生的名次、该名次的分数、
相同名次的人数和学号;同名次的学号输出再同一行中,一行最多输出10 个学号。

#include "stdio.h"
struct student
{int n;
int mk;
}
main( )
{int i,j,k,count=0,no;
struct student stu[100],*s[100],*p;
printf("\nPleasse enter mark(if mark<0 is end) \n");
for(i=0;i<100;i++)
{ printf("No.%4d= =',i+1);
scanf("%d",&stu[i].mk);
s[i]=&stu[i];
stu[i].n=i+1;
if(stu[i].mk<=0)break;
for(j=0;j<I;j++)
for(k=j+1;k<=I;k++)
if(s[j]->mk<s[k]->mk)
{p=s[j];s[j]=s[k];s[k]=p;}
}
for(no=1,count=1,j=0;j<I;j++)
{if(s[j]->mk>s[k+1]->mk)
{printf("\nNo.%3d= =%4d%4d:",no,s[j]->mk,count);
for(k=j-count+1;k<=j;k++)
{ptintf("%3d",s[k]->n);
if((k-(j-count))%10= =0&&k!=j)
printf("\n ");
}
count=1;
no++;
}
else count++;
}
}
2、现在有教师(姓名、单位、住址、职称)和学生(姓名、班级、住址、入学成绩)的信息。

请在输入
10名教师和学生的信息后,按姓名进行排序,最后按排序后的顺序进行输出,对于教师要输出姓名、单位、住址和职称,对学生要输出姓名、班级、住址和入学成绩。

请编程实现。

/*p332_2.c*/
#include "string.h"
union dwbj
{char dw[20];
char bj[10];
};
union zcrxcj
{char zhich[10];
float rxcj;
};
struct inf
{int fl;/*0:teacher;1:student*/
char name[20];
union dwbj db;
char addr[30];
union zcrxcj zs;
};
main()
{int i,j;
struct inf info[10];
for(i=0;i<10;i++)
{printf("teacher:input 0;student:input 1\n");
scanf("%d",&info[i].fl);
printf("Input name of teacher or student:\n");
scanf("%s",info[i].name);
printf("Input dept of teacher or class of student:\n");
if(info[i].fl==0)scanf("%s",info[i].db.dw);
else scanf("%s",info[i].db.bj);
printf("Input address:\n");
scanf("%s",info[i].addr);
printf("job of teacher or suc of student\n");
if(info[i].fl==0)scanf("%s",info[i].zs.zhich);
else scanf("%f",&info[i].zs.rxcj);
}
for(i=0;i<10;i++)
{printf("%s,",info[i].name);
if(info[i].fl==0)printf("%s,",info[i].db.dw);
else printf("%s,",info[i].db.bj);
printf("%s,",info[i].addr);
if(info[i].fl==0)printf("%s\n",info[i].zs.zhich);
else printf("%f\n",info[i].zs.rxcj);
}
}。

相关主题