struct Person {
char name[20], wp[20], pn[20], mpn[20], fz[20], em[20], qq[20];
};
struct Person book[100]; // 資料池
int idx[100]; // 間接索引陣列:idx[i] = book 中實際下標
int n = 0; // 當前筆數
索引陣列優勢:刪除/排序只需移動 int 索引,不動大型結構體,效率高且資料不碎片化。
void input_one(struct Person *p) {
printf("姓名: "); scanf("%s", p->name);
printf("單位: "); scanf("%s", p->wp);
printf("市話: "); scanf("%s", p->pn);
printf("手機: "); scanf("%s", p->mpn);
printf("關係: "); scanf("%s", p->fz);
printf("信箱: "); scanf("%s", p->em);
printf("QQ: "); scanf("%s", p->qq);
}
void display_all() {
int pages = (n + 9) / 10;
for (int pg = 0; pg < pages; pg++) {
int start = pg * 10, end = (pg == pages-1) ? n : start + 10;
for (int i = start; i < end; i++)
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
book[idx[i]].name, book[idx[i]].wp, book[idx[i]].pn,
book[idx[i]].mpn, book[idx[i]].fz, book[idx[i]].em, book[idx[i]].qq);
if (pg < pages-1) { printf("按 1 繼續..."); getchar(); getchar(); system("cls"); }
}
}
void search() {
char key[20]; scanf("%s", key);
for (int i = 0; i < n; i++) {
struct Person *p = &book[idx[i]];
if (!strcmp(p->name, key) || !strcmp(p->wp, key) || !strcmp(p->pn, key) ||
!strcmp(p->mpn, key) || !strcmp(p->fz, key) || !strcmp(p->em, key) || !strcmp(p->qq, key)) {
// 列印該筆
}
}
}
void delete() {
char key[20]; scanf("%s", key);
for (int i = 0; i < n; i++) {
if (match(book[idx[i]], key)) {
for (int j = i; j < n-1; j++) idx[j] = idx[j+1];
n--; break;
}
}
}
%19s) 防溢位。realloc) 突破 100 筆上限。此系統展示 C 語言結構體陣列 + 索引陣列的經典資料管理模式:資料不動、索引移動,簡單高效。適合作為教學專題、嵌入式輕量管理工具、面試手寫代碼參考。