---
# System prepended metadata

title: C習題6-21
tags: [C習題]

---

# C習題6-21
###### tags: `C習題`
## 題目:
>航空公司訂位, 一個班機10個座位分兩種等級(頭等艙&經濟艙)各一半, 使用一個一維陣列儲存座位狀態, 設計一個訂位系統, 包含:艙等選擇, 印出登機證(包含艙等&座位號碼),若其一艙等客滿可詢問更換艙等功能.
## 流程圖:
START -->輸入訂位艙等-->確認座位是否可用(回傳頭等/經濟艙客滿狀態)
-->條件狀態:

| 狀態                          |     |     |     |     |
|:----------------------------- |:--- |:--- |:--- |:--- |
| 頭等艙狀態(0=客滿,1=還有空位) | 1   | 0   | 1   | 0   |
| 經濟艙狀態(0=客滿,1=還有空位) | 0   | 1   | 1   | 0   |
| 輸入頭等艙=1                  | A   | E   | A   | X   |
| 輸入經濟艙=2                  | E   | A   | A   | X   |


-->**狀態A**:印出登機證-->END

-->**狀態E**:詢問更換艙等
---->Y, 印出登機證(狀態A)-->END
---->N, 客滿,印出下一班機資訊(狀態X)-->END

-->**狀態X**:客滿,印出下一班機資訊-->END

