C Primer Plus 第十四章 编程练习9

作者在 2013-05-18 14:35:49 发布以下内容
/*
巨人航空公司(见第8题)需要另一架飞机(同样容量)
并使它每天服务4个航班(航班102,311,444,519).
把程序扩展为能够处理4个航班.有一个顶层菜单可供选择航班和退出
选择了一个特定的航班,就会调出和第8题相似的菜单,但要加上一个新项:
确认一个座位分配
并用一个退回顶层菜单的选项代替退出选项,
每个显示要指明现在正在处理哪个航班.
座位分配显示必须要指明确认状态*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NAMELEN 10
#define SIZE 12
#define FNUMBERLEN 14
#define FLIGHT 4
struct order{
 unsigned short number;
 unsigned short flag;
 char fname[NAMELEN];
 char lname[NAMELEN];
};
struct flight_num{
	char filename[FNUMBERLEN];
};

void clearcache(void);     /*清空缓存*/
void mainmenu(void);
void showmenu(void);
void showsubmenu(int, char *, struct order *);
void initfile(struct flight_num *, struct order [][SIZE]);
void showEmptyNumber(struct order *, char *, int); /*显示空座位号码*/
void showEmptyList(struct order *, char *, int);  /*显示空座位列表*/
void showAlphaList(struct order *,  char *, int);  /*显示按字母顺序排列的座位列表*/
void addSeat(struct order *, char *, int);   /*添加一个订单*/
void confirm_seat(struct order *, char *, int);
void delSeat(struct order *, char *, int);   /*删除一个订单*/
struct order readfile(struct order *);

int main(void)
{
	struct order seat[FLIGHT][SIZE] = {0};
	struct flight_num filename[FLIGHT] = {
		"order_102.dat", "order_311.dat", "order_444.dat", "order_519.dat"
	};
	unsigned short flight[FLIGHT] = {102, 311, 444, 519};
	char ch;
	int flag;
 
	initfile(filename, seat);	/*初始化,创建数据文件*/
	mainmenu();		/*显示主菜单*/
	
	printf("Please select a flight number: ");
	while((ch = getchar()) && tolower(ch) != 'q')
	{
	 	flag = 0;
	 	clearcache();
	 	
	 	switch(tolower(ch)){
	 		case 'a':
				showsubmenu(flight[0], filename[0].filename, seat[0]);break;
		 	case 'b':
			 	showsubmenu(flight[1], filename[1].filename, seat[1]);break;
	 		case 'c':
			 	showsubmenu(flight[2], filename[2].filename, seat[2]);break;
	 		case 'd':
			 	showsubmenu(flight[3], filename[3].filename, seat[3]);break;
	 		default:flag = 1;break;
	 	}
	 	if(flag)
		{
			fprintf(stderr, "Invalid option.\n");
			printf("Try again: ");
			continue;
		}
	 
		mainmenu();
		printf("Please select a flight number: ");
 	}
 		
	printf("Bye.\n");
	return 0;
}

void initfile(struct flight_num * file, struct order seat[][SIZE])
{
	int i, j, num;
	FILE * fp;
	int size = sizeof(struct order);
	
	for(num = 0; num < FLIGHT; num++)
	{
		if((fp = fopen(file[num].filename, "rb")) == NULL)  /*如果文件不存在*/
 		{
  			if((fp = fopen(file[num].filename, "w+b")) == NULL) /*创建文件失败*/
			{
   				fprintf(stderr, "Can't create %s file.\n", file[num].filename);
   				exit(1);
  			}
  		
			for(i = 0; i < FLIGHT; i++)
				for(j = 0; j < SIZE; j++)
				{
					seat[i][j].number = j + 1;
					fwrite(&seat[i][j], size, 1, fp);
				}
		}
		fclose(fp);
	}
}

void showsubmenu(int flight, char * filename, struct order * seat)
{
	char ch;
	int flag;
	
	printf("\nWelcome to Flight %3d.\n\n", flight);
	
	showmenu();
	printf("Please select an operation: ");
	while((ch = getchar()) && tolower(ch) != 'g')
	{
	 	flag = 0;
	 	clearcache(); /*清空缓存中的字符*/
	 
	 	switch(tolower(ch)){
	 	case 'a':showEmptyNumber(seat, filename, flight);break;
	 	case 'b':showEmptyList(seat, filename, flight);break;
	 	case 'c':showAlphaList(seat, filename, flight);break;
	 	case 'd':addSeat(seat, filename, flight);break;
	 	case 'e':confirm_seat(seat, filename, flight);break;
	 	case 'f':delSeat(seat, filename, flight);break;
	 	default:flag = 1;break;
	 	}
		if(flag)
		{
			fprintf(stderr, "Invalid option.\n");
			printf("Try again: ");
			continue;
		}
		showmenu();
		printf("Please select again: ");
	}
	clearcache();
}

