当前位置:首页 > 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(\ gets(books[i].Name); printf(\ gets(books[i].Author); printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ } for (i=0;i printf(\%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 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 if (i!=t) { temp=words[t]; words[t]=words[i]; words[i]=temp; } } for (i=0;i printf(\ } } void main() { char *s=\program has a primary main function that must be named main\ func(s); } 8.5 有10个学生,每个学生的数据包括学号、姓名和三门课的成绩。用键盘输入10个学生的数据,要求打印出每个学生三门课的平均成绩,以及最高分的学生的数据(学号、姓名、 三门课的成绩和平均分数)。 #include typedef struct { char name[20]; char sno[20]; int score_1; int score_2; int score_3; double score_all; } STUDENT; #define N 10 STUDENT students[N]={0}; void main() { int i,max; for (i=0;i printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ printf(\ scanf(\ students[i].score_all=students[i].score_1+students[i].score_2+students[i].score_3; } max=0; for(i=0;i printf(\%s %d %d %d %lf \\n\students[i].sno, students[i].score_1, students[i].score_2, students[i].score_3, students[i].score_all/3); if (students[max].score_all
共分享92篇相关文档