# 05/22 https://hackmd.io/@-JG915ddTbSXM0tfs4kLTw/ryAJ5yABn # 主程式 ```c= #include <stdio.h> #include <stdlib.h> #include <string.h> #include"myheader.h" #define MAX_NAME_LENGTH 80 #define NUM_PERSONS 10 Person Group[NUM_PERSONS]; void showMenu() { 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() { for (int i = 0; i < NUM_PERSONS; i++) { Group[i].id = i + 1; Group[i].hired = 0; strcpy(Group[i].name, ""); } int choice; do { showMenu(); scanf("%d", &choice); switch (choice) { case 0: printf("Exiting...\n"); break; case 1: showPositionsToBeHired(Group); break; case 2: showAlreadyHiredPositionsByNumber(Group); break; case 3: showAlreadyHiredPositionsByName(Group); break; case 4: hirePerson(Group); break; case 5: firePerson(Group); break; default: printf("Invalid choice. Please try again.\n\n"); break; } } while (choice != 0); return 0; } ``` # 宣告檔頭 ```c= #ifndef MYHEADER_H_INCLUDED #define MYHEADER_H_INCLUDED #define MAX_NAME_LENGTH 80 #define NUM_PERSONS 10 typedef struct { int id; int hired; char name[MAX_NAME_LENGTH]; } Person; void showPositionsToBeHired(Person group[]); void showAlreadyHiredPositionsByNumber(Person group[]); void showAlreadyHiredPositionsByName(Person group[]); void hirePerson(Person group[]); void firePerson(Person group[]); #endif // MYHEADER_H_INCLUDED _H_INCLUDED ``` # 把showPositionsToBeHired移到一個新檔案 ```c= #include <stdio.h> #include"myheader.h" void showPositionsToBeHired(Person group[]) { printf("Positions to be hired:\n"); for (int i = 0; i < NUM_PERSONS; i++) { if (!group[i].hired) { printf("%d\n", group[i].id); } } printf("\n"); } ``` # 把showAlreadyHiredPositionsByNumber移到一個新檔案 ```c= #include <stdio.h> #include"myheader.h" void showAlreadyHiredPositionsByNumber(Person group[]) { printf("Already hired positions (Sorted by number):\n"); for (int i = 0; i < NUM_PERSONS; i++) { if (group[i].hired) { printf("%d\n", group[i].id); } } printf("\n"); } ``` # 把showAlreadyHiredPositionsByName移到一個新檔案 ```c= #include <stdio.h> #include"myheader.h" void showAlreadyHiredPositionsByName(Person group[]) { printf("Already hired positions (Sorted by name):\n"); for (int i = 0; i < NUM_PERSONS; i++) { if (group[i].hired) { printf("%s\n", group[i].name); } } printf("\n"); } ``` # 把hirePerson移到一個新檔案 ```c= #include <stdio.h> #include"myheader.h" void hirePerson(Person group[]) { int position; char name[MAX_NAME_LENGTH]; printf("Enter the position number to hire: "); scanf("%d", &position); if (position < 1 || position > NUM_PERSONS) { printf("Invalid position number.\n\n"); return; } if (group[position - 1].hired) { printf("Position is already hired.\n\n"); return; } printf("Enter the name of the person to hire: "); scanf("%s", name); group[position - 1].id = position; group[position - 1].hired = 1; strcpy(group[position - 1].name, name); printf("Person hired successfully.\n\n"); } ``` # 把firePerson移到一個新檔案 ```c= #include <stdio.h> #include"myheader.h" void firePerson(Person group[]) { int position; printf("Enter the position number to fire: "); scanf("%d", &position); if (position < 1 || position > NUM_PERSONS) { printf("Invalid position number.\n\n"); return; } if (!group[position - 1].hired) { printf("Position is not hired.\n\n"); return; } group[position - 1].hired = 0; strcpy(group[position - 1].name, ""); printf("Person fired successfully.\n\n"); } ```