---
# System prepended metadata

title: Chatgpt

---

# Chatgpt
以下是由chatgpt所生成的原始回答
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定義人員結構
struct Person {
    int id;
    int isRecruited;
    char name[80];
};

// 列印菜單
void printMenu() {
    printf("Choose a function, enter 0 to finish:\n");
    printf("1) Show list of positions to be hired\n");
    printf("2) Show list of already hired positions (Sort by number)\n");
    printf("3) Show list of already hired positions (Sort by name)\n");
    printf("4) Hire a person\n");
    printf("5) Fire a person\n");
}

// 顯示待招募職位列表（按編號排序）
void showPositionsToBeHired(struct Person recruits[]) {
    printf("Positions to be hired:\n");
    for (int i = 0; i < 10; i++) {
        if (!recruits[i].isRecruited) {
            printf("%d\n", recruits[i].id);
        }
    }
}

// 按編號排序顯示已招募職位列表
void showHiredPositionsByNumber(struct Person recruits[]) {
    printf("Hired positions (sorted by number):\n");
    for (int i = 0; i < 10; i++) {
        if (recruits[i].isRecruited) {
            printf("Position %d: %s\n", recruits[i].id, recruits[i].name);
        }
    }
}

// 按姓名排序顯示已招募職位列表
void showHiredPositionsByName(struct Person recruits[]) {
    printf("Hired positions (sorted by name):\n");
    // 複製人員陣列以進行排序
    struct Person sortedRecruits[10];
    memcpy(sortedRecruits, recruits, sizeof(struct Person) * 10);

    // 使用氣泡排序按姓名排序
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10 - i - 1; j++) {
            if (strcmp(sortedRecruits[j].name, sortedRecruits[j + 1].name) > 0) {
                struct Person temp = sortedRecruits[j];
                sortedRecruits[j] = sortedRecruits[j + 1];
                sortedRecruits[j + 1] = temp;
            }
        }
    }

    // 列印已招募職位列表
    for (int i = 0; i < 10; i++) {
        if (sortedRecruits[i].isRecruited) {
            printf("Position %d: %s\n", sortedRecruits[i].id, sortedRecruits[i].name);
        }
    }
}

// 招募一个人员
void hirePerson(struct Person recruits[]) {
    int position;
    char name[80];

    printf("Enter position number to hire: ");
    scanf("%d", &position);

    // 檢查職位是否已招募
    if (recruits[position - 1].isRecruited) {
        printf("Position %d is already hired.\n", position);
        return;
    }

    printf("Enter name for position %d: ", position);
    scanf("%s", name);

    recruits[position - 1].id = position;
    recruits[position - 1].isRecruited = 1;
    strcpy(recruits[position - 1].name, name);

    printf("Person hired for position %d.\n", position);
}

// 解雇一個人员
void firePerson(struct Person recruits[]) {
    int position;

    printf("Enter position number to fire: ");
    scanf("%d", &position);

    // 檢查職位是否已招募
    if (!recruits[position - 1].isRecruited) {
        printf("Position %d is not hired.\n", position);
        return;
    }

    recruits[position - 1].isRecruited = 0;
    printf("Person fired from position %d.\n", position);
}

int main() {
    struct Person recruits[10]; // 創建人員陣列

    // 初始化人員陣列
    for (int i = 0; i < 10; i++) {
        recruits[i].id = i + 1;
        recruits[i].isRecruited = 0;
    }

    int choice;

    do {
        printMenu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                showPositionsToBeHired(recruits);
                break;
            case 2:
                showHiredPositionsByNumber(recruits);
                break;
            case 3:
                showHiredPositionsByName(recruits);
                break;
            case 4:
                hirePerson(recruits);
                break;
            case 5:
                firePerson(recruits);
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }

        printf("\n");
    } while (choice != 0);

    printf("Program finished.\n");

    return 0;
}
```
## 把功能分成不同檔案
myheader
```c=
#ifndef MYHEADER_H_INCLUDED
#define MYHEADER_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定義人員結構
struct Person {
    int id;
    int isRecruited;
    char name[80];
};

