前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C primer plus 14章课后题--巨人航空公司问题

C primer plus 14章课后题--巨人航空公司问题

作者头像
嵌入式与Linux那些事
发布2021-05-20 15:46:03
2660
发布2021-05-20 15:46:03
举报
代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ture 1
#define false 0
#define SEATCOUNT 12
#define FLIGHTCOUNT  12
#define FNAME_LEN  50
#define LNAME_LEN  50
/*要全局声明*/
char subinput;
int topinput;

typedef struct air_info
{
    int set_num;
    bool isbook;
    char first_name[FNAME_LEN];
    char last_name[LNAME_LEN];
} air_info;
air_info air_info_arr[FLIGHTCOUNT][SEATCOUNT];
air_info (*p)[SEATCOUNT]=air_info_arr;
void init()
{
    int i = 0;
    int j = 0;

    for (i=0; i<FLIGHTCOUNT; i++)
    {
        for(j=0; j<SEATCOUNT; j++)
        {
            air_info_arr[i][j].first_name[FNAME_LEN]="E";
            air_info_arr[i][j].last_name[LNAME_LEN]="E";
            if((j%6)==0)
            {
                //方便测试
                air_info_arr[i][j].isbook=true;
            }
            else
                air_info_arr[i][j].isbook=false;
            air_info_arr[i][j].set_num=j;
        }
    }
}

void show_topmenu()
{
    printf("please choose flight:\r\n");
    printf("0) 102 \r\n");
    printf("1) 311 \r\n");
    printf("2) 444\r\n");
    printf("3) 519\r\n");
    printf("4) Quit\r\n");
}

/**
* 描述: 展示菜单
*输入:无
*返回值:无
* 作者: 飞猪
* 日期:2019-12-19
*/

void show_submenu()
{
    printf("To choose a function,enter its letter label:\r\n");
    printf("a)Show number of empty seats\r\n");
    printf("b)Show list of empty seats\r\n");
    printf("c)Show alphabetical list of seats\r\n");
    printf("d)Assign a customer to,a seat assignment\r\n");
    printf("e)Delete a seat assignment\r\n");
    printf("f)Quit\r\n");
}
/**
* 描述: 显示空座位的数量
*输入:结构体指针
*返回值:无
* 作者: 飞猪
* 日期: 2019-12-19
*
*/

int show_num(struct air_info (*p)[SEATCOUNT])
{
    // printf("测试%d\r\n",p[0].isbook);
    int j = 0;
    int count = 0;
    for(j=0; j<SEATCOUNT; j++)
    {
        if((p[topinput][j]).isbook==false)
            count++;
    }
    printf("%d航班的空座位号数量为%d\r\n",i,count);
    count = 0;
    printf("\r\n");
    main();
}
/**
* 描述: 显示详细的空座位列表
* 输入:结构体指针
* 返回值:无
* 作者: 飞猪
* 日期: 2019-12-19
**/

int show_list(struct air_info (*p)[SEATCOUNT])
{
    // printf("测试%d\r\n",p[0].isbook);
    int j = 0;
    for(j=0; j<SEATCOUNT; j++)
    {
        if((p[topinput][j]).isbook==false)
            printf("空座位号为%d\r\n",(p[topinput][j]).set_num);
    }

    printf("\r\n");
    main();

}
/**
* 描述: 根据传入的结构体指针,分配座位
*输入:结构体指针
*返回值:无
* 作者: 飞猪
* 日期:2019-12-19
*
*/
void assign_seat(struct air_info (*p)[SEATCOUNT])
{
    int j = 0;
    char ch;
    char tepm_fname[FNAME_LEN];
    char tepm_lname[LNAME_LEN];
    printf("please input your information:\r\n");
    printf("please input first name:\r\n");
    gets(tepm_fname);
    printf("please input last name:\r\n");
    gets(tepm_lname);
    for(j=0; j<SEATCOUNT; j++)
    {
        if(p[topinput][j].isbook==false)
        {
            printf("your seat number is %d \r\n",(p[topinput][j]).set_num);
            printf("are you sure  booking this seat? Y/N ");
            scanf(" %c", &ch);
            while (ch=='Y')
            {
                printf("your set is : %d\r\n",(p[topinput][j]).set_num);
                (p[topinput][j]).isbook=true;
                strcpy(p[topinput][j].first_name,tepm_fname);
                strcpy(p[topinput][j].last_name,tepm_lname);
                printf("your first_name is : %s\r\n",p[topinput][j].first_name);
                printf("your last_name is : %s\r\n",p[topinput][j].last_name);
                main();
            }

        }

    }

    printf("\r\n");

}
/*
*描述:根据传入的结构体指针,删除预定
*输入:结构体指针
*返回值:无
*作者:飞猪
*修改日期:2019-12-19
*/

void delete_assign(struct air_info (*p)[SEATCOUNT])
{
    char del_fir[FNAME_LEN];
    int j = 0;
    printf("please input the first name you will delete\r\n");
    gets(del_fir);
    for(j = 0; j<SEATCOUNT; j++)
    {
        if(!(strcmp((p[topinput][j]).first_name,del_fir)))
        {
            (p[topinput][j]).first_name[FNAME_LEN]="E";
            (p[topinput][j]).last_name[LNAME_LEN]="E";
            (p[topinput][j]).isbook=false;
            printf("delete",(p[topinput][j]).set_num);
            printf("delete sucess!\r\n");
            break;
        }
        else
        {
            printf("can not find the name that you input,please input again!\r\n");
        }
    }

    printf("\r\n");
    main();
}
/**
* 描述: 快速排序算法
* 输入:左区间 右区间
* 返回值:无
* 作者: 飞猪
* 日期: 2019-12-19
**/

void quick_sort(int left,int right)
{
    int a[SEATCOUNT];
    if(left>=right)
        return ;
    int i=left;
    int j=right;
    int key=a[i];
    while(i<j)
    {
        while(i<j&&key<=a[j])
            j--;
        a[i]=a[j];
        while(i<j&&key>=a[i])
            i++;
        a[j]=a[i];
    }
    a[i]=key;
    quick_sort(left,i-1);
    quick_sort(i+1,right);

}
/**
* 描述: 原题中为按字母排序(其实就是ascii码),这里修改为按座位号排序
* 输入:结构体指针
* 返回值:無
* 作者: 飞猪
* 日期: 2019-12-19
**/

void show_num_list(struct air_info (*p)[SEATCOUNT])
{
    int j = 0;
    quick_sort(p[topinput][0].set_num,p[topinput][SEATCOUNT].set_num);
    printf("seat has been sorted\r\n");
    for(j=0; j<SEATCOUNT; j++)
        printf("set_num:%d\e\n",p[topinput][j].set_num);
}


int main()
{

    topinput=0;
    init();
    show_topmenu();
    gets(&topinput);
    switch(topinput)
    {
    case'0':
            show_submenu();
        break;
    case'1':
            show_submenu();
        break;
    case'2':
            show_submenu();
        break;
    case'3':
            show_submenu();
        break;
    case'4':
            return -1 ;
        break;
    }
    printf("please input your selection: ");
    gets(&subinput);
    switch(subinput)
    {
    case'a':
            show_num(air_info_arr);
        break;
    case'b':
            show_list(air_info_arr);
        break;
    case'c':
            show_num_list(air_info_arr);
        break;
    case'd':
            assign_seat(air_info_arr);
        break;
    case'e':
            delete_assign(air_info_arr);
        break;
    case'f':
            return -1 ;
        break;
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档