【C语言】员工管理系统丨源码+解析

 系统功能说明

该员工管理系统包含以下核心功能模块:

  1. 数据持久化存储

    • 使用二进制文件存储员工数据
    • 启动自动加载数据,退出自动保存
    • 支持手动保存(选项6)
  2. 员工管理功能

    • 添加员工:包含工号唯一性验证
    • 显示所有:表格化展示员工信息
    • 查找员工:支持工号/姓名两种方式
    • 修改信息:交互式修改各项字段
    • 删除员工:二次确认防止误操作
  3. 数据结构设计

    • 使用结构体存储员工完整信息
    • 固定数组存储(最大1000名员工)
    • 包含ID、姓名、性别、年龄等关键字段
  4. 用户界面

    • 控制台菜单驱动界面
    • 表格化数据展示
    • 操作结果即时反馈

学习要点

 1.数据结构设

typedef struct {
    int id;
    char name[50];
    // ...其他字段
} Employee;

 结构体是组织相关数据的理想方式

 

 2.文件操作

// 读取数据
FILE *file = fopen(FILENAME, "rb");
fread(employees, sizeof(Employee), count, file);

// 保存数据
FILE *file = fopen(FILENAME, "wb");
fwrite(employees, sizeof(Employee), count, file);

二进制文件操作实现数据持久化

 

 3.​输入验证

// 工号唯一性检查
do {
    printf("工号: ");
    scanf("%d", &id);
    for (int i = 0; i < count; i++) {
        if (employees[i].id == id) {
            idExists = 1;
            break;
        }
    }
} while (idExists);

 确保关键数据的唯一性

 

 4.模块化设计 

  • 每个功能独立成函数
  • 高内聚低耦合结构
  • 菜单驱动控制流程

 

 源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

#define MAX_EMPLOYEES 1000  // 最大员工数量
#define FILENAME "employee.dat"  // 数据存储文件名

// 员工信息结构体
typedef struct {
    int id;             // 工号(唯一标识)
    char name[50];      // 姓名
    char gender[10];    // 性别
    int age;            // 年龄
    char department[30]; // 部门
    char position[30];  // 职位
    char phone[15];     // 电话
    float salary;       // 基本工资
} Employee;

Employee employees[MAX_EMPLOYEES];  // 员工数组
int count = 0;  // 当前员工数量

// 函数声明
void loadData();
void saveData();
void addEmployee();
void displayAll();
void findEmployee();
void modifyEmployee();
void deleteEmployee();
void showMenu();

// 主函数
int main() {
    loadData();  // 启动时加载数据
    
    while(1) {
        showMenu();
        char choice = getch();
        
        switch(choice) {
            case '1': addEmployee(); break;
            case '2': displayAll(); break;
            case '3': findEmployee(); break;
            case '4': modifyEmployee(); break;
            case '5': deleteEmployee(); break;
            case '6': saveData(); break;
            case '0': 
                saveData();
                printf("\n系统已退出,数据已保存!\n");
                exit(0);
            default: 
                printf("\n无效选项,请重新选择!\n");
                getch();
        }
    }
    return 0;
}

// 加载数据
void loadData() {
    FILE *file = fopen(FILENAME, "rb");
    if (!file) return;
    
    count = fread(employees, sizeof(Employee), MAX_EMPLOYEES, file);
    fclose(file);
    printf("\n成功加载%d条员工记录!\n", count);
}

// 保存数据
void saveData() {
    FILE *file = fopen(FILENAME, "wb");
    if (!file) {
        printf("\n文件保存失败!\n");
        return;
    }
    
    fwrite(employees, sizeof(Employee), count, file);
    fclose(file);
    printf("\n数据保存成功(共%d条记录)!\n", count);
}

// 添加员工
void addEmployee() {
    if (count >= MAX_EMPLOYEES) {
        printf("\n员工数量已达上限!\n");
        return;
    }
    
    Employee emp;
    printf("\n--- 添加新员工 ---\n");
    
    // 工号验证(确保唯一性)
    int id;
    int idExists;
    do {
        idExists = 0;
        printf("工号: ");
        scanf("%d", &id);
        
        for (int i = 0; i < count; i++) {
            if (employees[i].id == id) {
                printf("该工号已存在!\n");
                idExists = 1;
                break;
            }
        }
    } while (idExists);
    
    emp.id = id;
    printf("姓名: ");
    scanf("%s", emp.name);
    printf("性别: ");
    scanf("%s", emp.gender);
    printf("年龄: ");
    scanf("%d", &emp.age);
    printf("部门: ");
    scanf("%s", emp.department);
    printf("职位: ");
    scanf("%s", emp.position);
    printf("电话: ");
    scanf("%s", emp.phone);
    printf("工资: ");
    scanf("%f", &emp.salary);
    
    employees[count++] = emp;
    saveData();  // 自动保存
    printf("\n添加成功!\n");
}

// 显示所有员工
void displayAll() {
    printf("\n%-8s%-10s%-8s%-6s%-15s%-15s%-15s%-10s\n", 
           "工号", "姓名", "性别", "年龄", "部门", "职位", "电话", "工资");
    printf("------------------------------------------------------------\n");
    
    for (int i = 0; i < count; i++) {
        printf("%-8d%-10s%-8s%-6d%-15s%-15s%-15s%-10.2f\n",
               employees[i].id,
               employees[i].name,
               employees[i].gender,
               employees[i].age,
               employees[i].department,
               employees[i].position,
               employees[i].phone,
               employees[i].salary);
    }
    printf("\n共显示%d名员工信息\n", count);
    printf("\n按任意键返回...");
    getch();
}

