知識社群登入
位置: 艾鍗學院 Blog > 專業論壇 > 討論
1樓
seatingReservation.h
-----------------------------------
#ifndef SEATING_RESERVATION_DIANZHANG
#define SEATING_RESERVATION_DIANZHANG

#define DATA_FILENAME "seat_information.bin"
#define SEATING_CAPACITY 30

typedef enum {empty, assigned} seatStatus;

typedef struct seat_information {
    int seatID;
    seatStatus marker;
    char lastName[20];
    char firstName[20];
    struct seat_information *next;
} SeatInfo;

typedef struct {
    SeatInfo *head;
} Seat;

//Read seating infomation from a data file.
int seat_readInfo(Seat *seat);

//Write seating infomation from a data file.
int seat_writeInfo(Seat *seat);

//Add an assigned seating information to the list.
void seat_addSeatInfo(Seat *seat, SeatInfo *seatinfo);

//Delete an assigned seating information from the list.
void seat_delSeatInfo(Seat *seat, SeatInfo *seatinfo);

//Get number of empty seats.
int seat_getNumEmptySeat(Seat *seat);

//Print a list of empty seats.
void seat_printEmptySeat(Seat *seat);

//Print a list of assigned seats.
void seat_printAssignedSeat(Seat *seat);

//Assign a customer to a seat assignment.
void seat_assignSeat(Seat *seat);

//Delete an assigned seat.
void seat_delSeat(Seat *seat);

//Initialize a seating infomation.
SeatInfo* seat_init(void);

//Check if a seat id had been assigned.
int seat_isNode(Seat *seat, int num);

//Get a seat information by seat id.
SeatInfo* seat_getSeatInfo(Seat *seat, int num);

#endif

seatingReservation.c
--------------------------------
#include "seatingReservation.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Read seating infomation from a data file.
int seat_readInfo(Seat *seat) {
    int i=0;
    int check=0;
    SeatInfo buffer;
    SeatInfo *seatinfo;
    FILE *fp=NULL;
   
    fp=fopen(DATA_FILENAME, "rb");
    if(!fp) {
        printf("Failed to open a data file!\n");
        return 1;
    }
   
    printf("Data file reading...\n\n");
    for(i=0; i<SEATING_CAPACITY; i++) {
        check=fread(&buffer, sizeof(SeatInfo), 1, fp);
        if(1==check) {
            seatinfo=seat_init();
            seatinfo->seatID=buffer.seatID;
            seatinfo->marker=buffer.marker;
            strcpy(seatinfo->lastName, buffer.lastName);
            strcpy(seatinfo->firstName, buffer.firstName);
            seat_addSeatInfo(seat, seatinfo);
        } else {
            break;
        }
    }
   
    fclose(fp);
    return 0;
}

//Write seating infomation from a data file.
int seat_writeInfo(Seat *seat) {
    SeatInfo *trace=NULL;
    FILE *fp=NULL;
   
    fp=fopen(DATA_FILENAME, "wb");
    if(!fp) {
        printf("Failed to open a data file!\n");
        return 1;
    }
   
    printf("Data file writing...\n\n");
    trace=seat->head;
    while(trace!=NULL) {
        fwrite(trace, sizeof(SeatInfo), 1, fp);
        trace=trace->next;
    }
   
    fclose(fp);
    return 0;
}

//Add an assigned seating information to the list by ascending.
void seat_addSeatInfo(Seat *seat, SeatInfo *seatinfo) {
    SeatInfo *trace=NULL, *pre=NULL;
   
    //No seat information.
    if(seat->head==NULL) {
        seat->head=seatinfo;
        seat->head->next=NULL;
        return;
    }
   
    trace=seat->head;
   
    //Only one seat information.
    if(trace->next==NULL) {
        if(trace->seatID > seatinfo->seatID) {
            seatinfo->next=trace;
            seat->head=seatinfo;
            return;
        } else {
            trace->next=seatinfo;
            seatinfo->next=NULL;
            return;
        }
    }
   
    //More than one seat information.
    while(trace!=NULL) {
        if(trace->seatID>seatinfo->seatID) {
            seatinfo->next=trace;
            if(trace==seat->head) {
                seat->head=seatinfo;
                return;
            }
            pre->next=seatinfo;
            return;
        } else {
            pre=trace;
            trace=trace->next;
        }
    }
    pre->next=seatinfo;
    seatinfo->next=NULL;
}

//Delete an assigned seating information from the list.
void seat_delSeatInfo(Seat *seat, SeatInfo *seatinfo) {
    SeatInfo *trace=NULL;
   
    //No seat information.
    if(seat->head==NULL) {
        return;
    }
   
    trace=seat->head;
   
    //Only one seat information.
    if(trace->next==NULL) {
        seat->head=NULL;
        free(seatinfo);
        return;
    }
   
    //More than one seat information.
    if(trace==seatinfo) {
        seat->head=trace->next;
        free(seatinfo);
        return;
    } else {
        while(trace->next!=seatinfo) {
            trace=trace->next;
        }
        trace->next=seatinfo->next;
        free(seatinfo);
    }
}

//Get number of empty seats.
int seat_getNumEmptySeat(Seat *seat) {
    int count=0;
    SeatInfo *trace=NULL;
   
    trace=seat->head;
    while(trace!=NULL) {
        count++;
        trace=trace->next;
    }
    return SEATING_CAPACITY-count;
}

//Print a list of empty seats.
void seat_printEmptySeat(Seat *seat) {
    int i=0, count=0;
    SeatInfo *trace=NULL;
   
    trace=seat->head;
    for(i=1; i<=SEATING_CAPACITY; i++) {
        if(trace!=NULL && trace->seatID==i) {
            trace=trace->next;
            continue;
        }
       
        count++;
        printf("%2d ", i);
        if(count%5==0) {
            printf("\n");
        }
    }
}

