C Primer 第十四章 巨人航空

偷个懒 可以把第8、9、10三题合在一起做

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

struct seat 
{
    int num;
    int book_stat;
    char firstname[30];
    char lastname[30];
};

struct seat planeseats[4][12]={
                                {
                                    {1,0,"\0","\0"},
                                    {2,0,"\0","\0"},
                                    {3,1,"Alex","Zhu"},
                                    {4,1,"Peter","John"},
                                    {5,0,"\0","\0"},
                                    {6,1,"david","ding"},
                                    {7,0,"\0","\0"},
                                    {8,1,"Gloria","Wu"},
                                    {9,0,"\0","\0"},
                                    {10,0,"\0","\0"},
                                    {11,0,"\0","\0"},
                                    {12,0,"\0","\0"}
                                },
                                {
                                    {1,0,"\0","\0"},
                                    {2,0,"\0","\0"},
                                    {3,0,"\0","\0"},
                                    {4,0,"\0","\0"},
                                    {5,0,"\0","\0"},
                                    {6,0,"\0","\0"},
                                    {7,0,"\0","\0"},
                                    {8,0,"\0","\0"},
                                    {9,0,"\0","\0"},
                                    {10,0,"\0","\0"},
                                    {11,0,"\0","\0"},
                                    {12,0,"\0","\0"}
                                },
                                {
                                    {1,0,"\0","\0"},
                                    {2,0,"\0","\0"},
                                    {3,0,"\0","\0"},
                                    {4,0,"\0","\0"},
                                    {5,0,"\0","\0"},
                                    {6,0,"\0","\0"},
                                    {7,0,"\0","\0"},
                                    {8,0,"\0","\0"},
                                    {9,0,"\0","\0"},
                                    {10,0,"\0","\0"},
                                    {11,0,"\0","\0"},
                                    {12,0,"\0","\0"}
                                },
                                {
                                    {1,0,"\0","\0"},
                                    {2,0,"\0","\0"},
                                    {3,0,"\0","\0"},
                                    {4,0,"\0","\0"},
                                    {5,0,"\0","\0"},
                                    {6,0,"\0","\0"},
                                    {7,0,"\0","\0"},
                                    {8,0,"\0","\0"},
                                    {9,0,"\0","\0"},
                                    {10,0,"\0","\0"},
                                    {11,0,"\0","\0"},
                                    {12,0,"\0","\0"}
                                }
                               };
void show_empty_seats();
void show_passengerlist();
void bookseat();
void cancel_bookedseat();
char* strTolower(char *);

char ch,f;
int flightname;

int main(void)
{
   while(1)
   {
        puts("请选择航班号(102、311、444、519)");
        while (scanf("%d",&flightname)!=1 || ( flightname!=102 && flightname!=311 && flightname!=444 && flightname!=519))
        {
            puts("没有选择正确的航班号,请重新选择");
            while(getchar()!='\n');
        }
        getchar();
        switch (flightname)
        {
            case 102: f=0;
                      break;
            case 311: f=1;
                      break;
            case 444: f=2;
                      break;
            case 519: f=3;                    
        }
        while(1)
        {
                printf("您现在选择的是%d号航班\n",flightname);
                puts("a)显示空余座位号");
                puts("b)按名字字母顺序显示已经预订的座位");
                puts("c)预订座位");
                puts("d)取消预定座位");
                puts("q)回到上一级菜单");
                ch=getchar();
                ch=tolower(ch);
                while(getchar()!='\n');
                while(strchr("abcdq",ch)==NULL)
                {
                    puts("没有输入正确的选项,请重新输入");
                    ch=getchar();
                    while(getchar()!='\n');
                }
                if (ch=='q') break;
                void (*func[])()={show_empty_seats,show_passengerlist,bookseat,cancel_bookedseat};
                func[ch-97]();
        }
   }
   return 0;
}

