C Primer Plus(第六版)第十四章 编程练习答案

Posted 水番正文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第十四章 编程练习答案相关的知识,希望对你有一定的参考价值。

这次打了真的好多天,不过坚持下来了,现在知道开发并不容易,不过值得学习。--10.16

CH14 Code answer 1:

#include <stdio.h>
#include <string.h>

int Days(char *);

struct month {
	char name[10];
	char abbrve[4];
	int days;
	int monum;
};

struct month months[12] =
	{
		{ "January", "Jan", 31, 1 },
		{ "February", "Feb", 29, 2 },
		{ "March", "Mar", 31, 3 },
		{ "April", "Apr", 30, 4 },
		{ "May", "May", 31, 5 },
		{ "June", "Jun", 30, 6 },
		{ "July", "Jul", 31, 7 },
		{ "August", "Aug", 31, 8 },
		{ "September", "Sep", 30, 9 },
		{ "October", "Oct", 31, 10 },
		{ "November", "Nov", 30, 11 },
		{ "December", "Dec", 31, 12 } 
	};

int main(void)
{
	int total;
	char mon[10];					//一直疑问作者用字符指针来存储 但这边初始化的时候必须给定大小来存储 
	
	puts("Enter a month name:");
	while ( scanf("%s", mon) )
	{
		total = Days(mon);
		printf("%d\\n", total);
		puts("Enter next name(enter other to quit):");
	} 
	
	return 0;	
} 

int Days(char * mon)
{
	int index, i;
	int total = 0;
	
	for (index = 0; index < 12; index++)			//返回月份范围 
		if (!strcmp(months[index].abbrve, mon))
			break;
			
	if ( index < 12 && index >= 0)			//如果是不存在的月份index为12 
		for( i = 0 ; i <= index; i++ )
			total += months[i].days;
	else
		return -1;
	
	return total;
}

CH14 Code answer 2:

#include <stdio.h>
#include <string.h> 

int Days(char *, int);
char * s_gets(char *, int); 

struct month {
	char name[10];
	char abbrve[4];
	int days;
	char monum[3];
};

struct month months[12] =
	{
		{ "January", "Jan", 31, "1" },
		{ "February", "Feb", 29, "2" },
		{ "March", "Mar", 31, "3" },
		{ "April", "Apr", 30, "4" },
		{ "May", "May", 31, "5" },
		{ "June", "Jun", 30, "6" },
		{ "July", "Jul", 31, "7" },
		{ "August", "Aug", 31, "8" },
		{ "September", "Sep", 30, "9" },
		{ "October", "Oct", 31, "10" },
		{ "November", "Nov", 30, "11" },
		{ "December", "Dec", 31, "12" } 
	};

int main(void)
{
	int total;
	char mon[10] = {0};
	int n;
	int year;
	
	puts("Enter the month name or number:");
	while(s_gets(mon, 10) != NULL && mon[0] != '\\0')
	{
		puts("Enter the day:");
		scanf("%d", &n);
		while (getchar() != '\\n')
			continue;
				
		puts("Enter the year:");
		scanf("%d", &year);
		while (getchar() != '\\n')
			continue;
		
		total = Days(mon, n);
		printf("%d days have passed since %d\\n", total, year);
		
		puts("\\nEnter the month name or number:(Enter empty line to quit)");
	}
	
	return 0;
}

int Days(char * mon, int n)
{
	int i, index;
	int total = 0;
	
	if (strlen(mon) <= 2)			//一开始没加花括号 if只能管下面那条语句当判断错误 就会执行到下面那个if 而不是elseif 
	{
		for (index = 0; index < 12; index++)
			if ( !strcmp(months[index].monum, mon) )
				break;
	}		
	else if (strlen(mon) == 3)
	{
		for (index = 0; index < 12; index++)
			if ( !strcmp(months[index].abbrve, mon) )
				break;
	}		
	else
	{
		for (index = 0; index < 12; index++)
			if ( !strcmp(months[index].name, mon) )
				break;
	} 
	
	if (index < 12 && index >= 0)
	{ 
		for ( i = 0; i < index; i++)	//假设用户输入的是2月 那么就是一月+用户输入的天数 所以是<index 
			total += months[i].days;
	} 
	else
		return -1;
		 
	return total + n;
}

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');
		if (find)
			*find = '\\0';
		else
			while (getchar() != '\\n')
				continue;
	}
	
	return ret_val;
}

CH14 Code answer 3:

/* manybook.c -- multiple book inventory */
#include <stdio.h>
#include <string.h>

#define MAXTITL   40
#define MAXAUTL   40
#define MAXBKS   100              /* maximum number of books  */