#endif // MYHEADER_H_INCLUDED
```
main
```c=
#include "myheader.h"

// 列印菜單
void printMenu() {
    printf("Choose a function, enter 0 to finish:\n");
    printf("1) Show list of positions to be hired\n");
    printf("2) Show list of already hired positions (Sort by number)\n");
    printf("3) Show list of already hired positions (Sort by name)\n");
    printf("4) Hire a person\n");
    printf("5) Fire a person\n");
}


int main() {
    struct Person recruits[10]; // 創建人員陣列

    // 初始化人員陣列
    for (int i = 0; i < 10; i++) {
        recruits[i].id = i + 1;
        recruits[i].isRecruited = 0;
    }

    int choice;

    do {
        printMenu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                showPositionsToBeHired(recruits);
                break;
            case 2:
                showHiredPositionsByNumber(recruits);
                break;
            case 3:
                showHiredPositionsByName(recruits);
                break;
            case 4:
                hirePerson(recruits);
                break;
            case 5:
                firePerson(recruits);
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }

        printf("\n");
    } while (choice != 0);

    printf("Program finished.\n");

    return 0;
}
```
showlist
```c=
#include "myheader.h"
#include <stdio.h>

// 顯示待招募職位列表（按編號排序）
void showPositionsToBeHired(struct Person recruits[]) {
    printf("Positions to be hired:\n");
    for (int i = 0; i < 10; i++) {
        if (!recruits[i].isRecruited) {
            printf("%d\n", recruits[i].id);
        }
    }
}
```
showHiredByNumber
```c=
#include "myheader.h"

// 按編號排序顯示已招募職位列表
void showHiredPositionsByNumber(struct Person recruits[]) {
    printf("Hired positions (sorted by number):\n");
    for (int i = 0; i < 10; i++) {
        if (recruits[i].isRecruited) {
            printf("Position %d: %s\n", recruits[i].id, recruits[i].name);
        }
    }
}
```
showHiredByName
```c=
#include "myheader.h"

// 按姓名排序顯示已招募職位列表
void showHiredPositionsByName(struct Person recruits[]) {
    printf("Hired positions (sorted by name):\n");
    // 複製人員陣列以進行排序
    struct Person sortedRecruits[10];
    memcpy(sortedRecruits, recruits, sizeof(struct Person) * 10);

    // 使用氣泡排序按姓名排序
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10 - i - 1; j++) {
            if (strcmp(sortedRecruits[j].name, sortedRecruits[j + 1].name) > 0) {
                struct Person temp = sortedRecruits[j];
                sortedRecruits[j] = sortedRecruits[j + 1];
                sortedRecruits[j + 1] = temp;
            }
        }
    }

    // 列印已招募職位列表
    for (int i = 0; i < 10; i++) {
        if (sortedRecruits[i].isRecruited) {
            printf("Position %d: %s\n", sortedRecruits[i].id, sortedRecruits[i].name);
        }
    }
}
```
Hire
```c=
#include "myheader.h"

// 招募一个人员
void hirePerson(struct Person recruits[]) {
    int position;
    char name[80];

    printf("Enter position number to hire: ");
    scanf("%d", &position);

    // 檢查職位是否已招募
    if (recruits[position - 1].isRecruited) {
        printf("Position %d is already hired.\n", position);
        return;
    }

    printf("Enter name for position %d: ", position);
    scanf("%s", name);

    recruits[position - 1].id = position;
    recruits[position - 1].isRecruited = 1;
    strcpy(recruits[position - 1].name, name);

    printf("Person hired for position %d.\n", position);
}
```
Fire
```c=
#include "myheader.h"

// 解雇一個人员
void firePerson(struct Person recruits[]) {
    int position;

    printf("Enter position number to fire: ");
    scanf("%d", &position);

    // 檢查職位是否已招募
    if (!recruits[position - 1].isRecruited) {
        printf("Position %d is not hired.\n", position);
        return;
    }

    recruits[position - 1].isRecruited = 0;
    printf("Person fired from position %d.\n", position);
}
```
分好的檔案
![](https://hackmd.io/_uploads/Byc_xxZU2.png)



