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;
}
相关推荐
草莓熊Lotso4 小时前
Qt 进阶控件实战:下拉框、列表、表格 + 旋钮 / 滑动条全攻略
大数据·c语言·开发语言·c++·人工智能·qt
龙砚序5 小时前
【数据结构|线性表实战 01】基于顺序表的地铁站点管理:查找、增删、有序二路归并(C 语言实现)
c语言·数据结构·算法
龙砚序5 小时前
【数据结构|栈实战 03-1】顺序栈完整实现与工程应用:浏览器历史回溯、IP 地址进制转换(C 语言)
c语言·数据结构·tcp/ip
wuminyu7 小时前
OpenJDK学习笔记:如何构建支持动态注册、注销和重新注册的 Native回调管理器
java·linux·c语言·jvm·c++
金榜题名萱7 小时前
蓝桥杯 - 拍照 —— 动态规划
c语言·数据结构·c++·算法·职场和发展·蓝桥杯·动态规划
Matt Feng17 小时前
STM32串口ISP多路烧录:4个关键参数与批量下载步骤
c语言·stm32·单片机·嵌入式硬件·接口隔离原则
derek199717 小时前
软考软件设计师:B-树和 B+树的区别、线性探查法查找时到底比较了谁
c语言·c++·计算机网络·软件工程·学习方法
小灰灰搞电子18 小时前
C 语言基础:动态内存管理详解
c语言·开发语言
一只QAQ1 天前
c语言八股
c语言·开发语言
wuyk5551 天前
AI 时代的嵌入式该怎么学-11-C不是语法是对内存的显式建模
c语言·开发语言·stm32·单片机·嵌入式硬件·mcu·算法