struct book {                     /* set up book template     */
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

char * s_gets(char * st, int n);
void CPrint(struct book p[], int n);
void NPrint(struct book p[], int n);

int main(void)
{
    struct book library[MAXBKS]; /* array of book structures */
    int count = 0;
    int index;
    
    printf("Please enter the book title.\\n");
    printf("Press [enter] at the start of a line to stop.\\n");
    while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
           && library[count].title[0] != '\\0')
    {
        printf("Now enter the author.\\n");
        s_gets(library[count].author, MAXAUTL);
        printf("Now enter the value.\\n");
        scanf("%f", &library[count++].value);
        while (getchar() != '\\n')
            continue;          /* clear input line         */
        if (count < MAXBKS)
            printf("Enter the next title.\\n");
    }
    
    if (count > 0)
    {
        printf("Here is the list of your books:\\n");
        for (index = 0; index < count; index++)
            printf("%s by %s: $%.2f\\n", library[index].title,
						library[index].author, library[index].value);
						
        puts("\\n");
        CPrint(library, count);
        puts("\\n");
        NPrint(library, count);
    }
    else
    	printf("No books? Too bad.\\n");
    
    return 0;
}

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;
}

void CPrint(struct book p[], int n)
{
	int i, j;
	struct book tmp; 
	
	for (i = 0; i < n - 1; i++)
	{
		for ( j = i + 1; j < n; j++)
			if ( p[i].title[0] > p[j].title[0] ) 
			{
				tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		printf("%s by %s: $%.2f\\n", p[i].title, p[i].author, p[i].value);
	}
	printf("%s by %s: $%.2f\\n", p[i].title, p[i].author, p[i].value);	
	
} 

void NPrint(struct book p[], int n)
{
	int i, j;
	struct book tmp; 
	
	for (i = 0; i < n - 1; i++)
	{
		for ( j = i + 1; j < n; j++)
			if ( p[i].value > p[j].value ) 
			{
				tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		printf("%s by %s: $%.2f\\n", p[i].title, p[i].author, p[i].value);
	}
	printf("%s by %s: $%.2f\\n", p[i].title, p[i].author, p[i].value);	
	
} 

CH14 Code answer 4:

A:

#include <stdio.h>
#include <string.h>

#define LEN 20

struct names{
	char first[LEN];
	char center[LEN];
	char last[LEN];
};

struct guy{
	unsigned int sty;
	struct names handle;
};

void Print(const struct guy * p);

int main(void)
{
	struct guy ifm[5] ={
		{
			302039823,
			{ .first = "Zeep", .last = "Po" },
		},
		{
			302039823,
			{ "Emo", .last = "Tao" },
		},
		{
			302039823,
			{ "Jun", "Hao", "Zhu" },
		},
		{
			302039823,
			{ "Zhong", "Zheng", "Chen" },
		},
		{
			302039823,
			{ "P", .last = "W" },
		}	
	};
	
	Print(ifm);
	
	return 0;
}

void Print(const struct guy * p)		//传递地址 
{
	int i;
	
	for ( i = 0; i < 5; i++ )
	{
		if ( !strlen((p + i)->handle.center) )
			printf("%s.%s -- %d\\n", (p + i)->handle.last, (p + i)->handle.first, (p + i)->sty);
		else
			printf("%s.%s %c. -- %d\\n", (p + i)->handle.last, (p + i)->handle.first, 
								(p + i)->handle.center[0], (p + i)->sty);	
	} 
}

B:

#include <stdio.h>
#include <string.h>

#define LEN 20

struct names{
	char first[LEN];
	char center[LEN];
	char last[LEN];
};

struct guy{
	unsigned int sty;
	struct names handle;
};

void Print(const struct guy * p);

int main(void)
{
	struct guy ifm[5] ={
		{
			302039823,
			{ .first = "Zeep", .last = "Po" },
		},
		{
			302039823,
			{ "Emo", .last = "Tao" },
		},
		{
			302039823,
			{ "Jun", "Hao", "Zhu" },
		},
		{
			302039823,
			{ "Zhong", "Zheng", "Chen" },
		},
		{
			302039823,
			{ "P", .last = "W" },
		}	
	};
	
	Print(ifm);
	
	return 0;
}

void Print(const struct guy p[])		//传递个值 这是个副本 
{
	int i;
	
	for ( i = 0; i < 5; i++ )
	{
		if ( !strlen((p + i)->handle.center) )
			printf("%s.%s -- %d\\n", (p + i)->handle.last, (p + i)->handle.first, (p + i)->sty);
		else
			printf("%s.%s %c. -- %d\\n", (p + i)->handle.last, (p + i)->handle.first, 
								(p + i)->handle.center[0], (p + i)->sty);	
	} 
}

CH14 Code answer 5:

#include <stdio.h>
#include <string.h>

#define CSIZE 4
#define NLEN 10

struct name{
	char first[NLEN];
	char last[NLEN];
};

struct student{
	struct name hm;
	float grade[3];
	float avg;
};

char * s_gets(char * st, int n);
void Print_ifm(const struct student * p, int n);

int main(void)
{
	struct student guy[CSIZE];
	int count = 0;
	int i;
	
	puts("Enter a student name:");
	while ( count < CSIZE && s_gets(guy[count].hm.last, NLEN) != NULL 
				&& guy[count].hm.last[0] != '\\0' )
	{
		puts("Enter the student first name:");
		s_gets(guy[count].hm.first, NLEN);
		
		puts("Enter the student that three grades:");
		for ( i = 0; i < 3; i++)
			scanf("%f", &guy[count].grade[i]);
		guy[count].avg = (guy[count].grade[0] + guy[count].grade[1] +
								guy[count].grade[2]) / 3;
		while ( getchar() != '\\n' )
			continue;
			
		puts("Enter the next student name:(Enter empty line to quit)");
		count++;
	}
	
	Print_ifm(guy, count);
	printf("All avg:%-8.2f.", (guy[0].avg + guy[1].avg + guy[2].avg)/3 );
	
	return 0; 
}

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');   
        if (find)                 
            *find = '\\0';         
        else
            while (getchar() != '\\n')
                continue;          
    }
    return ret_val;
}