void show_empty_seats()
{
    printf("您选择的航班号是%d\n",flightname);
    puts("该航班中尚未预定的座位有:");
    for(int i=0;i<12;i++)
    {
        if (!planeseats[f][i].book_stat)
        printf("%d\n",planeseats[f][i].num);
    }
}

void show_passengerlist()
{  
    struct seat * p_planeseats[12];
    for (int i=0;i<12;i++)
    {
        p_planeseats[i]=&planeseats[f][i];
    }
    for (int i=0;i<11;i++)
    {
        for (int j=i+1;j<12;j++)
        {
            char *temp1 = strTolower(p_planeseats[i]->firstname);
            char *temp2 = strTolower(p_planeseats[j]->firstname);
            if (strcmp(temp1,temp2)>0)
            {
              struct seat * temp;
              temp=p_planeseats[i];
              p_planeseats[i]=p_planeseats[j];
              p_planeseats[j]=temp;
            }
            free (temp1);
            free (temp2);
        }
    }
    puts("已预定座位的乘客姓名为:");
    for (int i=0;i<12;i++)
    {
        if (p_planeseats[i]->firstname[0]!='\0')
        {
            printf("%s %s %d\n",p_planeseats[i]->firstname,p_planeseats[i]->lastname,p_planeseats[i]->num);
        }
    }     
}

void bookseat()
{
   struct seat book_tmp;
   int already_input=0;
   puts("请输入乘客的名:");
   scanf("%s",&book_tmp.firstname);
   getchar();
   puts("请输入乘客的姓:");
   scanf("%s",&book_tmp.lastname);
   getchar();
   puts("请输入想预订的座位号:");
   while(1)
   {
        while(!already_input)
        {
            while(scanf("%d",&book_tmp.num)!=1||book_tmp.num<1||book_tmp.num>12)
            {
                while(getchar()!='\n');
                puts("不是有效的座位号");
            }
            while(getchar()!='\n');
            if (!planeseats[f][book_tmp.num-1].book_stat)
            break;
            else 
            puts("座位已被预订,请重新输入:");
        }
        printf("%s %s,您预订的座位是%d航班的第%d号座位,Y)确认 R)重选座位号 C)回到上一级菜单",book_tmp.firstname,book_tmp.lastname,flightname,book_tmp.num);
        char ch=tolower(getchar());
        while(getchar()!='\n');
        if (ch=='y')
        {
            planeseats[f][book_tmp.num-1]=book_tmp;
            planeseats[f][book_tmp.num-1].book_stat=1;
            break;
        }
        else if (ch=='r')
        {
            puts("请输入想预订的座位号:");
            continue;
        }
        else if (ch=='c')
            break;
        else 
        { 
            puts("无此选项请重输");
            already_input=1;
        }
    return;
    }
}

void cancel_bookedseat()
{
    int book_delete_tmp;
    puts("请问需要解除预订的座位号,退出请按Q");
    while(scanf("%d",&book_delete_tmp)!=1 || book_delete_tmp>12 ||book_delete_tmp<1 || planeseats[f][book_delete_tmp-1].num==0)
    {
        if (tolower(getchar())=='q') 
        {
            while(getchar()!='\n');
            return;
        }
        while(getchar()!='\n');
        puts("您输入的座位号不正确或并未预订,请重新输入:");
    }
    while(getchar()!='\n');
    planeseats[f][book_delete_tmp-1].book_stat=0;
    planeseats[f][book_delete_tmp-1].firstname[0]='\0';
    planeseats[f][book_delete_tmp-1].lastname[0]='\0';
    printf("%d号座位的预订已经取消\n",book_delete_tmp);
}

char * strTolower(char * temp)
{
   char *p;
   int i=0;
   p=(char*)malloc(256*sizeof(char));
   strcpy(p,temp);
   while(*p!='\0')
   {
       *p=tolower(*p);
       p++;
       i++;
   }
   return p-i;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值