同济大学c语言第八章答案,C语言第8章习题及答案.doc

C语言第8章习题及答案

第八章

8.1 用一个数组存放图书信息,每本书是一个结构,包括下列几项信息:书名、作者、出版年月、借出否,试写出描述这些信息的说明,并编写一个程序,读入若干本书的信息,然后打印出以上信息。#include

typedef struct

{

char Name[20];

char Author[20];

int Date_Year;

int Date_Month;

int loaned;

} BOOK;

#define N 10

void main()

{

BOOK books[N];

int i;

for (i=0;i

{

printf("Input Book's Name:");

gets(books[i].Name);

printf("Input Book's Author:");

gets(books[i].Author);

printf("Input Book's Year of Publishing:");

scanf("%d",&books[i].Date_Year);

printf("Input Book's Month of Publishing:");

scanf("%d",&books[i].Date_Month);

printf("Input Book's Status, 1-Loaned, 2-Keepin:");

scanf("%d",&books[i].loaned);

}

for (i=0;i

{

printf("Book: %s, Author: %s, Publishing:%d-%d, Status:%d\n", books[i].Name, books[i].Author, books[i].Date_Year, books[i].Date_Month, books[i].loaned);

}

}

8.3 编写一个函数,统计并打印所输入的正文中的各个英文单词出现的次数,并按次数的递减顺序输出。#include

#include

typedef struct

{

char s[20];

int count;

} WORD;

#define N 100

WORD words[N]={0};

int word_count=0;

int IsSeparator(char c)

{

if ( 'A'<=c && c<='Z' ||

'a'<=c && c<='z' ||

'0'<=c && c<='9' ||

'_'==c ||

'-'==c)

return 0;

else

return 1;

}

void AddWord(char *s)

{

int i,found=0;

char ss[20];

for(i=0;*s && !IsSeparator(*s);s++,i++)

ss[i]=*s;

ss[i]=0;

for (i=0;i

if (strcmp(ss, words[i].s)==0)

{

found=1;

break;

}

if (found==0)

{

words[word_count].count=1;

strcpy(words[word_count].s, ss);

word_count++;

}

else

{

words[i].count++;

}

}

void func(char *s)

{

int i,j,t;

char c,lc;

WORD temp;

lc=*s;

if (!IsSeparator(lc))

AddWord(s);

for (i=1;c=*(s+i);i++)

{

if (!IsSeparator(c) && IsSeparator(lc))

AddWord(s+i);

lc=c;

}

for (i=0;i

{

t=i;

for(j=i+1;j

if (words[t].count

t=j;

if (i!=t)

{

temp=words[t];

words[t]=words[i];

words[i]=temp;

}

}

for (i=0;i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值