案例:设计一个工资管理系统,要求系统具有如下功能:添加员工、删除员工、修改员工信息、打印工资单、打印汇总表。【用函数指针方法实现】

做法1:

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

#define MAX_EMPLOYEE 100 // 最大员工数

// 职工信息结构体
typedef struct {
    char id[10]; // 职工编号
    char name[20]; // 姓名
    char gender[5]; // 性别
    char marital_status[10]; // 婚姻状况
    char degree[20]; // 学位
    char department[20]; // 所属部门
    char position[20]; // 职位
    float salary; // 工资
    int work_years; // 工龄
} Employee;

// 全局变量
Employee employees[MAX_EMPLOYEE]; // 员工数组
int employee_count = 0; // 员工数

// 函数声明
void add_employee();
void delete_employee();
void modify_employee();
void print_payroll();
void print_summary();
void save_data();
void load_data();

int main() {
    int choice;
    load_data(); // 加载数据

    while (1) {
        printf("工资管理系统\n");
        printf("1. 添加员工\n");
        printf("2. 删除员工\n");
        printf("3. 修改员工信息\n");
        printf("4. 打印工资单\n");
        printf("5. 打印汇总表\n");
        printf("6. 保存数据\n");
        printf("7. 退出\n");
        printf("请选择操作:");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_employee();
                break;
            case 2:
                delete_employee();
                break;
            case 3:
                modify_employee();
                break;
            case 4:
                print_payroll();
                break;
            case 5:
                print_summary();
                break;
            case 6:
                save_data();
                break;
            case 7:
                printf("谢谢使用!\n");
                return 0;
            default:
                printf("无效的选择!\n");
                break;
        }
    }
}

// 添加员工
void add_employee() {
    if (employee_count >= MAX_EMPLOYEE) {
        printf("员工数已达到上限,无法添加!\n");
        return;
    }

    Employee employee;
    printf("请输入职工编号:");
    scanf("%s", employee.id);
    printf("请输入姓名:");
    scanf("%s", employee.name);
    printf("请输入性别:");
    scanf("%s", employee.gender);
    printf("请输入婚姻状况:");
    scanf("%s", employee.marital_status);
    printf("请输入学位:");
    scanf("%s", employee.degree);
    printf("请输入所属部门:");
    scanf("%s", employee.department);
    printf("请输入职位:");
    scanf("%s", employee.position);
    printf("请输入工资:");
    scanf("%f", &employee.salary);
    printf("请输入工龄:");
    scanf("%d", &employee.work_years);

    employees[employee_count++] = employee;
    printf("添加成功!\n");
}

// 删除员工
void delete_employee() {
    char id[10];
    printf("请输入要删除的职工编号:");
    scanf("%s", id);

    int i;
    for (i = 0; i < employee_count; i++) {
        if (strcmp(employees[i].id, id) == 0) {
            int j;
            for (j = i; j < employee_count - 1; j++) {
                employees[j] = employees[j + 1];
            }
            employee_count--;
            printf("删除成功!\n");
            return;
        }
    }

    printf("未找到该职工!\n");
}

// 修改员工信息
void modify_employee() {
    char id[10];
    printf("请输入要修改的职工编号:");
    scanf("%s", id);

    int i;
    for (i = 0; i < employee_count; i++) {
        if (strcmp(employees[i].id, id) == 0) {
            Employee employee;
            printf("请输入姓名:");
            scanf("%s", employee.name);
            printf("请输入性别:");
            scanf("%s", employee.gender);
            printf("请输入婚姻状况:");
            scanf("%s", employee.marital_status);
            printf("请输入学位:");
            scanf("%s", employee.degree);
            printf("请输入所属部门:");
            scanf("%s", employee.department);
            printf("请输入职位:");
            scanf("%s", employee.position);
            printf("请输入工资:");
            scanf("%f", &employee.salary);
            printf("请输入工龄:");
            scanf("%d", &employee.work_years);

            employees[i] = employee;
            printf("修改成功!\n");
            return;
        }
    }

    printf("未找到该职工!\n");
}