## code:
```c=
#include <stdio.h>
#define SEAT 10 /* there are 10 seats in a flight*/
#define FCLS 4 /*0-4, 5 seats*/
#define ECO 9  /*5-9, 5 seats*/

/*Function Prototype*/
int initialArray(int s[SEAT], int a);
int checkSpaceFCLS(int s[SEAT], int i);
int checkSpaceECO(int s[SEAT], int i);
int printArray(int s[SEAT], int a);
int printBoardingPass(int class, int s[SEAT], int i);
int exprintBoardingPass(int class, int s[SEAT], int i);

int main()  
{
    int class,FCLSstatus,ECOstatus;
    int s[SEAT]; /*= {0,0,0,0,0,1,1,1,1,1};*/
    int change;
    
    initialArray(s,SEAT);       /* initial array before while loop*/
    /*printArray(s,SEAT); */        /*check array be initial*/
    
    printf("\n");
    printf("Please type 1 for First class:\n");
    printf("Please type 2 for Economy:\n");
    printf("key in -1 to cancel:\n");
    scanf("%d",&class);
        while(class!= -1) {
            printArray(s,SEAT);
            printf("\n");
            FCLSstatus = checkSpaceFCLS(s,SEAT);
            ECOstatus = checkSpaceECO(s,SEAT);
            /*printf("\nclass = %d",class);
            printf("\nFCLSstatus = %d\n",FCLSstatus);
            printf("ECOstatus = %d\n",ECOstatus);*/
            
            if((class == 1 && FCLSstatus == 1 && ECOstatus == 0) || (class == 2 && FCLSstatus == 0 && ECOstatus == 1) || (class == 1 && FCLSstatus == 1 && ECOstatus == 1) || (class == 2 && FCLSstatus == 1 && ECOstatus == 1)) 
            {
                /*printf("\nclass = %d",class);*/
                printBoardingPass(class,s,SEAT);   
            }
            if((class == 1 && FCLSstatus == 0 && ECOstatus == 0) || (class == 2 && FCLSstatus == 0 && ECOstatus == 0))
            {
                /*printf("\nclass = %d",class);*/
                printf("Seats are not avaliable now, Your next flight will arrived tomorror\n");
            }
            if((class == 1 && FCLSstatus == 0 && ECOstatus == 1) || (class == 2 && FCLSstatus == 1 && ECOstatus == 0) )
            {
                /*printf("\nclass = %d",class);*/
                printf("Selected class in not avaliable, would you like to change class?(9/0)\n");
                /*printf("\nclass = %d",class);*/
                scanf("%d",&change);
                
                printf("\nclass = %d",class);
                
                switch(change){
                    case 9:
                    /*case 9:*/
                        exprintBoardingPass(class,s,SEAT);
                        break;
                    case 0:
                    /*case 0:*/
                        printf("Seats are not avaliable now, Your next flight will arrived tomorror\n");
                        break;
                }
                
            }
            printf("\n\n\n");
            printf("Please type 1 for First class:\n");
            printf("Please type 2 for Economy:\n");
            printf("key in -1 to cancel:\n");
            scanf("%d",&class);
            
            
    }
    printf("*************************\n");
    printf("Thank you for your visit!\n");
    printf("*************************\n");
    
    return 0;
}

int printArray(int s[SEAT], int a)
{
    int i;/*counter*/
    printf("Array Status:\n");
    for(i=0;i<a;i++) {
        printf("%d\t",s[i]);
    }
    return 0;
}

int initialArray(int s[SEAT], int a)
{
    int i;/*counter*/
    for(i=0;i<a;i++) {
        s[i] = 1;
    }
    return s[SEAT];
}

int checkSpaceFCLS(int s[SEAT], int i) /*check array status, return FCLSstatus, ECOstatus;value = 1 means avaliable, value = 0 means not avaliable*/
{
    int FCLSstatus,sumFCLS;
    
    for(i=0;i<=FCLS;i++) {
        sumFCLS += s[i];
    }
   
    if(sumFCLS == 0) {
        FCLSstatus = 0;
        /*return FCLSstatus;*/
    } else { 
        FCLSstatus = 1;
        /*return FCLSstatus;*/
    }
}

int checkSpaceECO(int s[SEAT], int i)
{
    int ECOstatus,sumECO;
    for(i=5;i<=ECO;i++) {
        sumECO += s[i];
    }
    if(sumECO == 0) {
        ECOstatus = 0;
        /*return ECOstatus;*/
    } else {
        ECOstatus = 1;
        /*return ECOstatus;*/
    }
}

int printBoardingPass(int class, int s[SEAT], int i)
{
    int c, FCLSseat, ECOseat;
    
    printf("Welcome Aboard\n");
    
    switch(class) {
        case 1:
            for(c=0;c<=4;c++) 
            {
                if(s[c] == 0)
                    {
                        continue;
                    }
                if(s[c] != 0)  
                    {
                        s[c] = 0;
                        FCLSseat = c + 1;
                    }
                    break;
            }
            printf("YOUR SEAT\nFirst Class\nNo.%d\n",FCLSseat);
            break;
        case 2:
            for(c=5;c<=9;c++)
            {
                if(s[c] == 0)
                    {
                        continue;
                    }
                if(s[c] != 0)
                    {
                        s[c] = 0;
                        ECOseat = c + 1;
                    }
            break;
            }
        printf("YOUR SEAT\nEco Class\nNo.%d\n",ECOseat);
        break;
        
    }
}

int exprintBoardingPass(int class, int s[SEAT], int i)
{
    int c, FCLSseat, ECOseat;
    
    printf("Welcome Aboard\n");
    /*printf("class = %d\n",class);*/
    switch(class) 
    {
        case 1:
            for(c=5;c<=9;c++)
            {
                if(s[c] == 0)
                    {
                        continue;
                    }
                if(s[c] != 0)
                    {
                        s[c] = 0;
                        ECOseat = c + 1;
                    }
            break;
            }
        printf("YOUR SEAT\nEco Class\nNo.%d\n",ECOseat);
        break;
        
        case 2:
            for(c=0;c<=4;c++) 
            {
                if(s[c] == 0)
                    {
                        continue;
                    }
                if(s[c] != 0)  
                    {
                        s[c] = 0;
                        FCLSseat = c + 1;
                    }
                    break;
            }
            printf("YOUR SEAT\nFirst Class\nNo.%d\n",FCLSseat);
        break;
        
        
    }
    
}
```
## 心得
* 想流程圖花了很多時間
* 因為現在函式都用return傳值, 所以只能回傳1個值, 讓叫用函式後回傳值指定到變數裡面儲存狀態.以後應該要找看看有沒有引數丟進去可以回傳多個值的方法。
* 兩個if會沒動作-->這應該是scanf造成變數被修改, 需要用switch多重選擇式
* Printbordingpass沒有傳結果值回來...有執行print但是中間的switch壞掉 . scanf 一個字元後, class值被清0?(Why?)-->最後確認在scanf之後, class被清空,只好把確認對話框改成數字判斷

拼拼湊湊出來的程式碼, 一開始先把I/O做出來, 初始化陣列(檢查功能時用指定陣列數字), 然後scanf一個值(艙等)作為最初的狀態判斷, 再進到while迴圈,檢查陣列狀態後回傳個別艙等的狀態值, 再依照艙等狀態、艙等狀態做switch判斷式, 跑3個case(A&E&X).印出登機證的功能把陣列更新寫進去, 更換艙等其實是把印出登機證的程式改成反向, 結構都一樣。

原本在檢查陣列時想用一個函式解決, 途中發現一條函式好像只能回傳一個值, 所以先拆成兩個函式寫。checkSpaceFCLS & checkSpaceECO.

不解的是不知道為何scanf 字符進變數change會改到變數class裡面的值(變成0),難道這兩個變數儲存記憶體位置剛好相鄰, 字符進去溢位('\0')跑去其他位置?
