当前位置:文档之家› C语言第8章习题及答案

C语言第8章习题及答案

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

#include <>typedef struct{char Name[20];char Author[20];int Date_Year;int Date_Month;int loaned;} BOOK;#define N 10void main(){BOOK books[N];int i;for (i=0;i<N;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<N;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);}}编写一个函数,统计并打印所输入的正文中的各个英文单词出现的次数,并按次数的递减顺序输出。

#include <>#include <>typedef struct{char s[20];int count;} WORD;#define N 100WORD 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;elsereturn 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<word_count;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<word_count-1;i++){t=i;for(j=i+1;j<word_count;j++)if (words[t].count<words[j].count)t=j;if (i!=t){temp=words[t];words[t]=words[i];words[i]=temp;}}for (i=0;i<word_count;i++){printf("%s : %d \n", words[i].s, words[i].count);}}void main(){char *s="Every C program has a primary main function that must be named main";func(s);}有10个学生,每个学生的数据包括学号、姓名和三门课的成绩。

用键盘输入10个学生的数据,要求打印出每个学生三门课的平均成绩,以及最高分的学生的数据(学号、姓名、三门课的成绩和平均分数)。

#include <>#include <>typedef struct{char name[20];char sno[20];int score_1;int score_2;int score_3;double score_all;} STUDENT;#define N 10STUDENT students[N]={0};void main(){int i,max;for (i=0;i<N;i++){printf("Input #%d Student's Name:");scanf("%s",students[i].name);printf("Input #%d Student's SNO:");scanf("%s",students[i].sno);printf("Input #%d Student's Score_1:");scanf("%d",&students[i].score_1);printf("Input #%d Student's Score_2:");scanf("%d",&students[i].score_2);printf("Input #%d Student's Score_3:");scanf("%d",&students[i].score_3);students[i].score_all=students[i].score_1+students[i].score_2+students[i].s core_3;}max=0;for(i=0;i<N;i++){printf("%s %s %d %d %d %lf \n",students[i].name, 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<students[i].score_all)max=i;}printf("top = %s %s %d %d %d %lf \n",students[max].name, students[max].sno, students[max].score_1, students[max].score_2, students[max].score_3, students[max].score_all/3);}请编程序建立一个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。

#include <>#include <>typedef struct __NODE{int data;__NODE* next;} NODE, *LPNODE;LPNODE head=NULL;void AddNode(int data){LPNODE t;t=(LPNODE)malloc(sizeof(NODE));t->data=data;t->next=NULL;if (head==NULL)head=t;else{t->next=head;head=t;}}int Count(LPNODE head){int i=0;while (head!=NULL){i++;head=head->next;}return i;}void main(){int n;do{scanf("%d",&n);if (n==-1) break;AddNode(n);} while(n!=-1);printf("Count:%d", Count(head));}已知一个链表,链表中的结构为:struct object{char ch;struct object *next;}编写函数统计链表中的结点个数。

#include <>#include <>typedef struct __NODE{int data;__NODE* next;} NODE, *LPNODE;LPNODE head=NULL;void AddNode(int data){LPNODE t;t=(LPNODE)malloc(sizeof(NODE));t->data=data;t->next=NULL;if (head==NULL)head=t;else{t->next=head;head=t;}}int Count(LPNODE head){int i=0;while (head!=NULL){i++;head=head->next;}return i;}void main(){int n;do{scanf("%d",&n);if (n==-1) break;AddNode(n);} while(n!=-1);printf("Count:%d", Count(head));}说明一个枚举类型enum month,它的枚举元素为:Jan、Feb、…、Dec。

相关主题