// 查找员工
void findEmployee() {
    int option;
    printf("\n--- 查找方式 ---\n");
    printf("1. 按工号查找\n");
    printf("2. 按姓名查找\n");
    printf("请选择: ");
    scanf("%d", &option);
    
    if (option == 1) {
        int id;
        printf("输入工号: ");
        scanf("%d", &id);
        
        for (int i = 0; i < count; i++) {
            if (employees[i].id == id) {
                printf("\n%-8s%-10s%-8s%-6s%-15s%-15s%-15s%-10s\n", 
                       "工号", "姓名", "性别", "年龄", "部门", "职位", "电话", "工资");
                printf("%-8d%-10s%-8s%-6d%-15s%-15s%-15s%-10.2f\n",
                       employees[i].id,
                       employees[i].name,
                       employees[i].gender,
                       employees[i].age,
                       employees[i].department,
                       employees[i].position,
                       employees[i].phone,
                       employees[i].salary);
                printf("\n按任意键返回...");
                getch();
                return;
            }
        }
        printf("\n未找到该员工!\n");
    } 
    else if (option == 2) {
        char name[50];
        printf("输入姓名: ");
        scanf("%s", name);
        int found = 0;
        
        for (int i = 0; i < count; i++) {
            if (strcmp(employees[i].name, name) == 0) {
                if (!found) {
                    printf("\n%-8s%-10s%-8s%-6s%-15s%-15s%-15s%-10s\n", 
                           "工号", "姓名", "性别", "年龄", "部门", "职位", "电话", "工资");
                    found = 1;
                }
                printf("%-8d%-10s%-8s%-6d%-15s%-15s%-15s%-10.2f\n",
                       employees[i].id,
                       employees[i].name,
                       employees[i].gender,
                       employees[i].age,
                       employees[i].department,
                       employees[i].position,
                       employees[i].phone,
                       employees[i].salary);
            }
        }
        
        if (!found) {
            printf("\n未找到该员工!\n");
        } else {
            printf("\n按任意键返回...");
            getch();
        }
    }
}

// 修改员工信息
void modifyEmployee() {
    int id;
    printf("\n输入要修改的员工工号: ");
    scanf("%d", &id);
    
    for (int i = 0; i < count; i++) {
        if (employees[i].id == id) {
            printf("\n当前信息:\n");
            printf("%-8s%-10s%-8s%-6s%-15s%-15s%-15s%-10s\n", 
                   "工号", "姓名", "性别", "年龄", "部门", "职位", "电话", "工资");
            printf("%-8d%-10s%-8s%-6d%-15s%-15s%-15s%-10.2f\n",
                   employees[i].id,
                   employees[i].name,
                   employees[i].gender,
                   employees[i].age,
                   employees[i].department,
                   employees[i].position,
                   employees[i].phone,
                   employees[i].salary);
            
            printf("\n输入新信息:\n");
            printf("姓名 (%s): ", employees[i].name);
            scanf("%s", employees[i].name);
            printf("性别 (%s): ", employees[i].gender);
            scanf("%s", employees[i].gender);
            printf("年龄 (%d): ", employees[i].age);
            scanf("%d", &employees[i].age);
            printf("部门 (%s): ", employees[i].department);
            scanf("%s", employees[i].department);
            printf("职位 (%s): ", employees[i].position);
            scanf("%s", employees[i].position);
            printf("电话 (%s): ", employees[i].phone);
            scanf("%s", employees[i].phone);
            printf("工资 (%.2f): ", employees[i].salary);
            scanf("%f", &employees[i].salary);
            
            saveData();
            printf("\n修改成功!\n");
            return;
        }
    }
    printf("\n未找到该员工!\n");
}

// 删除员工
void deleteEmployee() {
    int id;
    printf("\n输入要删除的员工工号: ");
    scanf("%d", &id);
    
    for (int i = 0; i < count; i++) {
        if (employees[i].id == id) {
            printf("\n确定删除 %s (工号:%d)? (y/n): ", employees[i].name, id);
            char confirm = getch();
            
            if (confirm == 'y' || confirm == 'Y') {
                for (int j = i; j < count - 1; j++) {
                    employees[j] = employees[j + 1];
                }
                count--;
                saveData();
                printf("\n删除成功!\n");
            } else {
                printf("\n取消删除\n");
            }
            return;
        }
    }
    printf("\n未找到该员工!\n");
}

// 显示菜单
void showMenu() {
    system("cls");  // 清屏
    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("==========================\n");
    printf("请选择操作: ");
}

 

扩展建议

  1. 增强功能

    • 薪资计算(增加绩效、奖金等字段)
    • 按部门/薪资排序功能
    • 员工考勤记录管理
  2. 优化方向

    • 使用动态内存分配替代固定数组
    • 增加数据加密存储
    • 实现更友好的用户界面

建议保存此源码为employee_system.c,使用C编译器(如GCC)编译运行。

系统会自动创建数据文件,无需额外配置

 

  资源推荐  :

C/C++学习交流君羊 << 点击加入

C/C++教程

C/C++学习路线,就业咨询,技术提升

posted @ 2025-07-08 15:27  C语言实战大全  阅读(61)  评论(0)    收藏  举报