// 打印工资单
void print_payroll() {
    char id[10];
    printf("请输入要打印工资单的职工编号:");
    scanf("%s", id);

    int i;
    for (i = 0; i < employee_count; i++) {
        if (strcmp(employees[i].id, id) == 0) {
            printf("职工编号:%s\n", employees[i].id);
            printf("姓名:%s\n", employees[i].name);
            printf("性别:%s\n", employees[i].gender);
            printf("婚姻状况:%s\n", employees[i].marital_status);
            printf("学位:%s\n", employees[i].degree);
            printf("所属部门:%s\n", employees[i].department);
            printf("职位:%s\n", employees[i].position);
            printf("工资:%.2f\n", employees[i].salary);
            printf("工龄:%d\n", employees[i].work_years);
            return;
        }
    }

    printf("未找到该职工!\n");
}

// 打印汇总表
void print_summary() {
    float total_salary = 0;
    int total_work_years = 0;

    int i;
    for (i = 0; i < employee_count; i++) {
        total_salary += employees[i].salary;
        total_work_years += employees[i].work_years;
    }

    printf("员工数:%d\n", employee_count);
    printf("平均工资:%.2f\n", total_salary / employee_count);
    printf("平均工龄:%d\n", total_work_years / employee_count);
}

// 保存数据
void save_data() {
    FILE *fp = fopen("employees.dat", "wb");
    if (fp == NULL) {
        printf("无法打开文件!\n");
        return;
    }

    fwrite(&employee_count, sizeof(int), 1, fp);
    fwrite(employees, sizeof(Employee), employee_count, fp);

    fclose(fp);
    printf("保存成功!\n");
}

// 加载数据
void load_data() {
    FILE *fp = fopen("employees.dat", "rb");
    if (fp == NULL) {
        printf("无法打开文件!\n");
        return;
    }

    fread(&employee_count, sizeof(int), 1, fp);
    fread(employees, sizeof(Employee), employee_count, fp);

    fclose(fp);
    printf("加载成功!\n");
}

C语言小案例_i++

做法2:

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

#define MAX_EMPLOYEES 100

// 职工信息结构体
typedef struct {
    int id; // 职工编号
    char name[20]; // 姓名
    char gender[5]; // 性别
    char marital_status[10]; // 婚姻状况
    char degree[20]; // 学位
    char department[20]; // 所属部门
    char position[20]; // 职位
    float salary; // 工资
    int work_years; // 工龄
} Employee;

// 全局变量
Employee employees[MAX_EMPLOYEES]; // 职工信息数组
int num_employees = 0; // 职工数量

// 函数声明
void add_employee();
void delete_employee();
void modify_employee();
void print_payroll();
void print_summary();
void save_data();
void load_data();
void print_menu();
void execute_choice(int choice);

// 函数指针数组
void (*functions[])() = {add_employee, delete_employee, modify_employee, print_payroll, print_summary, save_data, load_data};

int main() {
    int choice;
    load_data(); // 加载数据
    do {
        print_menu(); // 打印菜单
        scanf("%d", &choice);
        execute_choice(choice); // 执行用户选择的操作
    } while (choice != 0);
    return 0;
}

// 添加职工
void add_employee() {
    if (num_employees >= MAX_EMPLOYEES) {
        printf("职工数量已达上限,无法添加新职工!\n");
        return;
    }
    Employee employee;
    printf("请输入职工编号:");
    scanf("%d", &employee.id);
    printf("请输入职工姓名:");
    scanf("%s", employee.name);
    printf("请输入职工性别:");
    scanf("%s", employee.gender);
    printf("请输入职工婚姻状况:");
    scanf("%s", employee.marital_status);
    printf("请输入职工学位:");
    scanf("%s", employee.degree);
    printf("请输入职工所属部门:");
    scanf("%s", employee.department);
    printf("请输入职工职位:");
    scanf("%s", employee.position);
    printf("请输入职工工资:");
    scanf("%f", &employee.salary);
    printf("请输入职工工龄:");
    scanf("%d", &employee.work_years);
    employees[num_employees++] = employee;
    printf("职工信息添加成功!\n");
}

