简单的座位预定程序

希望各位帮助指正。

能显示剩余座位,显示剩余的座位号,能登记个人信息,删除个人信息。

先把代码贴上来:

和先前的目录有很多相似之处,除了显示剩余座位号,由于我采用的是字符串形式的座位号,所以会比数字编号稍微麻烦点。

显示座位编号的代码如下:

通过strcmp()比较 :结构中记录座位编号的成员  与  字符串数组

如果结果为1,则存储在temp临时数组中。

在打印字符串数组时,如果下标为temp数组成员,则不打印。

//主函数声明了一个字符串数组,每个元素都是一个字符串。
//const char* seat_code[MAX] = { "A01","A02","B01","B02","C01","C02","D01","D02","E01","E02","F01","F02" };
void
empty_seats_list(struct airplane info[], const char** seat_code) { int i = 0, j = 0, a = 0; int temp[MAX]; //一个临时数组,记录字符串数组的下标用 if (count == 0) { puts("Here is the list of empty seat:"); puts("********"); while (i < MAX) { puts(*(seat_code + i)); i++; } puts("********"); } else { for(;i < count;i++) { for(;j<MAX;j++) if (strcmp(info[i].seat, seat_code[j]) == 0) { temp[a] = j; a++; break; } } int n = 0; int num = sizeof(temp) / sizeof(temp[0]); puts("Here is the list of empty seat:"); puts("*********"); while (n < MAX) { if (compare(temp, n, num)) { puts(seat_code[n]); n++; } else n++; } puts("*********"); } } int compare(int temp[], int n, int num) { for (int j = 0; j < num; j++) { if (n == temp[j]) return 0; else continue; } return 1; }

 

完整代码如下:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define LEN 20
# define MAX 12

struct airplane {
    int statue;
    char seat[4];
    char fname[LEN];
    char lname[LEN];
};
int count; //已售出数量

char menu();
void empty_seat_qt();            //剩余数量
void empty_seats_list(struct airplane info[], const char**);     //空座位编号列表
int compare(int[], int, int);                                      //辅助上一个函数
void show(const char**);                                        //这个功能,没有做,只是简单的打印所有座位的编号
void assign(struct airplane info[], struct airplane* pt, const char**);  //登记
void del(struct airplane info[],struct airplane*pt);         //删除
char* s_gets(char* st, int n);

int main()
{
    struct airplane info[MAX];
    struct airplane* pt;
    pt = &info[0];

    FILE* fp;
    int filecount = 0;
    int size = sizeof(struct airplane);
    const char* seat_code[MAX] = { "A01","A02","B01","B02","C01","C02","D01","D02","E01","E02","F01","F02" };


    if ((fp = fopen("Book_System.dat", "a+b")) == NULL)
    {
        fputs("Can't open the file:Book_System.dat.", stderr);
        exit(1);
    }

    while (count < MAX && fread(&info[count], size, 1, fp) == 1)
    {
        if (count == 0)
            puts("Here is the booked seats:");
        printf("Statue: %d, Seat_No:%s ,Name: %s %s.\n", info[count].statue, info[count].seat, info[count].fname, info[count].lname);
        count++;               //计算已有的结构
    }
    filecount = count;
    rewind(fp);

    char ch;
    ch = menu();
    while (ch != 'f')
    {
        while (getchar() != '\n')
            continue;
        switch (ch)
        {
        case 'a': puts("Number of empty seats:");
            empty_seat_qt();
            break;
        case 'b': empty_seats_list(info, seat_code);
            break;
        case 'c': show(seat_code);
            break;
        case 'd': assign(info, pt, seat_code);
            break;
        case 'e': del(info,pt);
        }
        puts("Enter you next moving :");
        ch = menu();
    }

    if (count > 0)
    {
        puts("Here is the list of passengers:");
        for (int index =0;index<count ;index++)
            printf("Statue :%d    Seat_No:%s   Name:%s %s .\n", info[index].statue, info[index].seat,info[index].fname, info[index].lname);
        fp= fopen("Book_System.dat", "w+b");        //就是这里,忘记改文件名了.....
        fwrite(&info[0],size,count,fp);
    }
    else
    {
        puts("No Passenger.");
        fopen("Book_System.dat", "w");
    }


    puts("Bye.");
    fclose(fp);

    return 0;
}

char menu()
{
    char c;
    puts("To choose a funcaton,enter its letter label:\n"
        "a) Show number of empty seats\n"
        "b) Show list of empty seats\n"
        "c) Show alphabetical list of seats\n"
        "d) Assign a customer to a seat assignment\n"
        "e) Delete a seat assignemnt\n"
        "f) Quit");
    if (strchr("abcdef", c = getchar()) != NULL)
        return c;
    else
    {
        while (strchr("abcdef", c) == NULL)
        {
            puts("To choose a funcaton,enter its letter label:\n"
                "a) Show number of empty seats\n"
                "b) Show list of empty seats\n"
                "c) Show alphabetical list of seats\n"
                "d) Assign a customer to a seat assignment\n"
                "e) Delete a seat assignemnt\n"
                "f) Quit");
            c = getchar();
        }
    }
    return c;
}

void empty_seat_qt()
{
    puts("****************");
    printf("%d empty seats.\n", MAX - count);
    puts("****************");
}
void empty_seats_list(struct airplane info[], const char** seat_code)
{
    int i = 0, j = 0, a = 0;
    int temp[MAX];

    if (count == 0)
    {
        puts("Here is the list of empty seat:");
        puts("********");
        while (i < MAX)
        {
            puts(*(seat_code + i));
            i++;
        }
        puts("********");
    }
    else
    {
        for(;i < count;i++)
        {
            for(;j<MAX;j++)
            if (strcmp(info[i].seat, seat_code[j]) == 0)
            {
                temp[a] = j;
                a++;
                break;
            }
        }
        int n = 0;
        int num = sizeof(temp) / sizeof(temp[0]);
        puts("Here is the list of empty seat:");
        puts("*********");
        while (n < MAX)
        {
            if (compare(temp, n, num))
            {
                puts(seat_code[n]);
                n++;
            }
            else
                n++;
        }
        puts("*********");
    }

}

int compare(int temp[], int n, int num)
{
    for (int j = 0; j < num; j++)
    {
        if (n == temp[j])
            return 0;
        else
            continue;
    }
    return 1;
}
void show(const char** seat_code)
{
    int i = 0;
    while (i < MAX)
    {
        puts(seat_code[i]);
        i++;
    }
}
void assign(struct airplane info[], struct airplane* pt, const char** seat_code)
{
    empty_seats_list(info, seat_code);
    struct airplane temp;
    temp = *pt;

    puts("Please choice your seat from the list:");
    if (count < MAX && s_gets(temp.seat, 4) != NULL && temp.seat[0] != '\0')               //可能需要修改的地方
    {
        puts("Enter your first name:");
        s_gets(temp.fname, LEN);
        puts("Enter your last name:");
        s_gets(temp.lname, LEN);
        temp.statue = 1;
    }
    pt[count] = temp;
    count++;
}

void del(struct airplane info[],struct airplane*pt)
{
    extern int count;
    int del;
    int i = 0;
    int j = 0;

    puts("Which content would you like to delete?Press q to the menu:");
    for (int index = 0; index < count; index++)
        printf("Seat_No: %s, Name:%s %s\n", info[index].seat, info[index].fname, info[index].lname);

    while (scanf("%d", &del) == 1)
    {
        while (i < count)
        {
            if (j == (del - 1))
            {
                j++;
                continue;
            }
            else
            {
                info[i] = info[j];
                pt[i] = info[i];
                i++;
                j++;
            }
        }
        count = count - 1;
        puts("Which Item you'd like to delete,press q to the menu:");
    }
    puts("What is you next moving:");
    while (getchar() != '\n')
        continue;
}
char* s_gets(char* st, int n)
{
    char* ret_val;
    char* find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
            *find = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

 

posted @ 2022-02-25 21:05  Tolerieren  阅读(158)  评论(0)    收藏  举报