# 電機一乙 11128271 蔡嘉泓 U-11 5/16 Hackmd作業 ## Function-Pointer ## 1. Use qsort to sort an array with different ways: 1. Sort from small to large 2. Sort from large to small 3. Odd numbers first, then even numbers; from small to large。 ```c= #include <stdio.h> #include <stdlib.h> int compareAscending(const void *a, const void *b) { int num1 = *(const int *)a; int num2 = *(const int *)b; return (num1 - num2); } int main() { int arr[] = {5, 2, 8, 1, 9}; int size = sizeof(arr) / sizeof(arr[0]); qsort(arr, size, sizeof(int), compareAscending); printf("Sorted array in ascending order: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; } ``` ![](https://hackmd.io/_uploads/H1mVnnDH2.png) ```c= #include <stdio.h> #include <stdlib.h> int compareDescending(const void *a, const void *b) { int num1 = *(const int *)a; int num2 = *(const int *)b; return (num2 - num1); } int main() { int arr[] = {5, 2, 8, 1, 9}; int size = sizeof(arr) / sizeof(arr[0]); qsort(arr, size, sizeof(int), compareDescending); printf("Sorted array in descending order: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; } ``` ![](https://hackmd.io/_uploads/S15I2nPSn.png) ```c= #include <stdio.h> #include <stdlib.h> int compareOddEven(const void *a, const void *b) { int num1 = *(const int *)a; int num2 = *(const int *)b; if ((num1 % 2 == 0) && (num2 % 2 == 0)) { return (num1 - num2); } else if ((num1 % 2 != 0) && (num2 % 2 != 0)) { return (num1 - num2); } else if (num1 % 2 != 0) { return -1; } else { return 1; } } int main() { int arr[] = {5, 2, 8, 1, 9}; int size = sizeof(arr) / sizeof(arr[0]); qsort(arr, size, sizeof(int), compareOddEven); printf("Sorted array with odd numbers first: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; } ``` ![](https://hackmd.io/_uploads/rJptnnvSn.png) ## 2. 優秀小組正在招募10個人員,根據以下要求,編寫一個處理程式。 1. 使用一個人員結構陣列,共有10個元素,每個元素為一個結構,包括以下欄位: 編號:整數(1...10) 是否已招募:0=未招募;1=已招募 。 姓名:80 個字元的陣列,存放招募人員姓名 該程式有以下選單: Choose a function, enter 0 to finish: 1) Show list of positions to be hired // Show numbers in a row 2) Show list of already hired positions (Sort by number) 3) Show list of already hired positions (Sort by name) 4) Hire a person // Input number and name to set a position 5) Fire a person // Input number to fire a person 本題請同學先完成選單的功能,當使用者選用某一項功能時,先印出功能之說明即可。 ```c= #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 80 #define NUM_POSITIONS 10 typedef struct { int number; int hired; char name[MAX_NAME_LEN]; } Position; int cmp_by_number(const void *a, const void *b) { Position *pos_a = (Position *)a; Position *pos_b = (Position *)b; return pos_a->number - pos_b->number; } int cmp_by_name(const void *a, const void *b) { Position *pos_a = (Position *)a; Position *pos_b = (Position *)b; return strcmp(pos_a->name, pos_b->name); } void show_positions(Position positions[], int hired) { for (int i = 0; i < NUM_POSITIONS; i++) { if (positions[i].hired == hired) { printf("%d %s\n", positions[i].number, positions[i].name); } } } void hire_person(Position positions[]) { int number; char name[MAX_NAME_LEN]; printf("Enter the number of the position to hire: "); scanf("%d", &number); getchar(); printf("Enter the name of the person to hire: "); fgets(name, MAX_NAME_LEN, stdin); name[strcspn(name, "\n")] = 0; positions[number - 1].hired = 1; strcpy(positions[number - 1].name, name); } void fire_person(Position positions[]) { int number; printf("Enter the number of the position to fire: "); scanf("%d", &number); positions[number - 1].hired = 0; } int main() { Position positions[NUM_POSITIONS]; for (int i = 0; i < NUM_POSITIONS; i++) { positions[i].number = i + 1; positions[i].hired = 0; strcpy(positions[i].name, ""); } int choice; while (1) { 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"); scanf("%d", &choice); if (choice == 0) { break; } else if (choice == 1) { show_positions(positions, 0); } else if (choice == 2) { qsort(positions, NUM_POSITIONS, sizeof(Position), cmp_by_number); show_positions(positions, 1); } else if (choice == 3) { qsort(positions, NUM_POSITIONS, sizeof(Position), cmp_by_name); show_positions(positions, 1); } else if (choice == 4) { hire_person(positions); } else if (choice == 5) { fire_person(positions); } } return 0; } ``` ![](https://hackmd.io/_uploads/BktlnhPB3.png)