C语言答案第8章
第8章
[习题-1]/*c8_1.c*/
#include
struct book
{
char title[20];
char aditor[10];
int year;
int month;
char flag;
}liber[10];
void main( )
{
int i;
for(i=0;i<10;i++)
{
rintf("Input book title:\n");
scanf("%s",liber[i].title);
printf("Input book aditor:\n");
scanf("%s",liber[i].aditor);
printf("Input print date,year and month :\n");
scanf("%d%d",&liber[i].year,&liber[i].month);
printf("Input book information:\n");
scanf("%c",&liber[i].flag);
}
for(i=0;i<10;i++) printf("%s,%s,%d-%d,%c\n",liber[i].title,liber[i].aditor,liber[i].year,liber[i].month,liber[i].flag);
}
[习题-2]/*c8_2.c*/
#include
#include
#include
struct list
{
char words[20];
int num;
struct list *next;
};
typedef struct list node;
typedef node *link;
void count(char *str)/*统计正文中单词个数*/
{
link ptr,head,ptrn,headnew;
int i=0,j=0,prei=0;
char temp[20];
ptr=(link)malloc(sizeof(node));
head=ptr;
ptr->next=NULL;
do
{
if(*(str+i)==' '||*(str+i)=='\0')
{/*出现一个单词*/
temp[j]='\0';
ptr=head;
while(ptr->next!=NULL)
{
if(strcmp(ptr->next->words, temp)==0)/*该单词已经出现*/
{
ptr->next->num++;
break;/*结束内层while循环*/
}
else
ptr=ptr->next;
}
if(ptr->next==NULL)/*该单词是新单词*/
{
ptr->next=(link)malloc(sizeof(node));
strcpy(ptr->next->words,temp);
ptr->next->num=1;
ptr->next->next=NULL;
}
if(*(str+i)=='\0') break; /*全部正文已经结束,跳出循环*/
j=0;
i++;
continue;
}
temp[j++]=str[i++];
}while(1);
ptrn=(link)malloc(sizeof(node));/*新建一个按照顺序排列的链表*/
headnew=ptrn;
ptrn->next=NULL;
ptr=head->next;
ptrn=headnew;
while(head->next!=NULL)
/*把head链表中的结点依次取出按照顺序链接到新的链表headnew中*/
{
while(ptrn->next!=NULL&&ptrn->next->num>=ptr->num)
ptrn=ptrn->next;
head->next=ptr->next;
ptr->next=ptrn->next;
ptrn->next=ptr;
ptr=head->next;
ptrn=headnew;
}
ptr=headnew->next;
while(ptr!=NULL){/*将排好序的结点输出*/