C Primer 第十四章 巨人航空

C Primer 第十四章 巨人航空,第1张

C Primer 第十四章 巨人航空

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

#include 
#include 
#include 
#include 

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

struct seat planeseats[4][12]={
                                {
                                    {1,0,"",""},
                                    {2,0,"",""},
                                    {3,1,"Alex","Zhu"},
                                    {4,1,"Peter","John"},
                                    {5,0,"",""},
                                    {6,1,"david","ding"},
                                    {7,0,"",""},
                                    {8,1,"Gloria","Wu"},
                                    {9,0,"",""},
                                    {10,0,"",""},
                                    {11,0,"",""},
                                    {12,0,"",""}
                                },
                                {
                                    {1,0,"",""},
                                    {2,0,"",""},
                                    {3,0,"",""},
                                    {4,0,"",""},
                                    {5,0,"",""},
                                    {6,0,"",""},
                                    {7,0,"",""},
                                    {8,0,"",""},
                                    {9,0,"",""},
                                    {10,0,"",""},
                                    {11,0,"",""},
                                    {12,0,"",""}
                                },
                                {
                                    {1,0,"",""},
                                    {2,0,"",""},
                                    {3,0,"",""},
                                    {4,0,"",""},
                                    {5,0,"",""},
                                    {6,0,"",""},
                                    {7,0,"",""},
                                    {8,0,"",""},
                                    {9,0,"",""},
                                    {10,0,"",""},
                                    {11,0,"",""},
                                    {12,0,"",""}
                                },
                                {
                                    {1,0,"",""},
                                    {2,0,"",""},
                                    {3,0,"",""},
                                    {4,0,"",""},
                                    {5,0,"",""},
                                    {6,0,"",""},
                                    {7,0,"",""},
                                    {8,0,"",""},
                                    {9,0,"",""},
                                    {10,0,"",""},
                                    {11,0,"",""},
                                    {12,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("您选择的航班号是%dn",flightname);
    puts("该航班中尚未预定的座位有:");
    for(int i=0;i<12;i++)
    {
        if (!planeseats[f][i].book_stat)
        printf("%dn",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]!='')
        {
            printf("%s %s %dn",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]='';
    planeseats[f][book_delete_tmp-1].lastname[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!='')
   {
       *p=tolower(*p);
       p++;
       i++;
   }
   return p-i;
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5503822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存