void Print_ifm(const struct student * p, int n)
{
	int i;
	
	for ( i = 0; i < n; i++)
		printf( "%s %s:  exam1:%-8.2f exam2:%-8.2f exam3:%-8.2f  avg:%-5.2f.\\n", (p + i)->hm.first, 
					(p + i)->hm.last, (p + i)->grade[0], (p + i)->grade[1], (p + i)->grade[2],
					(p + i)->avg );
}

CH14 Code answer 6:

#include <stdio.h>
#include <stdlib.h>

#define NLEN 20
#define MMB 18
#define IFM 5
#define LMAX 256

struct name{
	char first[NLEN];
	char last[NLEN];
};

struct member{
	int num;
	struct name self;
	int exam;
	int bingo;
	int walk;
	int point;
	float beat;
};

void f_gets(FILE * fp, struct member (*p)[IFM], int next[MMB]);
void Print(struct member (*p)[IFM], int next[MMB]);

int main(void)
{
	FILE * fp;
	struct member guy[MMB][IFM] = { 0 };	
	char fname[NLEN];			
	int next[MMB] = { 0 };			//存储同一名棒球手的多次出线
	 
	puts("Enter the file name:");
	scanf("%s", fname);
	if ( (fp = fopen(fname, "r")) == NULL )
	{
		fprintf(stderr, "Can't open the file!");
		exit(EXIT_FAILURE);
	}
	
//	fscanf(fp, "%d", &n);			一定提前尝试能不能读取!!要不然理论想通了但一直不通 
//	printf("%d", n);				发现是读取文件的指针少了个括号 
	f_gets(fp, guy, next); 
	
	Print(guy, next);

	puts("\\nDone!");
	
	return 0;
} 
//这个程序真是打了太久了 把与文件和结构放在一起 莫名其妙各种地方卡了半天 还是要先单个测试再打循环 
void f_gets(FILE * fp, struct member (*p)[IFM], int next[MMB])
{
	int i, j;	
	int itmp = 0;
	
//尝试创建一个结构作为载体 但fgets对于文件的读取一直调试不好 于是用了这个比较笨的方法 但真的没有什么其他思路	
    while (fscanf(fp, "%d", &itmp) == 1)
    {
//    	printf("%d", itmp - 1);
    	if ( p[itmp - 1][next[itmp - 1]].num != 0 )
    		next[itmp - 1]++;
    		
    	p[itmp - 1][next[itmp - 1]].num = itmp;
    	fscanf(fp, "%s", p[itmp - 1][next[itmp - 1]].self.first);
//    	puts(p[itmp - 1][next[itmp - 1]].self.first); 
    	fscanf(fp, "%s", p[itmp - 1][next[itmp - 1]].self.last);
    	fscanf(fp, "%d", &p[itmp - 1][next[itmp - 1]].exam);
    	fscanf(fp, "%d", &p[itmp - 1][next[itmp - 1]].bingo);
    	fscanf(fp, "%d", &p[itmp - 1][next[itmp - 1]].walk);
    	fscanf(fp, "%d", &p[itmp - 1][next[itmp - 1]].point);	
	}
		
}

void Print(struct member (*p)[IFM], int next[MMB])
{
	int i, j;
	
	for ( i = 0, j = 0; i < MMB; i++, j = 0)
	{
		if (p[i][j].num == 0)
			break;
			
		float x = 0, y = 0;
		for ( j = 0; j <= next[i]; j++ )
		{
			x += p[i][j].bingo;
			y += p[i][j].exam;
		}

		for ( j = 0; j <= next[i]; j++ )
			p[i][j].beat = x / y;
		
		for ( j = 0; j <= next[i]; j++)		//数组是存在第0所以数为0 所以要<=	
			printf("%d %-5s %-8s %-5d %-5d %-5d %-5d %-5.2f\\n", p[i][j].num, p[i][j].self.first, 
				p[i][j].self.last, p[i][j].exam, p[i][j].bingo,
					p[i][j].walk, p[i][j].point, p[i][j].beat);
			
	}
 } 

