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

举报
嵌入式与Linux那些事 发表于 2022/03/28 22:37:19 2022/03/28
【摘要】 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ture 1 #define f...
#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;
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296

文章来源: blog.csdn.net,作者:嵌入式与Linux那些事,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_16933601/article/details/103642840

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。