void showEmptyNumber(struct order * seat, char * filename, int flight) /*显示空座位号码*/
{
	int i, num = 0, count = 0;
	FILE * fp;
	int size = sizeof(struct order);
 
	if((fp = fopen(filename, "rb")) == NULL)  /*读取文件失败*/
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(1);
	}
	
	while(count < SIZE && fread(&seat[count], size, 1, fp) == 1)
	{
		if(seat[count].flag != 0)
			num++;
		count++;
	}
	
	fclose(fp);
 
	if(num == SIZE)
	{
		fprintf(stderr, "\nThe flight No.%d have not empty seat.\n\n", flight);
		return;
	}
 
	printf("\nThe flight number %d empty seat number:\n", flight);
	for(i = 0; i < SIZE; i++)
		if(seat[i].flag == 0)
			printf("%4d", seat[i].number);
	printf("\n\n");
}

void showEmptyList(struct order * seat, char * filename, int flight)  /*显示空座位列表*/
{
	int i, num = 0, count = 0;
	FILE * fp;
	int size = sizeof(struct order);
 
	if((fp = fopen(filename, "rb")) == NULL)  /*读取文件失败*/
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(1);
	}
	
	while(count < SIZE && fread(&seat[count], size, 1, fp) == 1)
	{
		if(seat[count].flag != 0)
			num++;
		count++;
	}
 
	fclose(fp);
	if(num == SIZE)
	{
		fprintf(stderr, "\nThe flight No.%d have not empty seat.\n\n", flight);
		return;
	}
 
	printf("\nThe flight number %d empty seat list:\n", flight);
	printf("\nNumber   FirstName   Lastname\n");
	for(i = 0; i < SIZE; i++)
		if(seat[i].flag == 0)
			printf("%4d\n", seat[i].number);
	printf("\n\n");
}

void showAlphaList(struct order * seat, char * filename, int flight)
{
	struct order *pst[SIZE];  /*结构指针数组*/
	struct order *tmp;
	int i, j, num = 0, count = 0;
	FILE * fp;
	int size = sizeof(struct order);
 
	if((fp = fopen(filename, "rb")) == NULL)  /*读取文件失败*/
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(1); 
	}
 
	while(count < SIZE && fread(&seat[count], size, 1, fp) == 1)
	{
		if(seat[count].flag != 0)
			num++;
		count++;
	}
 
	fclose(fp);
 
	if(num == 0)
	{
		fprintf(stderr, "\nThe flight No.%d have not order record.\n\n", flight);
		return;
	}
 
	for(i = 0; i < SIZE; i++)  /*为结构数组每个元素分配一个结构指针*/
		pst[i] = &seat[i];
 
	for(i = 0; i < SIZE - 1; i++)
		for(j = i + 1; j < SIZE; j++)
		{
			if(strcmp(pst[i]->fname,pst[j]->fname) > 0) /*比较 FIRSTNAME*/
			{
				tmp = pst[i];
				pst[i] = pst[j];
				pst[j] = tmp;
			}
			else if(strcmp(pst[i]->fname,pst[j]->fname) == 0) /*如果FNAME相同,比较LNAME*/
			{
				if(strcmp(pst[i]->lname,pst[j]->lname) > 0)
				{
					tmp = pst[i];
					pst[i] = pst[j];
					pst[j] = tmp;
				}
			}
		}
 
	printf("\nThe flight No.%d alphabetical list of seats:\n", flight);
	printf("  FirstName      Lastname     Number\n");
	for(i = 0; i < SIZE; i++)
		if(pst[i]->flag != 0)
			printf("%10s     %10s       %4d\n", pst[i]->fname, pst[i]->lname, pst[i]->number);
	printf("\n\n");

}