CH14 Code answer 7:

/* booksave.c -- saves structure contents in a file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXTITL  40
#define MAXAUTL  40
#define MAXBKS   10             /* maximum number of books */

struct book {                   /* set up book template    */
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

char * s_gets(char * st, int n);
int Find_Emt(struct book * p); 
void Del_Tag(struct book * p, char * name);

int main(void)
{
    struct book library[MAXBKS] = { 0 }; /* array of structures 要记得清空作者居然没初始化    */
    int count = 0;
    int index;
    FILE * pbooks;
    int size = sizeof (struct book);
    int choice = 0;
    
    
    //打开文件 
    if ( (pbooks = fopen("book.txt", "a+b")) == NULL )
    {
        fputs("Can't open book.txt file\\n",stderr);
        exit(1);
    }
    
    
    //打印原始信息 count计算个数 
    rewind(pbooks);            /* go to start of file     */
    while (count < MAXBKS &&  fread(&library[count], size, 1, pbooks) == 1)
    {
        if (count == 0)
            puts("Current contents of book.txt:");
        printf("%s by %s: $%.2f\\n",library[count].title,
               library[count].author, library[count].value);
        count++;
    }
    if (count == MAXBKS)
    {
        fputs("The book.txt file is full.", stderr);
        exit(2);
    }
    
    
    //进行增删操作 
    puts("0:Del data     1:Add data     7:Quit");
    while( scanf("%d", &choice) && choice != 7 )
    {
    	while (getchar() != '\\n')
    		continue;
    	if (choice == 1 || choice == 0 )
    	{
    		if (choice == 1)
    		{
    			puts("Please add new book titles.");
    			puts("Press [enter] at the start of a line to stop.");
    			count = Find_Emt(library);			
    			while (  count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
           			&& library[count].title[0] != '\\0' )		//Find_Emt放在while中就会出错是为什么?? 
    			{
        			puts("Now enter the author.");
        			s_gets(library[count].author, MAXAUTL);
        			puts("Now enter the value.");
        			scanf("%f", &library[count].value);
        			while (getchar() != '\\n')
            			continue;                /* clear input line  */
        			if (count < MAXBKS)
            			puts("Enter the next title.");
        			count = Find_Emt(library);
    			}
			}
			else
			{
				pbooks = fopen("book.txt", "r+b"); //出现修改所以要r+b打开 
				
				puts("Enter the Del name of title");
				char name[MAXTITL];
				s_gets(name, MAXTITL);
				Del_Tag(library, name);
			} 
		}
		else
			puts("Invalid input!");
		puts("0:Del data     1:Add data     7:Quit");
	}
    
    
    //打印之后的信息 并写入文件 
    if (count > 0)
    {
    	rewind(pbooks);
        puts("Here is the list of your books:");
        for (index = 0; index < count; index++)
        {
        	if (library[index].title[0] != '\\0')
        	{
        		printf("%s by %s: $%.2f\\n",library[index].title,
                   library[index].author, library[index].value);
                fwrite(&library[index], size, 1, pbooks);	//根据count全部写入 
			} 		
		}  
    }
    else
    	puts("No books? Too bad.\\n");
    
    
    puts("Bye.\\n");
    fclose(pbooks);
    
    return 0;
}

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');   
        if (find)                 
            *find = '\\0';         
        else
            while (getchar() != '\\n')
                continue;         
    }
    return ret_val;
}

int Find_Emt(struct book * p)
{
	int i;
	
	for ( i = 0; i < MAXBKS; i++ )
	{
//		printf("%s ", p[i].title);
		if ( p[i].title[0] == '\\0' )
			break;	
	}
			
//	printf("i = %d ", i);
	return i;
}

void Del_Tag(struct book * p, char * name)
{
	int i;
	
	for ( i = 0; i < MAXBKS; i++ )
		if (!strcmp(name, p[i].title))
		{
			p[i].title[0] = '\\0';
			break;
		}
}

CH14 Code answer 8:

#include <stdio.h>
#include <string.h>

#define NMAX 10
#define MMAX 12

struct member{
	int num;
	int reserve;
	char first[NMAX];
	char last[NMAX];
};

char * s_gets(char * st, int n);
void Print_imf(void);
void Emt_Num(struct member * p);
void Emt_Seats(struct member * p);
void Ex_Seats(struct member p[MMAX]);
int Find_Emt(struct member * p);
void Set_Seats(struct member * p);
void Del_Seats(struct member * p);

