---
# System prepended metadata

title: 內容

---

# 內容
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PETS 100

typedef struct {
    int id;
    char name[50];
    int age;
    char features[100];
} Pet;

void initializePets(Pet pets[], int *numPets) {
    *numPets = 0;
    // 初始化寵物陣列
}

void addPet(Pet pets[], int *numPets, int id, const char *name, int age, const char *features) {
    if (*numPets >= MAX_PETS) {
        printf("寵物已達到最大數量，無法新增。\n");
        return;
    }

    pets[*numPets].id = id;
    strcpy(pets[*numPets].name, name);
    pets[*numPets].age = age;
    strcpy(pets[*numPets].features, features);

    (*numPets)++;
    printf("新增寵物成功。\n");
}

void removePet(Pet pets[], int *numPets, int id) {
    int i, found = 0;

    for (i = 0; i < *numPets; i++) {
        if (pets[i].id == id) {
            found = 1;
            break;
        }
    }

    if (found) {
        // 將找到的寵物從陣列中刪除
        for (; i < (*numPets - 1); i++) {
            pets[i] = pets[i + 1];
        }
        (*numPets)--;
        printf("刪除寵物成功。\n");
    } else {
        printf("找不到編號為 %d 的寵物。\n", id);
    }
}

void searchPetByName(const Pet pets[], int numPets, const char *name) {
    int i, found = 0;

    for (i = 0; i < numPets; i++) {
        if (strcmp(pets[i].name, name) == 0) {
            found = 1;
            printf("找到寵物：\n");
            printf("編號：%d\n", pets[i].id);
            printf("名字：%s\n", pets[i].name);
            printf("年齡：%d\n", pets[i].age);
            printf("特徵：%s\n", pets[i].features);
            break;
        }
    }

    if (!found) {
        printf("找不到名字為 %s 的寵物。\n", name);
    }
}

void searchPetById(const Pet pets[], int numPets, int id) {
    int i, found = 0;

    for (i = 0; i < numPets; i++) {
        if (pets[i].id == id) {
            found = 1;
            printf("找到寵物：\n");
            printf("編號：%d\n", pets[i].id);
            printf("名字：%s\n", pets[i].name);
            printf("年齡：%d\n", pets[i].age);
            printf("特徵：%s\n", pets[i].features);
            break;
        }
    }

    if (!found) {
        printf("找不到編號為 %d 的寵物。\n", id);
    }
}

void searchPetByAge(const Pet pets[], int numPets, int age) {
    int i, found = 0;

    for (i = 0; i < numPets; i++) {
        if (pets[i].age == age) {
            found = 1;
            printf("找到寵物：\n");
            printf("編號：%d\n", pets[i].id);
            printf("名字：%s\n", pets[i].name);
            printf("年齡：%d\n", pets[i].age);
            printf("特徵：%s\n", pets[i].features);
        }
    }

    if (!found) {
        printf("找不到年齡為 %d 的寵物。\n", age);
    }
}

void searchPetByFeatures(const Pet pets[], int numPets, const char *features) {
    int i, found = 0;

    for (i = 0; i < numPets; i++) {
        if (strstr(pets[i].features, features) != NULL) {
            found = 1;
            printf("找到寵物：\n");
            printf("編號：%d\n", pets[i].id);
            printf("名字：%s\n", pets[i].name);
            printf("年齡：%d\n", pets[i].age);
            printf("特徵：%s\n", pets[i].features);
        }
    }

    if (!found) {
        printf("找不到具有特徵 %s 的寵物。\n", features);
    }
}

int main() {
    Pet pets[MAX_PETS];
    int numPets = 0;
    int choice, id, age;
    char name[50], features[100];

    initializePets(pets, &numPets);

    do {
        printf("\n--- 寵物店管理系統 ---\n");
        printf("1. 新增寵物\n");
        printf("2. 刪除寵物\n");
        printf("3. 透過姓名尋找寵物\n");
        printf("4. 透過編號尋找寵物\n");
        printf("5. 透過年齡尋找寵物\n");
        printf("6. 透過特徵尋找寵物\n");
        printf("0. 離開\n");
        printf("請選擇操作：");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("輸入寵物的編號：");
                scanf("%d", &id);
                printf("輸入寵物的名字：");
                scanf("%s", name);
                printf("輸入寵物的年齡：");
                scanf("%d", &age);
                printf("輸入寵物的特徵：");
                scanf("%s", features);
                addPet(pets, &numPets, id, name, age, features);
                break;
            case 2:
                printf("輸入要刪除的寵物編號：");
                scanf("%d", &id);
                removePet(pets, &numPets, id);
                break;
            case 3:
                printf("輸入要搜尋的寵物名字：");
                scanf("%s", name);
                searchPetByName(pets, numPets, name);
                break;
            case 4:
                printf("輸入要搜尋的寵物編號：");
                scanf("%d", &id);
                searchPetById(pets, numPets, id);
                break;
            case 5:
                printf("輸入要搜尋的寵物年齡：");
                scanf("%d", &age);
                searchPetByAge(pets, numPets, age);
                break;
            case 6:
                printf("輸入要搜尋的寵物特徵：");
                scanf("%s", features);
                searchPetByFeatures(pets, numPets, features);
                break;
            case 0:
                printf("程式結束。\n");
                break;
            default:
                printf("無效的選擇。\n");
                break;
        }
    } while (choice != 0);

    return 0;
}
```
- 在此程式中使用Pet結構來表示每隻寵物的資訊，包含編號、名字、年齡和特徵，管理員可以透過不同的選單進行操作，進行管理的動作。