C PRIMER PLUS 第14章 第9题

#include<stdio.h>
#include<string.h>
#define NAMEL 21
#define SEATS 12
struct giant {
    int seat_number;
    _Bool is_ordered;
    char fname[NAMEL];
    char lname[NAMEL];
};
void showmenu(void);
void eatline(void);
char* s_gets(char* st, int n);
int main(void)
{
    char choice;
    int i, empty_seats_count = 0, assign_number,airline;
    struct giant plane[SEATS];
    struct giant airline102[SEATS];
    struct giant airline311[SEATS];
    struct giant airline444[SEATS];
    struct giant airline519[SEATS];
    struct giant* pts[SEATS];
    struct giant* temp;
    for (i = 0; i < SEATS; i++)
    {
        plane[i].is_ordered = 0;
        plane[i].fname[0] = '\0';
        plane[i].lname[0] = '\0';
        plane[i].seat_number = i + 1;
        airline102[i] = plane[i];
        airline311[i] = plane[i];
        airline444[i] = plane[i];
        airline519[i] = plane[i];
    }
    printf("Input the number of the airline to select it(input 0 to quit):");
    while ((scanf_s("%d", &airline) == 1) && airline != 0)
    {
        eatline();
        switch (airline)
        {
        case 0:
            goto quit;
        case 102:
            for (i = 0; i < SEATS; i++)
                pts[i] = &airline102[i];
            break;
        case 311:
            for (i = 0; i < SEATS; i++)
                pts[i] = &airline311[i];
            break;
        case 444:
            for (i = 0; i < SEATS; i++)
                pts[i] = &airline444[i];
            break;
        case 519:
            for (i = 0; i < SEATS; i++)
                pts[i] = &airline519[i];
            break;
        }
        printf("Now you are operating airline%d.\n", airline);
        showmenu();
        while ((scanf("%c", &choice) == 1) && choice != 'f')
        {
            eatline();
            switch (choice)
            {
            case 'a':
                printf("Number of empty seats:\n");
                for (i = 0; i < SEATS; i++)
                    if (pts[i]->is_ordered == 0)
                        empty_seats_count++;
                printf("There are %d empty seats.\n", empty_seats_count);
                empty_seats_count = 0;
                break;
            case 'b':
                printf("List of empty seats:\n");
                for (i = 0; i < SEATS; i++)
                    if (pts[i]->is_ordered == 0)
                        printf("Seat number %2d.\n", pts[i]->seat_number);
                break;
            case 'c':
                printf("Alphabetical list of seats depend on customer's first name:\n");
                for (i = 0; i < SEATS - 1; i++)
                    for (int j = i + 1; j < SEATS; j++)
                        if ((strcmp(pts[i]->fname, pts[j]->fname)) > 0)
                        {

                            temp = pts[i];
                            pts[i] = pts[j];
                            pts[j] = temp;
                        }
                for (i = 0; i < SEATS; i++)
                    if (pts[i]->is_ordered == 1)
                        printf("%s %s:seat number %2d.\n", pts[i]->fname,
                            pts[i]->lname, pts[i]->seat_number);
                break;
            case 'd':
                printf("Input the seat number you wanna assign:");
                scanf_s("%d", &assign_number);
                eatline();
                for (i = 0; i < SEATS; i++)
                {
                    if (pts[i]->seat_number == assign_number)
                    {
                        printf("Input your first name:");
                        s_gets(pts[i]->fname, NAMEL);
                        printf("Input your last name:");
                        s_gets(pts[i]->lname, NAMEL);
                        pts[i]->is_ordered = 1;
                    }
                }
                break;
            case 'e':
                printf("Input the seat number you wanna delete assignment:");
                while (scanf_s("%d", &assign_number) != 1 || assign_number < 1 || assign_number>12)
                    printf("Input seat number from 1 to 12");
                eatline();
                for (i = 0; i < SEATS; i++)
                {
                    if (pts[i]->seat_number == assign_number)
                    {
                        pts[i]->is_ordered = 0;
                        pts[i]->fname[0] = '\0';
                        pts[i]->lname[0] = '\0';
                    }
                }
                break;
            case 'f':;
            case 'g':
                printf("Confirm seat assignment:\n");
                for (i = 0; i < SEATS; i++)
                {
                    if (pts[i]->is_ordered == 1)
                        printf("%s %s:seat number %2d.\n", pts[i]->fname,
                            pts[i]->lname, pts[i]->seat_number);
                }
                break;
            default:
                printf("Input a,b,c,d,e,f or g to seltect.\n");
                continue;
            }
            showmenu();
            printf("Now you are operating airline%d.\n", airline);
        }
        printf("Input the number of the airline to select it(input 0 to quit):");
    }
    quit:return 0;
}
void showmenu(void)
{
    printf("To choose a function, enter its letter label :\n"
        "a) Show number of empty seats\nb) Show list of empty seats\n"
        "c) Show alphabetical list of seats\nd) Assign a customer to a seat assignment\n"
        "e) Delete a seat assignment\nf) Quit\ng) Confirm the seat assignment\n");
}
void eatline(void)
{
    while (getchar() != '\n')
        continue;
}
char* s_gets(char* st, int n)
{
    char* ret_val, * find;
    if (ret_val = fgets(st, n, stdin))
        if (find = strchr(st, '\n'))
            *find = '\0';
        else
            while (getchar() != '\n')
                continue;
    return ret_val;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值