int main(void)
{
	char ch;
	struct member group[MMAX] = 
	{ 
		{ 1, 1, "Po", "Zeep" },
		{ 2, 0, "C", "Zz" },
		{ 3, 1, "Tao", "Tao" },
		{ 4, 0, "Lan", "N" }, 
		{ 5, 0, "Zhu", "Hao" },
		{ 6, 1, "Ma", "C" },
		{ 7, 0, "W", "P" },
		{ 8, 0, "Hu", "Rui" },
		{ 9, 1, "C", "C" },
		{ 10, 1, "Yan", "CQ" },
		{ 11, 0, "Z", "Z" },
		{ 12, 1, "Q", "J" }
	};
	int count;
	
	Print_imf();
	while ( scanf("%c", &ch) && ch != 'f' )
	{
		printf("\\n");
		while (getchar() != '\\n')
			continue;
			
		if ( ch < 'f' && ch >= 'a' )
		{
			switch (ch)
			{
				case 'a':
					Emt_Num(group);
					break;
				case 'b':
					Emt_Seats(group);
					break;
				case 'c':
					Ex_Seats(group);
					break;
				case 'd':
					Set_Seats(group);
					break; 
				case 'e':
					Del_Seats(group);
					break;
			}
		}
		else
			puts("Invalid input!");
		Print_imf();
	}
	
	printf("\\nDone!");
	
	return 0;
}

void Print_imf(void)
{
	puts("To choose a function, enter its letter label:");
	puts("a) Show number of empty seats");
	puts("b) Show list of empty seats");
	puts("c) Show alphabetical list of seats");
	puts("d) Assign a customer to a seat assignment");
	puts("e) Delete a seat assignment");
	puts("f) Quit");
}

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');   
        if (find)                 
            *find = '\\0';         
        else
            while (getchar() != '\\n')
                continue;         
    }
    return ret_val;
}

void Emt_Num(struct member * p)
{
	int i, count;
	
	for ( i = 0; i < MMAX; i++)
		if ( p[i].reserve == 1)
			count++;
	printf("There are %d empty seats!\\n\\n\\n", MMAX - count );
}

void Emt_Seats(struct member * p)
{
	int i;
	
	for ( i = 0; i < MMAX; i++)
	{
		if ( p[i].reserve == 0 )
			printf("Seat %d is empty!\\n", i + 1);
	}
		
	puts("\\n");	 
}

void Ex_Seats(struct member p[MMAX])
{
	int i, j;
	struct member tmp;
	
	printf("\\n");
	for ( i = 0; i < MMAX - 1; i++ )
		if ( p[i].reserve == 1 )				//加了判断只排序预定的座位 
		{
			for ( j = i + 1; j < MMAX; j++)
			{
				if (p[j].reserve == 1)
				{
//					printf("%s %d       ", p[i].first, p[i].reserve);
					if (p[i].first[0] > p[j].first[0])
					{
						tmp = p[i];
						p[i] = p[j];
						p[j] = tmp;
					}
				}		 
			}		
			printf("%d %s %s\\n", p[i].num, p[i].first, p[i].last);	
		}	
	if (p[i].reserve == 1)
		printf("%d %s %s\\n", p[i].num, p[i].first, p[i].last);//i正处于最后一位 前面迭代没有到这位

	puts("\\n");	
} 

int Find_Emt(struct member * p)
{
	int i;
	
	for ( i = 0; i < MMAX; i++)
		if ( p[i].reserve == 0 )
			return i;
}

void Set_Seats(struct member * p)
{
	int index;
	
	index = Find_Emt(p);
	puts("Enter the first name.");
    puts("Press [enter] at the start of a line to stop.");
	while ( index < MMAX && s_gets(p[index].first, NMAX) 
				&& p[index].first[0] != '\\0')
	{
		puts("Enter the last name");
		s_gets(p[index].last, NMAX);
		p[index].reserve = 1;
		puts("Enter the next first name");
		index = Find_Emt(p);
	}
	puts("\\n") ;
}

void Del_Seats(struct member * p)
{
	int n, i;
	
	puts("Enter the tag num to delete");
	while ( scanf("%d", &n) && n < MMAX && n != 0)
	{
		while (getchar() != '\\n')
			continue;
		for (i = 0; i < MMAX; i++)		//找了一早上bug... 因为每次c功能会把数组下标打乱 
			if (p[i].num == n)			//于是这边设置删除下标就会设错 本来应该传递数组进c功能 
				p[i].reserve = 0;		//但写法应该是已经是传递结构创建副本 但好像没用 
//		p[n - 1].reserve = 0;
		puts("Enter the tag num to delete(Enter 0 to quit)");
	}
	puts("\\n"); 
}

CH14 Code answer 9:

#include <stdio.h>
#include <string.h>

