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;
}
相关推荐
东城居士1 小时前
理解Linux LCD驱动编程
linux·嵌入式系统·1024程序员节
frcoder1 小时前
TackHarness打造属于自己的编程工作流
agent·ai编程·1024程序员节·研发·harness
neocheng_5221 小时前
希望拿证后还能继续学习,AI 认证怎么选?
1024程序员节
星恒随风1 小时前
Linux线程同步与互斥:彻底理解读者写者问题、读写锁原理、读者优先与写者优先(附pthread完整实验)
linux·服务器·笔记·1024程序员节
QX_hao1 小时前
【GPG】使用GPG安全传递隐私信息
1024程序员节·gpg
特立独行的猫a1 小时前
仓颉语言cj-tauri 跨端开发框架实战:给鸿蒙侧加一个「读设备电量」的原生能力
ui·harmonyos·tauri·1024程序员节·仓颉·鸿蒙pc
_upupup1 小时前
Linux中的进程
linux·服务器·1024程序员节
AI技术新视界1 小时前
递归自我改进(RSI)是什么
人工智能·llm·1024程序员节·rsi
@zulnger1 小时前
安卓进行稳定性测试
android·appium·1024程序员节