C Primer Plus(第五版) 第十四章 第八题



删除操作还没有做好,有些小地方不知道如何处理了,可能需要看一些开源项目来理解如何进行字符串操作。

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

#define MAX_char 20
#define NUMBER 12//number of seat

struct seat
{
 int number;//0~11
 int used;//0~1
 char First[MAX_char];//20
 char Last[MAX_char];//20
};
struct airplane
{
 struct seat seats[NUMBER];
};

char menu();
void init(struct airplane *a);
void show_empty(struct airplane *a);
int cal_empty(struct airplane *a);
void show_alp(struct airplane a);
void add_customer(struct airplane *a,int pos);
void delet_seat(struct airplane *a);
int b[12]={0,1,2,3,4,5,6,7,8,9,10,11};//seats

int cmp1(const void *a,const void *b)
{
 return strcmp(((struct seat*)a)->First,((struct seat *)b)->First);
}

void eatline()
{
 while(getchar()!='\n')
  continue;
}
int get_int(void)
{
 int a;
 char ch;
 while(scanf("%d",&a)!=1||a<0||a>11)
 {
  while((ch=getchar())!='\n')
   putchar(ch);
  puts(" is not correct input");
  puts("Please input the integer in the scope 0~11.");
 }
 eatline();
 return a;
}

int main(int argc, char **argv)
{
 //printf("hello world\n");
 char choose;
 struct airplane Planes;
 FILE *in;
 int i=0;//0~11
 int filecount=0;
 
 init(&Planes);//initial
 if((in=fopen("seats.dat","a+b"))==NULL)
 {
  fputs("Can't open the planes.dat",stderr);
  exit(1);
 }
 //rewind(in);
 while(i<NUMBER&&fread(&(Planes.seats[i]),sizeof(struct seat),1,in)==1)
 {
  if(i==0) puts("Here is current state of seats in airplane:");
  
  if(Planes.seats[i].used==1) printf("Number:%-2d Used:%-2d Customer %-10s %-10s\n",Planes.seats[i].number,Planes.seats[i].used,Planes.seats[i].First,Planes.seats[i].Last);
  i++;
 }
 filecount=i;
 while(1)
 {
  choose=menu();
  if(choose=='a') cal_empty(&Planes);
  if(choose=='b') show_empty(&Planes);
  if(choose=='c') show_alp(Planes);
  if(choose=='d')
  {
   add_customer(&Planes,i);
   //printf("%s %s No:%d Used:%d\n",Planes.seats[i].First,Planes.seats[i].Last,Planes.seats[i].number,Planes.seats[i].used);   
   
   fwrite(&(Planes.seats[i]),sizeof(struct seat),1,in);
   i++;
  }
  if(choose=='e') delet_seat(&Planes);
  if(choose=='f') { break;}
  //printf("you choose the choice:%c\n",choose);
  puts("\n");
 }
 /*if(i>0)
 {
  int index;
  puts("Here is the list of your plane:");
  for(index=0;index<12;index++)
   printf("%s %s No:%d Used:%d\n",Planes.seats[index].First,Planes.seats[index].Last,Planes.seats[index].number,Planes.seats[index].used);
  fwrite(&(Planes.seats[filecount]),sizeof(struct seat),i-filecount,in);
 }*/
 fclose(in);
 return 0;
}
void add_customer(struct airplane *a,int pos)
{
 int remain=cal_empty(a);
 show_empty(a);
 int sign_end=0;//0 or 1
 puts("Which seat do you want to choose? ");
 struct seat save;
 save.number=get_int();
 puts("Please input your First name");
 gets(save.First);
 puts("Please input your Last name");
 gets(save.Last);
 puts("Do you want to save the information? Please press 'y' to confirm.");
 if(tolower(getchar())=='y')
 { save.used=1;
  //int pos=save.number;
  a->seats[pos].number=save.number;
  strcpy(a->seats[pos].First,save.First);
  strcpy(a->seats[pos].Last,save.Last);
  a->seats[pos].used=save.used;
 }
 else
  save.used=0;
 eatline();
 //printf("%s %s No:%d Used:%d\n",save.First,save.Last,save.number,save.used);
}
void delet_seat(struct airplane *a)
{
 int pos,i=0;
 show_alp(*a);
 puts("which postion do you want to delete?");
 scanf("%d",&pos);
 eatline();
 while(i<NUMBER)
 {
  if(a->seats[i].number==pos)
   {a->seats[i].used=0;break;}
  i++;
 }
 
}
void init(struct airplane *a)
{
 int i=0;
 
 while(i<NUMBER)
 {
  a->seats[i].number=i;
  a->seats[i].used=0;
  strcpy(a->seats[i].First," ");
  strcpy(a->seats[i].Last," ");
  //printf("%s %s number: %d used:%d\n",a->seats[i].Last,a->seats[i].First,a->seats[i].number,a->seats[i].used);
  i++;
 }
}
void show_alp(struct airplane a)
{
 qsort(a.seats,12,sizeof(struct seat),cmp1);
 int i=0;
 while(i<NUMBER)
 {
  if(a.seats[i].used==1)
   printf("%-10s %-10s NO:%-.2d\n",a.seats[i].First,a.seats[i].Last,a.seats[i].number);
  i++;
 }
 
}
int cal_empty(struct airplane *a)
{
 int i=0;
 int count=0;
 while(i<NUMBER)
 {
  if(a->seats[i].used==0)
   count++;
  i++;
 }
 printf("The empty seats in this plane:%d\n",count);
 return count;
}
void show_empty(struct airplane *a)
{
 int i=0;
 while(i<NUMBER)
 {
  if(i==0) puts("The Empty Number of this plane: ");
  if(a->seats[i].used==1) b[a->seats[i].number]=-1;
  i++;
 }
 for(i=0;i<NUMBER;i++)
  if(b[i]!=-1) printf("%d ",b[i]);
  
 int b[12]={0,1,2,3,4,5,6,7,8,9,10,11};
 printf("\n");
}
char menu()
{
 char ans;
 puts("To choose a function,enter its letter label:");
 puts("a)Show number of empty seats");
 puts("b)Show list of empty");
 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");
 ans=tolower(getchar());
 eatline();
 while(strchr("abcdef",ans)==NULL)
 {
  puts("Please input the one of 'a,b,c,d,e,f'");
  ans=tolower(getchar());
  eatline();
 }
 return ans;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值