#define NMAX 10
#define MMAX 12
#define PLN 4

int plane[PLN] = {102, 311, 444, 519};

struct member{
	int num;
	int reserve;
	char first[NMAX];
	char last[NMAX];
};

char * s_gets(char * st, int n);
void Print_imf(int flight);
void Emt_Num(struct member * p);
void Emt_Seats(struct member * p);
void Ex_Seats(struct member p[MMAX]);
int Find_Emt(struct member * p);
void Set_Seats(struct member * p);
void Del_Seats(struct member * p);
void Print_Pln(void); 
void Confirm_Seats(struct member * p);

int main(void)
{
	char ch;
	int flight;
	struct member group[PLN][MMAX] = 
	{ 
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		},
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		},
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		},
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		}
	};
	
	Print_Pln();
	while (scanf("%d", &flight) && flight != 0)		//接受航班号 
	{
		printf("\\n");
		while (getchar() != '\\n')
			continue;
			
		if ( flight <= 4 && flight >= 1 )
		{
			Print_imf(flight);
			while ( scanf("%c", &ch) && ch != 'g' )
			{
				printf("\\n");
				while (getchar() != '\\n')
					continue;
			
				if ( ch <= 'f' && ch >= 'a' )
				{
					switch (ch)
					{
						case 'a':
							Emt_Num(group[flight - 1]);
							break;
						case 'b':
							Emt_Seats(group[flight - 1]);
							break;
						case 'c':
							Ex_Seats(group[flight - 1]);
							break;
						case 'd':
							Set_Seats(group[flight - 1]);
							break; 
						case 'e':
							Del_Seats(group[flight - 1]);
							break;
						case 'f':
							Confirm_Seats(group[flight - 1]);
							break;
					}	
				}
				else
					puts("Invalid input!");
				Print_imf(flight);
			}			
		}
		else	
			puts("Invalid input!");
		puts("\\n");
		Print_Pln();
	}
	
	printf("\\nDone!");
	
	return 0;
}

void Print_imf(int flight)
{
	printf("Current flight:%d\\n", plane[flight - 1]); 
	puts("To choose a function, enter its letter label:");
	puts("a) Show number of empty seats");
	puts("b) Show list of empty seats");
	puts("c) Show alphabetical list of seats");
	puts("d) Assign a customer to a seat assignment");
	puts("e) Delete a seat assignment");
	puts("f) Confirm seats");
	puts("g) Quit");
}

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');   
        if (find)                 
            *find = '\\0';         
        else
            while (getchar() != '\\n')
                continue;         
    }
    return ret_val;
}

void Emt_Num(struct member * p)
{
	int i, count;
	
	for ( i = 0; i < MMAX; i++)
		if ( p[i].reserve == 1)
			count++;
	printf("There are %d empty seats!\\n\\n\\n", MMAX - count );
}

void Emt_Seats(struct member * p)
{
	int i;
	
	for ( i = 0; i < MMAX; i++)
	{
		if ( p[i].reserve == 0 )
			printf("Seat %d is empty!\\n", i + 1);
	}
		
	puts("\\n");	 
}

void Ex_Seats(struct member p[MMAX])
{
	int i, j;
	struct member tmp;
	
//	for ( i = 0; i < MMAX; i++)			找不到该如何传递结构 现在都是传递指针 
//		printf("%d ", p[i].num);
	
	printf("\\n");
	for ( i = 0; i < MMAX - 1; i++ )
		if ( p[i].reserve == 1 )				//加了判断只排序预定的座位 
		{
			for ( j = i + 1; j < MMAX; j++)
			{
				if (p[j].reserve == 1)
				{
//					printf("%s %d       ", p[i].first, p[i].reserve);
					if (p[i].first[0] > p[j].first[0])
					{
						tmp = p[i];
						p[i] = p[j];
						p[j] = tmp;
					}
				}		 
			}		
			printf("%d %s %s\\n", p[i].num, p[i].first, p[i].last);	
		}	
	if (p[i].reserve == 1)
		printf("%d %s %s\\n", p[i].num, p[i].first, p[i].last);//i正处于最后一位 前面迭代没有到这位

	puts("\\n");	
} 

int Find_Emt(struct member * p)
{
	int i;
	
	for ( i = 0; i < MMAX; i++)
		if ( p[i].reserve == 0 )
			return i;
}

void Set_Seats(struct member * p)
{
	int index;
	
	index = Find_Emt(p);
	puts("Enter the first name.");
    puts("Press [enter] at the start of a line to stop.");
	while ( index < MMAX && s_gets(p[index].first, NMAX) 
				&& p[index].first[0] != '\\0')
	{
		puts("Enter the last name");
		s_gets(p[index].last, NMAX);
		p[index].reserve = 1;
		puts("Enter the next first name");
		index = Find_Emt(p);
	}
	puts("\\n") ;
}