//Print a list of assigned seats.
void seat_printAssignedSeat(Seat *seat) {
    SeatInfo *trace=NULL;
   
    if(seat->head==NULL) {
        printf("0\n");
    } else {
        trace=seat->head;
        while(trace!=NULL) {
            printf("ID:%-2d Last Name:%-15s First Name:%-15s\n", \
            trace->seatID, trace->lastName, trace->firstName);
            trace=trace->next;
        }
    }
}

//Assign a customer to a seat assignment.
void seat_assignSeat(Seat *seat) {
    SeatInfo *seatinfo=NULL;
    int num=0;
    char check;
   
    seatinfo=seat_init();
   
    printf("Empty seat list:\n");
    seat_printEmptySeat(seat);
    printf("\n");
    printf("Please input a seat number you want to reserve:");
    scanf("%d", &num);
    if(num<1 || num>SEATING_CAPACITY) {
        printf("%d is an error seat id!\n", num);
        return;
    } else {
        if(seat_isNode(seat, num)) {
            printf("Sorry! The seat had been reserved.\n");
            return;
        }
       
        seatinfo->seatID=num;
        seatinfo->marker=assigned;
   
        printf("Please input your last name:");
        scanf("%s", seatinfo->lastName);
        printf("Please input your first name:");
        scanf("%s", seatinfo->firstName);
   
        printf("\n");
        printf("Please check your information again.\n");
        printf("Seat ID:%d\n", seatinfo->seatID);
        printf("Your last name:%s\n", seatinfo->lastName);
        printf("Your first name:%s\n", seatinfo->firstName);
   
        while(1) {
            printf("Do you want to reserve the seat ? (y or n)\n");
            scanf(" %c", &check);
            switch(check) {
                case 'y':
                case 'Y':
                    seat_addSeatInfo(seat, seatinfo);
                    return;
                case 'n':
                case 'N':
                    return;
                default:
                    printf("Input error!\n");
            }
        }
    } 
}

//Delete an assigned seat.
void seat_delSeat(Seat *seat) {
    SeatInfo *seatinfo=NULL;
    int num=0;
    char check;
   
    printf("Assigned seat list:\n");
    seat_printAssignedSeat(seat);
    printf("Please input a seat number you want to delete:");
    scanf("%d", &num);
    if(num<1 || num>SEATING_CAPACITY) {
        printf("%d is an error seat id!\n", num);
        return;
    } else {
        if(!seat_isNode(seat, num)) {
            printf("Sorry! The seat had not been reserved.\n");
            return;
        }
       
        seatinfo=seat_getSeatInfo(seat, num);
        printf("\n");
        printf("Please check the information again.\n");
        printf("Seat ID:%d\n", seatinfo->seatID);
        printf("Your last name:%s\n", seatinfo->lastName);
        printf("Your first name:%s\n", seatinfo->firstName);
   
        while(1) {
            printf("Do you want to delete the seat ? (y or n)\n");
            scanf(" %c", &check);
            switch(check) {
                case 'y':
                case 'Y':
                    seat_delSeatInfo(seat, seatinfo);
                    return;
                case 'n':
                case 'N':
                    return;
                default:
                    printf("Input error!\n");
            }
        }
    } 
}

//Initialize a seating infomation.
SeatInfo* seat_init(void) {
    SeatInfo *seatinfo=NULL;
   
    seatinfo=(SeatInfo*)malloc(sizeof(SeatInfo));
    if(!seatinfo) {
        printf("Failed to allocate a memory block!\n");
        exit(1);
    } else {
        return seatinfo;
    }
}

//Check if a seat id had been assigned.
int seat_isNode(Seat *seat, int num) {
    SeatInfo *trace=NULL;
   
    if(seat->head==NULL) {
        return 0;
    } else {
        trace=seat->head;
        while(trace!=NULL) {
            if(trace->seatID==num) {
                return 1;
            } else {
                trace=trace->next;
            }
        }
        return 0;
    }
}

//Get a seat information by seat id.
SeatInfo* seat_getSeatInfo(Seat *seat, int num) {
    SeatInfo *trace=NULL;
   
    trace=seat->head;
    while(trace!=NULL) {
        if(trace->seatID==num) {
            return trace;
        } else {
            trace=trace->next;
        }
    }
   
    return NULL;
}

main.c
------------------
#include "seatingReservation.h"
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char fun;
    Seat seat;
   
    seat.head=NULL;
    if(seat_readInfo(&seat)) {
        printf("Error!\n");
        exit(1);
    }
   
    while(1) {
        printf("\n");
        printf("To choose a function, enter its letter label:\n");
        printf("a) Show number of empty seats.\n");
        printf("b) Show list of empty seats.\n");
        printf("c) Show alphabetical list of seats.\n");
        printf("d) Assign a customer to a seat assignment.\n");
        printf("e) Delete a seat assignment.\n");
        printf("f) Quit.\n");
        scanf(" %c", &fun);
        switch(fun) {
            case 'a':
                printf("Number of empty seats now is: %d\n", seat_getNumEmptySeat(&seat));
                break;
            case 'b':
                printf("Empty seat list:\n");
                seat_printEmptySeat(&seat);
                break;
            case 'c':
                printf("Assigned seat list:\n");
                seat_printAssignedSeat(&seat);
                break;
            case 'd':
                seat_assignSeat(&seat);
                break;
            case 'e':
                seat_delSeat(&seat);
                break;
            case 'f':
                seat_writeInfo(&seat);
                return 0;
            default:
                printf("Input error!\n");
        }
    }
}
2樓
char user_select (void)
{
    int ch;

    ch = getchar();
    while (getchar() != '\n')
        continue;
    return ch;
}