// 删除职工
void delete_employee() {
    int id, i, j;
    printf("请输入要删除的职工编号:");
    scanf("%d", &id);
    for (i = 0; i < num_employees; i++) {
        if (employees[i].id == id) {
            for (j = i; j < num_employees - 1; j++) {
                employees[j] = employees[j + 1];
            }
            num_employees--;
            printf("职工信息删除成功!\n");
            return;
        }
    }
    printf("未找到该职工信息!\n");
}

// 修改职工信息
void modify_employee() {
    int id, i;
    printf("请输入要修改的职工编号:");
    scanf("%d", &id);
    for (i = 0; i < num_employees; i++) {
        if (employees[i].id == id) {
            printf("请输入职工姓名:");
            scanf("%s", employees[i].name);
            printf("请输入职工性别:");
            scanf("%s", employees[i].gender);
            printf("请输入职工婚姻状况:");
            scanf("%s", employees[i].marital_status);
            printf("请输入职工学位:");
            scanf("%s", employees[i].degree);
            printf("请输入职工所属部门:");
            scanf("%s", employees[i].department);
            printf("请输入职工职位:");
            scanf("%s", employees[i].position);
            printf("请输入职工工资:");
            scanf("%f", &employees[i].salary);
            printf("请输入职工工龄:");
            scanf("%d", &employees[i].work_years);
            printf("职工信息修改成功!\n");
            return;
        }
    }
    printf("未找到该职工信息!\n");
}

// 打印工资单
void print_payroll() {
    int id, i;
    printf("请输入要打印工资单的职工编号:");
    scanf("%d", &id);
    for (i = 0; i < num_employees; i++) {
        if (employees[i].id == id) {
            printf("职工编号:%d\n", employees[i].id);
            printf("职工姓名:%s\n", employees[i].name);
            printf("职工性别:%s\n", employees[i].gender);
            printf("职工婚姻状况:%s\n", employees[i].marital_status);
            printf("职工学位:%s\n", employees[i].degree);
            printf("职工所属部门:%s\n", employees[i].department);
            printf("职工职位:%s\n", employees[i].position);
            printf("职工工资:%.2f\n", employees[i].salary);
            printf("职工工龄:%d\n", employees[i].work_years);
            return;
        }
    }
    printf("未找到该职工信息!\n");
}

// 打印汇总表
void print_summary() {
    int i;
    float total_salary = 0;
    printf("职工编号\t职工姓名\t职工工资\n");
    for (i = 0; i < num_employees; i++) {
        printf("%d\t\t%s\t\t%.2f\n", employees[i].id, employees[i].name, employees[i].salary);
        total_salary += employees[i].salary;
    }
    printf("总工资:%f\n", total_salary);
}

// 保存数据到文件
void save_data() {
    FILE *fp;
    fp = fopen("employees.dat", "wb");
    if (fp == NULL) {
        printf("文件打开失败!\n");
        return;
    }
    fwrite(&num_employees, sizeof(int), 1, fp);
    fwrite(employees, sizeof(Employee), num_employees, fp);
    fclose(fp);
    printf("数据保存成功!\n");
}

// 从文件中加载数据
void load_data() {
    FILE *fp;
    fp = fopen("employees.dat", "rb");
    if (fp == NULL) {
        printf("文件打开失败!\n");
        return;
    }
    fread(&num_employees, sizeof(int), 1, fp);
    fread(employees, sizeof(Employee), num_employees, fp);
    fclose(fp);
    printf("数据加载成功!\n");
}

// 打印菜单
void print_menu() {
    printf("工资管理系统\n");
    printf("1. 添加职工\n");
    printf("2. 删除职工\n");
    printf("3. 修改职工信息\n");
    printf("4. 打印工资单\n");
    printf("5. 打印汇总表\n");
    printf("6. 保存数据\n");
    printf("7. 加载数据\n");
    printf("0. 退出系统\n");
    printf("请选择操作:");
}

// 执行用户选择的操作
void execute_choice(int choice) {
    if (choice < 0 || choice > 7) {
        printf("无效的选择!\n");
        return;
    }
    if (choice == 0) {
        printf("感谢使用工资管理系统!\n");
        return;
    }
    (*functions[choice - 1])();
}