void Del_Seats(struct member * p)
{
	int n, i;
	
	puts("Enter the tag num to delete");
	while ( scanf("%d", &n) && n < MMAX && n != 0)
	{
		while (getchar() != '\\n')
			continue;
		for (i = 0; i < MMAX; i++)		//找了一早上bug... 因为每次c功能会把数组下标打乱 
			if (p[i].num == n)			//于是这边设置删除下标就会设错 本来应该传递数组进c功能 
				p[i].reserve = 0;		//但写法应该是已经是传递结构创建副本 但好像没用 
//		p[n - 1].reserve = 0;
		puts("Enter the tag num to delete(Enter 0 to quit)");
	}
	puts("\\n"); 
}

void Print_Pln(void)
{
	puts("Select your flight number(Enter 0 to quit):");
	puts("1) 102          2) 311");
	puts("3) 444          4) 519");
}

void Confirm_Seats(struct member * p)
{
	int i, j;
	
	printf("Serial     Reserve     FirstN     LastN\\n");
	for ( i = 0; i < MMAX; i++)
		for ( j = 0; j < MMAX; j++)
			if (p[j].num == i)
			{
				printf("%-7d    %-7d     %-6s     %-5s\\n", p[j].num, p[j].reserve, 
					p[j].first, p[j].last);
				break;
			}
	puts("\\n");			
}

CH14 Code answer 10:

#include <stdio.h>
#include <string.h>

#define NMAX 10
#define MMAX 12
#define PLN 4

typedef void (*V_FP_STRUCTP)(struct member *);

int plane[PLN] = {102, 311, 444, 519};

struct member{
	int num;
	int reserve;
	char first[NMAX];
	char last[NMAX];
};

char * s_gets(char * st, int n);
void Print_imf(int flight);
void Emt_Num(struct member * p);
void Emt_Seats(struct member * p);
void Ex_Seats(struct member * p);
int Find_Emt(struct member * p);
void Set_Seats(struct member * p);
void Del_Seats(struct member * p);
void Print_Pln(void); 
void Confirm_Seats(struct member * p);

int main(void)
{
	char ch;
	int flight;
	V_FP_STRUCTP arpf[6] = {Emt_Num, Emt_Seats, Ex_Seats, Set_Seats, Del_Seats, Confirm_Seats};
	struct member group[PLN][MMAX] = 
	{ 
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		},
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		},
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		},
		{
			{ 1, 1, "Po", "Zeep" },
			{ 2, 0, "C", "Zz" },
			{ 3, 1, "Tao", "Tao" },
			{ 4, 0, "Lan", "N" }, 
			{ 5, 0, "Zhu", "Hao" },
			{ 6, 1, "Ma", "C" },
			{ 7, 0, "W", "P" },
			{ 8, 0, "Hu", "Rui" },
			{ 9, 1, "C", "C" },
			{ 10, 1, "Yan", "CQ" },
			{ 11, 0, "Z", "Z" },
			{ 12, 1, "Q", "J" }
		}
	};
	
	Print_Pln();
	while (scanf("%d", &flight) && flight != 0)		//接受航班号 
	{
		printf("\\n");
		while (getchar() != '\\n')
			continue;
			
		if ( flight <= 4 && flight >= 1 )
		{
			Print_imf(flight);
			while ( scanf("%c", &ch) && ch != 'g' )
			{
				printf("\\n");
				while (getchar() != '\\n')
					continue;
			
				if ( ch <= 'f' && ch >= 'a' )
				{
					switch (ch)
					{
						case 'a':
							arpf[0](group[flight - 1]);
							break;
						case 'b':
							arpf[1](group[flight - 1]);
							break;
						case 'c':
							arpf[2](group[flight - 1]);
							break;
						case 'd':
							arpf[3](group[flight - 1]);
							break; 
						case 'e':
							arpf[4](group[flight - 1]);
							break;
						case 'f':
							arpf[5](group[flight - 1]);
							break;
					}	
				}
				else
					puts("Invalid input!");
				Print_imf(flight);
			}			
		}
		else	
			puts("Invalid input!");
		puts("\\n");
		Print_Pln();
	}
	
	printf("\\nDone!");
	
	return 0;
}

void Print_imf(int flight)
{
	printf("Current flight:%d\\n", plane[flight - 1]); 
	puts("To choose a function, enter its letter label:");
	puts("a) Show number of empty seats");
	puts("b) Show list of empty seats");
	puts("c) Show alphabetical list of seats");
	puts("d) Assign a customer to a seat assignment");
	puts("e) Delete a seat assignment");
	puts("f) Confirm seats");
	puts("g) Quit");
}

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');   
        if (find)                 
            *find = '\\0';         
        else
            while (getchar() != '\\n')
                continue;         
    }
    return ret_val;
}

