/*巨人航空公司的的机群是有每架容量为12个座位的飞机组成的 */
/*编写一个订票程序 */
/*声明一个数据结构,结构模板成员是 标示座位的号码,座位是否分配出去的判断标记,预定者的姓名*/
/*程序需要有个交互式界面,有操作者选择所需要的操作 */
/*1 选择按号码顺序输出所有空座位 2 输出所有空座位 3 按字母顺序输出座位情况 4 为客户分配座位 */
/*5 取消一个预定 6 退出 */
/*任何一个选项都应该允许客户选择退出系统 */
/*菜单式循环显示的,尤其是在执行完一个功能后 */
/*每次运行程序都要把程序的数据保存到一个文件中,而每次打开程序都要加载该文件 */
/*选择文件头,因为需要打开文件并且要判断文件打开是否成功,如不成功将退出程序 */
#include<stdio.h>
#include<stdlib.h>
#define N 12
#define S 20
#define L "**************************"
struct name
{
char fn[S];
char ln[S];
};
struct plane
{
int cod;
int t_f;
struct name customer;
};
char showmenu(void); /*文件菜单 */
char showemptynumber(struct plane *); /*输出一个空座位的号码*/
char showemptylist(struct plane *,int); /*输出所有空座位的列表*/
char showalphabeticallist(struct plane *,int); /*按字符顺序输出座位 */
char assigncustomer(struct plane *,int); /*座位的预定操作 */
char deleteaseat(struct plane *,struct name *); /*取消一个座位的预定 */
int main(void)
{
struct plane pl[N]; /*声明一个结构数组,储存座位信息*/
char ch; /*声明一个字符变量用于判断操作 */
FILE *fp; /*声明一个文件指针 */
int i=0;
struct name t;
if((fp=fopen("tx.txt","r"))==NULL)/*打开数据文件*/
{
puts("Can't open file !");
exit(1);
}
for(i=0;i<N;i++)
{
fscanf(fp,"%d%d%s%s",
&pl[i].cod,&pl[i].t_f,pl[i].customer.fn,pl[i].customer.ln);
}
if(fclose(fp)!=0)
puts("The file can't close");
ch=showmenu();
while(ch!='f')
{
switch(ch)
{
case 'a':ch=showemptynumber(pl);break;
case 'b':ch=showemptylist(pl,N);break;
case 'c':ch=showalphabeticallist(pl,N);break;
case 'd':
puts("Enter number for seat:");
scanf("%d",&i);
while(getchar()!='/n')
continue;
ch=assigncustomer(pl,i);
break;
case 'e':
puts("Enter customer first name:");
gets(t.fn);
puts("Enter customer last name:");
gets(t.ln);
ch=deleteaseat(pl,&t);
break;
default:puts("Input error try it again!");
ch=getchar();
while(getchar()!='/n')
continue;
break;
}
}
if((fp=fopen("tx.txt","w"))==NULL)/*打开数据文件*/
{
puts("Can't open file !");
exit(2);
}
for(i=0;i<N;i++)
{
fprintf(fp,"%d %d %s %s/n",
pl[i].cod,pl[i].t_f,pl[i].customer.fn,pl[i].customer.ln);
}
if(fclose(fp)!=0)
puts("The file can't close");
return 0;
}
char showmenu(void)
{
char cha;
printf("%s %s %s/n",L,"MENU",L);
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");
printf("%s %s %s/n",L,"MENU",L);
cha=getchar();
while(getchar()!='/n')
continue;
return cha;
}
char showemptynumber(struct plane *pp)
{
char c;
while(pp->t_f)
{
pp++;
}
printf("%d %d/n",pp->cod,pp->t_f);
c=showmenu();
return c;
}
char showemptylist(struct plane *pp,int x)
/*输出所有空座位的列表*/
{
int i;
char c;
for(i=0;i<x;i++)
{
if(!(pp->t_f))
{
printf("%d %d/n",pp->cod,pp->t_f);
}
pp++;
}
c=showmenu();
return c;
}
char showalphabeticallist(struct plane *pp,int x)
/*按字符顺序输出座位 */
{
int i,j;
char c;
struct plane temp;
for(i=0;i<x-1;i++)
{
for(j=i+1;j<x;j++)
{
if(strcmp((pp+i)->customer.fn,(pp+j)->customer.fn)>0)
{
temp=*(pp+i);
*(pp+i)=*(pp+j);
*(pp+j)=temp;
}
}
}
for(i=0;i<x;i++)
{
printf("%d %d %s %s/n",
pp->cod,pp->t_f,pp->customer.fn,pp->customer.ln);
pp++;
}
c=showmenu();
return c;
}
char assigncustomer(struct plane *pp,int x)
/*座位的预定操作 */
{
char c;
while(1)
{
if(pp->cod==x)
{
puts("please enter customer first name");
gets(pp->customer.fn);
puts("please enter customer last name");
gets(pp->customer.ln);
pp->t_f=1;
break;
}
pp++;
}
c=showmenu();
return c;
}
char deleteaseat(struct plane *pp,struct name *x)
/*取消一个座位的预定 */
{
char c;
while(1)
{
if(strcmp(pp->customer.fn,x->fn)==0&&strcmp(pp->customer.ln,x->ln)==0)
{
strcpy(pp->customer.fn,"ooo");
strcpy(pp->customer.ln,"ooo");
pp->t_f=0;
break;
}
pp++;
}
c=showmenu();
return c;
}