void addSeat(struct order * seat, char * filename, int flight)
{
	int i, count = 0, num = 0;
	int sum;
	FILE * fp;
	int size = sizeof(struct order);
 
	if((fp = fopen(filename, "r+b")) == NULL)
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(2);
	}
	
	while(count < SIZE && fread(&seat[count], size, 1, fp) == 1)
	{
		if(seat[count].flag != 0)
			num++;
		count++;
	}
	fclose(fp);
 
	if(num == SIZE)
	{
		fprintf(stderr, "\nThe flight No.%d is full of orders.\n\n", flight);
		return;
	}

	printf("\nThe flight No.%d empty seat number:\n", flight);    /*显示空座位列表*/
	for(i = 0; i < SIZE; i++)
		if(seat[i].flag == 0)
			printf("%6d", seat[i].number);
	printf("\n\n");
 
	printf("Please enter seat number(q to quit): ");
	while(scanf("%d", &sum) == 1)
	{
		clearcache();
		sum--;
  
		if(sum >= 0 && sum <= 11)      /*判断输入的数字*/
		{
			if(seat[sum].flag == 1)     /*判断是否是空座位*/
			{
				fprintf(stderr, "\nThe seats have been booked.\n");
				printf("Please enter seat number again(q to quit): ");
				continue;
			}
   
			seat[sum].flag = 1;      /*设定标记*/
   			//seat[num].number = num;     /*接受输入信息,存入结构体*/
			printf("Enter the first name: \n");
			gets(seat[sum].fname);
			printf("Enter the last name: \n");
			gets(seat[sum].lname);
   
			//fwrite(&seat;[sum], size, 1, fp);  /*将一条订单信息写入文件*/
			printf("Please enter seat number(q to quit): ");
		}
  		else
		{
			fprintf(stderr, "Invalid number.\n");
			printf("Please enter seat number again(q to quit): ");
			continue;
		}
	}/*
	if((fp = fopen(filename, "w+b")) == NULL)
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(2);
	}
	for(i = 0; i < SIZE; i++)
		fwrite(&seat[i], size, 1, fp);

	fclose(fp);*/
	clearcache();
}

void delSeat(struct order * seat, char * filename, int flight)   /*删除订单*/
{
	int i, count = 0, num = 0;
	int sum;
	char null[1] = "\0";
	FILE * fp;
	int size = sizeof(struct order);
 
	if((fp = fopen(filename, "r+b")) == NULL)
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(2);
	}
	while(count < SIZE && fread(&seat[count], size, 1, fp) == 1)
	{
		if(seat[count].flag != 0)
			num++;
		count++;
	}
	fclose(fp);
 
	if(num == 0)
	{
		fprintf(stderr, "\nThe flight No.%d have not order record.\n\n", flight);
		return;
	}
 
	printf("The flight No.%d seat order list:\n", flight);    /*显示订单列表*/
	printf("Number     FirstName     Lastname\n");
	
	for(i = 0; i < SIZE; i++)
		if(seat[i].flag != 0)
			printf("%4d     %10s     %10s\n", seat[i].number, seat[i].fname, seat[i].lname);
	printf("\n\n");
 
	printf("Please enter seat number(q to quit): ");
	while(scanf("%d", &sum) == 1)
	{
		clearcache();
		sum--;
  
		if(sum >= 0 && sum <= 11)      /*判断输入的数字*/
		{
			if(seat[sum].flag != 1)     /*判断是否是空座位*/
			{
				fprintf(stderr, "The seats have no booked.\n");
				printf("Please try again(q to quit): ");
				continue;
			}
   
			seat[sum].flag = 0;      /*设定标记*/
			//strcpy(seat[sum].fname, null);
			//strcpy(seat[sum].lname, null);
   
			printf("Deleted Order No.%d .\n", sum + 1);
			printf("enter next seat number(q to quit): ");
		}
		else
		{
			fprintf(stderr, "Invalid number.\n");
			printf("Try again(q to quit): ");
			continue;
		}
	}
	if((fp = fopen(filename, "w+b")) == NULL)
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(2);
	}
	for(i = 0; i < SIZE; i++)
		fwrite(&seat[i], size, 1, fp);
 
	clearcache();
	fclose(fp);
}

void confirm_seat(struct order * seat, char * filename, int flight)	/*确认分配座位*/
{
	int i, count = 0, num = 0;
	int sum;
	FILE * fp;
	int size = sizeof(struct order);
	
	if((fp = fopen(filename, "w+b")) == NULL)
	{
		fprintf(stderr, "Can't open %s file.\n", filename);
		exit(2);
	}
	for(i = 0; i < SIZE; i++)
		fwrite(&seat[i], size, 1, fp);
 
	//clearcache();
	fclose(fp);
	printf("\nThe seats have been allocated.\n\n");
}
void showmenu(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) Confirm the order");				/*确认分配一个座位*/
	puts("f) Delete a seat assignment");      /*删除一个订单*/
	puts("g) Return to main menu");
}

void mainmenu(void)
{
	puts("To choose a flight number, enter its letter label:");
	puts("a) Flight number 102");
	puts("b) Flight number 311");
	puts("c) Flight number 444");
	puts("d) Flight number 519");
	puts("q) Quit");
}
void clearcache(void)
{
	while(getchar() != '\n')
		continue;
}
文章评论,共0条
游客请输入验证码
浏览41416次