void Emt_Num(struct member * p)
{
	int i, count;
	
	for ( i = 0; i < MMAX; i++)
		if ( p[i].reserve == 1)
			count++;
	printf("There are %d empty seats!\\n\\n\\n", MMAX - count );
}

void Emt_Seats(struct member * p)
{
	int i;
	
	for ( i = 0; i < MMAX; i++)
	{
		if ( p[i].reserve == 0 )
			printf("Seat %d is empty!\\n", i + 1);
	}
		
	puts("\\n");	 
}

void Ex_Seats(struct member * p)
{
	int i, j;
	struct member tmp;
	
//	for ( i = 0; i < MMAX; i++)			找不到该如何传递结构 现在都是传递指针 
//		printf("%d ", p[i].num);
	
	printf("\\n");
	for ( i = 0; i < MMAX - 1; i++ )
		if ( p[i].reserve == 1 )				//加了判断只排序预定的座位 
		{
			for ( j = i + 1; j < MMAX; j++)
			{
				if (p[j].reserve == 1)
				{
//					printf("%s %d       ", p[i].first, p[i].reserve);
					if (p[i].first[0] > p[j].first[0])
					{
						tmp = p[i];
						p[i] = p[j];
						p[j] = tmp;
					}
				}		 
			}		
			printf("%d %s %s\\n", p[i].num, p[i].first, p[i].last);	
		}	
	if (p[i].reserve == 1)
		printf("%d %s %s\\n", p[i].num, p[i].first, p[i].last);//i正处于最后一位 前面迭代没有到这位

	puts("\\n");	
} 

int Find_Emt(struct member * p)
{
	int i;
	
	for ( i = 0; i < MMAX; i++)
		if ( p[i].reserve == 0 )
			return i;
}

void Set_Seats(struct member * p)
{
	int index;
	
	index = Find_Emt(p);
	puts("Enter the first name.");
    puts("Press [enter] at the start of a line to stop.");
	while ( index < MMAX && s_gets(p[index].first, NMAX) 
				&& p[index].first[0] != '\\0')
	{
		puts("Enter the last name");
		s_gets(p[index].last, NMAX);
		p[index].reserve = 1;
		puts("Enter the next first name");
		index = Find_Emt(p);
	}
	puts("\\n") ;
}

void Del_Seats(struct member * p)
{
	int n, i;
	
	puts("Enter the tag num to delete");
	while ( scanf("%d", &n) && n < MMAX && n != 0)
	{
		while (getchar() != '\\n')
			continue;
		for (i = 0; i < MMAX; i++)		//找了一早上bug... 因为每次c功能会把数组下标打乱 
			if (p[i].num == n)			//于是这边设置删除下标就会设错 本来应该传递数组进c功能 
				p[i].reserve = 0;		//但写法应该是已经是传递结构创建副本 但好像没用 
//		p[n - 1].reserve = 0;
		puts("Enter the tag num to delete(Enter 0 to quit)");
	}
	puts("\\n"); 
}

void Print_Pln(void)
{
	puts("Select your flight number(Enter 0 to quit):");
	puts("1) 102          2) 311");
	puts("3) 444          4) 519");
}

void Confirm_Seats(struct member * p)
{
	int i, j;
	
	printf("Serial     Reserve     FirstN     LastN\\n");
	for ( i = 0; i < MMAX; i++)
		for ( j = 0; j < MMAX; j++)
			if (p[j].num == i)
			{
				printf("%-7d    %-7d     %-6s     %-5s\\n", p[j].num, p[j].reserve, 
					p[j].first, p[j].last);
				break;
			}
	puts("\\n");			
}

CH14 Code answer 11:

#include <stdio.h>
#include <math.h>

typedef double (*SIN)(double);

#define MAX 100

void TransForm(double *, double *, int, SIN fp);

int main(void)
{
	int i, j;
	double source[MAX];
	double target[MAX];
	
	for ( i = 0; i < MAX; i++)		//随便初始化 
		source[i] = i;
	
	for ( i = 0; i < 4; i++)
	{
		printf("The %d time:\\n", i + 1);
		TransForm(source, target, MAX, sin);
	}
	
	return 0;
}

void TransForm(double * source, double * target, int n, SIN fp)
{
	int i;
	
	for ( i = 0; i < n; i++)
	{
		target[i] = fp(source[i]);
		printf("%-5.2lf ", target[i]);
		if ( (i + 1) % 10 == 0 )
			printf("\\n");
	}
		
}

以上是关于C Primer Plus(第六版)第十四章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章

C Primer Plus(第六版)第十五章 编程练习答案

C Primer Plus(第六版)第十二章 编程练习答案

C Primer Plus(第六版)第十一章 编程练习答案

C Primer Plus(第六版)第十七章 编程练习答案

C Primer Plus(第六版)第十三章 编程练习答案

C Primer Plus(第六版)第十六章 编程练习答案