C语言第8章习题及答案
printf("Count:%d", Count(head));
}
8.9 已知一个链表,链表中的结构为:
struct object
{char ch;
struct object *next;
}
编写函数统计链表中的结点个数。
#include <stdio.h>
#include <malloc.h>
typedef struct __NODE
}
8.5 有10个学生,每个学生的数据包括学号、姓名和三门课的成绩。用键盘输入10个学生的数据,要求打印出每个学生三门课的平均成绩,以及最高分的学生的数据(学号、姓名、三门课的成绩和平均分数)。
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[20];
{
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)
}
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;
}
8.7 请编程序建立一个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。
#include <stdio.h>
#include <malloc.h>
typedef struct __NODE
{
int data;
__NODE* next;
} NODE, *LPNODE;
}
}
8.3 编写一个函数,统计并打印所输入的正文中的各个英文单词出现的次数,并按次数的递减顺序输出。#include <stdio.h>
#include <string.h>
typedef struct
{
char s[20];
int count;
} WORD;
#define N 100
WORD words[N]={0};
void Last_Month(month m)
{
int i;
if (m==0)
i=11;
else
i=m-1;
printf("%s pre: %s \n",ms[m], ms[i]);
}
void printmon(month m)
{
printf("%s val: %d\n", ms[m], m);
}
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);
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++)
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<N;i++)
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);
void main()
{
month i;
for (i=Jan;i<=Dec;i=(enum month)(i+1))
{
Last_Month(i);
printmon(i);
}
}
8.13 设有一包含职工编号、年龄和性别的单向链表,分别使用函数完成以下功能:
(1)建立链表;
(2)分别统计男女职工的人数;
(3)在链表尾部插入新职工;
if (n==-1) break;
AddNode(n);
} while(n!=-1);
printf("Count:%d", Count(head));
}
8.11 说明一个枚举类型enum month,它的枚举元素为:Jan、Feb、…、Dec。编写能显示上个月名称的函数last_month。例如,输入Jan时能显示Dec。再编写另一个函数printmon,用于打印枚举变量的值(枚举元素)。最后编写主函数调用上述函数生成一张12个月份及其前一个月份的对照表。#include <stdio.h>
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;
students[i].score_all=students[i].score_1+students[i].score_2+students[i].score_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);
{
int sno;
int age;
int sex;
__EMPLOYEE* next;
} EMPLOYEE, *LPEMPLOYEE;
LPEMPLOYEE head=NULL, retired=NULL;
void AddNode(int sno, int age, int sex)
{
LPEMPLOYEE t,p;
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);
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;
}