首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> 關於C

C 語言實戰:結構體實現通訊錄管理系統(模塊化設計)

     編輯:關於C

需求分析

實現一個可存儲 1000 聯系人的通訊錄,支持增刪改查、顯示、清空、按姓名排序。採用三模塊分離架構:

  1. 頭文件:定義數據結構與函數聲明;
  2. 測試主程序:菜單交互、調度功能函數;
  3. 功能實現文件:具體業務邏輯。

模塊一:數據結構定義 (contact.h)

#pragma once
#include <string.h>

typedef struct Peo {
    char name[20];
    char sex[4];
    int age;
    char tell[12];
    char address[20];
} Peo;

typedef struct Contact {
    Peo data[1000];
    int count;
} Contact;

// 函數聲明
void add_stu(Contact *p);
void del_stu(Contact *p);
void find_stu(Contact *p);
void modify_stu(Contact *p);
void show_stu(Contact *p);
void clear_stu(Contact *p);
void sort_stu(Contact *p);

模塊二:主程序與菜單 (test.c)

#include <stdio.h>
#include <stdlib.h>
#include "contact.h"

Contact con; // 全局通訊錄實例

void menu() {
    printf("********** 通訊錄管理系統 **********\n");
    printf("* 1. 添加聯系人   2. 刪除聯系人 *\n");
    printf("* 3. 查找聯系人   4. 修改聯系人 *\n");
    printf("* 5. 顯示所有     6. 清空通訊錄 *\n");
    printf("* 7. 姓名排序     0. 退出系統   *\n");
    printf("************************************\n");
}

int main() {
    int input = 1;
    menu();
    while (input) {
        printf("請輸入選項: ");
        scanf("%d", &input);
        switch (input) {
            case 1: add_stu(&con); break;
            case 2: del_stu(&con); break;
            case 3: find_stu(&con); break;
            case 4: modify_stu(&con); break;
            case 5: show_stu(&con); break;
            case 6: clear_stu(&con); break;
            case 7: sort_stu(&con); break;
            case 0: exit(0);
            default: printf("輸入錯誤,請重試!\n");
        }
    }
    return 0;
}

模塊三:核心功能實現 (contact.c)

通用查找函數

int search(Contact *p, const char *name) {
    for (int i = 0; i < p->count; ++i)
        if (strcmp(p->data[i].name, name) == 0) return i;
    return -1;
}

添加聯系人

void add_stu(Contact *p) {
    if (p->count >= 1000) { printf("通訊錄已滿!\n"); return; }
    printf("姓名: "); scanf("%19s", p->data[p->count].name);
    printf("性別: "); scanf("%3s", p->data[p->count].sex);
    printf("年齡: "); scanf("%d", &p->data[p->count].age);
    printf("電話: "); scanf("%11s", p->data[p->count].tell);
    printf("地址: "); scanf("%19s", p->data[p->count].address);
    p->count++;
    printf("添加成功!\n");
}

刪除聯系人(含確認)

void del_stu(Contact *p) {
    char name[20];
    printf("輸入要刪除的姓名: "); scanf("%19s", name);
    int pos = search(p, name);
    if (pos == -1) { printf("不存在該聯系人。\n"); return; }
    printf("確定刪除?(1=是/0=否): ");
    int confirm; scanf("%d", &confirm);
    if (confirm) {
        for (int i = pos; i < p->count - 1; ++i)
            p->data[i] = p->data[i+1];
        p->count--;
        printf("刪除成功。\n");
    } else printf("已取消。\n");
}

查找、修改、顯示、清空

void find_stu(Contact *p) {
    char name[20];
    printf("輸入查找姓名: "); scanf("%19s", name);
    int pos = search(p, name);
    if (pos != -1) {
        Peo *e = &p->data[pos];
        printf("%s %s %d %s %s\n", e->name, e->sex, e->age, e->tell, e->address);
    } else printf("查無此人。\n");
}

void modify_stu(Contact *p) {
    char name[20];
    printf("輸入要修改的姓名: "); scanf("%19s", name);
    int pos = search(p, name);
    if (pos == -1) { printf("不存在。\n"); return; }
    printf("新姓名: "); scanf("%19s", p->data[pos].name);
    printf("新性別: "); scanf("%3s", p->data[pos].sex);
    printf("新年齡: "); scanf("%d", &p->data[pos].age);
    printf("新電話: "); scanf("%11s", p->data[pos].tell);
    printf("新地址: "); scanf("%19s", p->data[pos].address);
    printf("修改完成。\n");
}

void show_stu(Contact *p) {
    if (p->count == 0) { printf("通訊錄為空。\n"); return; }
    for (int i = 0; i < p->count; ++i) {
        Peo *e = &p->data[i];
        printf("%d: %s %s %d %s %s\n", i+1, e->name, e->sex, e->age, e->tell, e->address);
    }
}

void clear_stu(Contact *p) { p->count = 0; printf("已清空。\n"); }

按姓名排序(冒泡法)

void sort_stu(Contact *p) {
    for (int i = 0; i < p->count - 1; ++i)
        for (int j = 0; j < p->count - 1 - i; ++j)
            if (strcmp(p->data[j].name, p->data[j+1].name) > 0) {
                Peo tmp = p->data[j];
                p->data[j] = p->data[j+1];
                p->data[j+1] = tmp;
            }
    printf("排序完成。\n");
    show_stu(p);
}

編譯與運行

gcc test.c contact.c -o addressbook
./addressbook

設計亮點總結

  • 模塊解耦:頭文件隔離接口,便於團隊協作與維護;
  • 指針傳遞:函數接收 Contact*,避免結構體拷貝開銷;
  • 邊界檢查:防止數組越界、輸入緩衝區溢出;
  • 可擴展性:易於改為動態分配或鏈表存儲突破 1000 上限。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved