C_Primer_Plus_chapter17_exercise3

题目

Suppose list.h(Listing 17.3) uses the following defintion of a list:

c 复制代码
#define MAXSIZE 100
typedef struct list
{
  Item entries[MAXSIZE];  /* array of items */
  int items;  /*  number of items in list */
}List;

Rewrite the list.c(Listing 17.5) functions to fit this defintion and test the resulting code with the film3.c(Listing17.4) program.

解答

list.h

c 复制代码
#ifndef LIST_H_
#define LIST_H_
#include <stdbool.h>
#define TSIZE 45
#define MAXSIZE 100

struct film
{
	char title[TSIZE];
	int rating;
};

typedef struct film Item;

typedef struct list
{
	Item entries[MAXSIZE];
	int items;
} List;

void InitializeList(List * plist);

bool ListIsEmpty(const List *plist);

bool ListIsFull(const List *plist);

unsigned int ListItemCount(const List *plist);

bool AddItem(Item item, List * plist);

void Traverse (const List *plist, void (* pfun)(Item item) );

void EmptyTheList(List * plist);
#endif

list.c

c 复制代码
#include "list.h"

static void CopyToList(Item item, List * plist);

void InitializeList(List * plist)
{
	plist->items = 0;
}

bool ListIsEmpty(const List * plist)
{
	if (plist->items == 0)
		return true;
	else
		return false;
}

bool ListIsFull(const List * plist)
{
	bool full;
	if (plist->items == MAXSIZE)
		full = true;
	else
		full = false;
	return full;
}

unsigned int ListItemCount(const List * plist)
{
	return plist->items;
}

bool AddItem(Item item, List * plist)
{
	if (plist->items == MAXSIZE)
		return false;
	CopyToList(item, plist);
	plist->items++;
	return true;
}

void Traverse (const List * plist, void (* pfun)(Item item) )
{
	int i;
	for (i=0;i<plist->items;i++)
	{
		(*pfun)(plist->entries[i]);
	}
}

void EmptyTheList(List * plist)
{
	plist->items=0;
}

static void CopyToList(Item item, List * plist)
{
	plist->entries[plist->items] = item; 
}

film3.c

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
void showmovies(Item item);
char * s_gets(char * st, int n);
int main(void)
{
	List movies;
	Item temp;

	InitializeList(&movies);
	if (ListIsFull(&movies))
	{
		fprintf(stderr,"The list if full! Bye!\n");
		exit(1);
	}

	puts("Enter first movie title:");
	while (s_gets(temp.title, TSIZE) != NULL && temp.title[0] != '\0')
	{
		puts("Enter your rating in integer<0-10>:");
		int status,ch;
		while((status=scanf("%d", &temp.rating))!=1||temp.rating<0||temp.rating>10)
		{
			if(status==EOF)
			{
				break;
			}
			puts("wrong input,Enter your rating in integer<0-10>:");
			while((ch=getchar()) != '\n'  &&  ch != EOF)
				continue;
		}
		if(status==EOF)
		{
			break;
		}
		while((ch=getchar()) != '\n'  &&  ch != EOF)
			continue;
		if (AddItem(temp, &movies)==false)
		{
			fprintf(stderr,"The list is full.\n");
			break;
		}
		if (ListIsFull(&movies))
		{
			puts("The list is now full.");
			break;
		}
		puts("Enter next movie title (empty line to stop):");
	}
	/* display */
	if (ListIsEmpty(&movies))
		printf("No data entered. ");
	else
	{
		printf ("Here is the movie list:\n");
		Traverse(&movies, showmovies);
	}
	printf("You entered %d movies.\n", ListItemCount(&movies));
	/* clean up */
	EmptyTheList(&movies);
	printf("Bye!\n");
	return 0;
}
void showmovies(Item item)
{
	printf("Movie: %s Rating: %d\n", item.title, item.rating);
}
char * s_gets(char * st, int n)
{
	char * ret_val;
	char * find;
	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n'); // look for newline
		if (find) // if the address is not NULL,
			*find = '\0'; // place a null character there
		else
			while (getchar() != '\n')
				continue; // dispose of rest of line
	}
	return ret_val;
}
相关推荐
Zwawa2 小时前
让屏幕动起来:OLED动画效果,丝滑光标、帧动画和一只贪吃蛇
c语言·单片机·嵌入式
普通人63 小时前
WebGL与WebGPU入门指南
开发语言·webgl·1024程序员节
꧁细听勿语情꧂3 小时前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
§ꦿCFོ༉14 小时前
嵌入式八股文—C语言入门学习(四)
c语言·开发语言·学习
猫猫不是喵喵.20 小时前
从0到1创建一个微服务项目
java·数据库·微服务·1024程序员节
Severus_black1 天前
【C++初阶】Stack、Queue算法题大练兵!
c语言·c++·算法·stl
唐青枫1 天前
C 语言 strcpy() 用法详解与实战 Demo
c语言
程序猿打工仔1 天前
计算机基础(C语言)考编、专技岗
c语言·开发语言·算法
꧁细听勿语情꧂1 天前
二叉树余题,排序、直接插入排序、希尔排序
c语言·开发语言·数据结构·算法
是main不是漫1 天前
【C++——类和对象(上)】筑基篇:以封装奠定面向对象之基
c语言·开发语言·c++