编辑代码

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

// 员工信息结构体
typedef struct {
    char department[50];  // 部门
    int id;               // 工号
    char name[50];        // 姓名
    float base_salary;    // 基本工资
    float bonus;          // 奖金
    float deduction;      // 扣发工资
    float actual_salary;  // 实发工资
} Employee;

// 全局变量
Employee employees[100];  // 最多存储100名员工
int employee_count = 0;   // 当前员工数量

// 函数声明
void add_employee();
void search_employee();
void modify_employee();
void delete_employee();
void sort_employees();
void display_all();
void calculate_salary(Employee *emp);

int main() {
    int choice;
    
    while(1) {
        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: add_employee(); break;
            case 2: search_employee(); break;
            case 3: modify_employee(); break;
            case 4: delete_employee(); break;
            case 5: sort_employees(); break;
            case 6: display_all(); break;
            case 0: exit(0);
            default: printf("无效选择,请重新输入!\n");
        }
    }
    
    return 0;
}

// 计算实发工资
void calculate_salary(Employee *emp) {
    emp->actual_salary = emp->base_salary + emp->bonus - emp->deduction;
}

// 添加员工
void add_employee() {
    if(employee_count >= 100) {
        printf("员工数量已达上限!\n");
        return;
    }
    
    Employee new_emp;
    
    printf("\n请输入员工信息:\n");
    printf("部门: ");
    scanf("%s", new_emp.department);
    printf("工号: ");
    scanf("%d", &new_emp.id);
    printf("姓名: ");
    scanf("%s", new_emp.name);
    printf("基本工资: ");
    scanf("%f", &new_emp.base_salary);
    printf("奖金: ");
    scanf("%f", &new_emp.bonus);
    printf("扣发工资: ");
    scanf("%f", &new_emp.deduction);
    
    calculate_salary(&new_emp);
    
    // 检查工号是否已存在
    for(int i = 0; i < employee_count; i++) {
        if(employees[i].id == new_emp.id) {
            printf("工号已存在!\n");
            return;
        }
    }
    
    employees[employee_count++] = new_emp;
    printf("员工添加成功!\n");
}

// 查询员工
void search_employee() {
    int choice, id;
    char name[50], department[50];
    int found = 0;
    
    printf("\n查询方式:\n");
    printf("1. 按工号查询\n");
    printf("2. 按姓名查询\n");
    printf("3. 按部门查询\n");
    printf("请选择查询方式: ");
    scanf("%d", &choice);
    
    switch(choice) {
        case 1:
            printf("请输入工号: ");
            scanf("%d", &id);
            for(int i = 0; i < employee_count; i++) {
                if(employees[i].id == id) {
                    printf("\n员工信息:\n");
                    printf("部门: %s\n", employees[i].department);
                    printf("工号: %d\n", employees[i].id);
                    printf("姓名: %s\n", employees[i].name);
                    printf("基本工资: %.2f\n", employees[i].base_salary);
                    printf("奖金: %.2f\n", employees[i].bonus);
                    printf("扣发工资: %.2f\n", employees[i].deduction);
                    printf("实发工资: %.2f\n", employees[i].actual_salary);
                    found = 1;
                    break;
                }
            }
            break;
            
        case 2:
            printf("请输入姓名: ");
            scanf("%s", name);
            for(int i = 0; i < employee_count; i++) {
                if(strcmp(employees[i].name, name) == 0) {
                    printf("\n员工信息:\n");
                    printf("部门: %s\n", employees[i].department);
                    printf("工号: %d\n", employees[i].id);
                    printf("姓名: %s\n", employees[i].name);
                    printf("基本工资: %.2f\n", employees[i].base_salary);
                    printf("奖金: %.2f\n", employees[i].bonus);
                    printf("扣发工资: %.2f\n", employees[i].deduction);
                    printf("实发工资: %.2f\n", employees[i].actual_salary);
                    found = 1;
                }
            }
            break;
            
        case 3:
            printf("请输入部门: ");
            scanf("%s", department);
            for(int i = 0; i < employee_count; i++) {
                if(strcmp(employees[i].department, department) == 0) {
                    printf("\n员工信息:\n");
                    printf("部门: %s\n", employees[i].department);
                    printf("工号: %d\n", employees[i].id);
                    printf("姓名: %s\n", employees[i].name);
                    printf("基本工资: %.2f\n", employees[i].base_salary);
                    printf("奖金: %.2f\n", employees[i].bonus);
                    printf("扣发工资: %.2f\n", employees[i].deduction);
                    printf("实发工资: %.2f\n", employees[i].actual_salary);
                    found = 1;
                }
            }
            break;
            
        default:
            printf("无效选择!\n");
            return;
    }
    
    if(!found) {
        printf("未找到匹配的员工!\n");
    }
}

// 修改员工信息
void modify_employee() {
    int id, found = 0;
    
    printf("\n请输入要修改的员工工号: ");
    scanf("%d", &id);
    
    for(int i = 0; i < employee_count; i++) {
        if(employees[i].id == id) {
            printf("\n当前员工信息:\n");
            printf("部门: %s\n", employees[i].department);
            printf("工号: %d\n", employees[i].id);
            printf("姓名: %s\n", employees[i].name);
            printf("基本工资: %.2f\n", employees[i].base_salary);
            printf("奖金: %.2f\n", employees[i].bonus);
            printf("扣发工资: %.2f\n", employees[i].deduction);
            printf("实发工资: %.2f\n", employees[i].actual_salary);
            
            printf("\n请输入新的信息(不修改则直接回车):\n");
            
            char input[50];
            float temp;
            
            printf("部门: ");
            scanf("%s", input);
            if(strlen(input) > 0) {
                strcpy(employees[i].department, input);
            }
            
            printf("姓名: ");
            scanf("%s", input);
            if(strlen(input) > 0) {
                strcpy(employees[i].name, input);
            }
            
            printf("基本工资: ");
            scanf("%f", &temp);
            if(temp > 0) {
                employees[i].base_salary = temp;
            }
            
            printf("奖金: ");
            scanf("%f", &temp);
            if(temp > 0) {
                employees[i].bonus = temp;
            }
            
            printf("扣发工资: ");
            scanf("%f", &temp);
            if(temp > 0) {
                employees[i].deduction = temp;
            }
            
            calculate_salary(&employees[i]);
            printf("员工信息修改成功!\n");
            found = 1;
            break;
        }
    }
    
    if(!found) {
        printf("未找到该工号的员工!\n");
    }
}

// 删除员工
void delete_employee() {
    int id, found = 0;
    
    printf("\n请输入要删除的员工工号: ");
    scanf("%d", &id);
    
    for(int i = 0; i < employee_count; i++) {
        if(employees[i].id == id) {
            // 将后面的员工前移
            for(int j = i; j < employee_count - 1; j++) {
                employees[j] = employees[j + 1];
            }
            employee_count--;
            printf("员工删除成功!\n");
            found = 1;
            break;
        }
    }
    
    if(!found) {
        printf("未找到该工号的员工!\n");
    }
}

// 排序员工
void sort_employees() {
    int choice;
    
    printf("\n排序方式:\n");
    printf("1. 按工号排序\n");
    printf("2. 按部门排序\n");
    printf("3. 按实发工资排序\n");
    printf("请选择排序方式: ");
    scanf("%d", &choice);
    
    switch(choice) {
        case 1:
            // 按工号排序
            for(int i = 0; i < employee_count - 1; i++) {
                for(int j = 0; j < employee_count - 1 - i; j++) {
                    if(employees[j].id > employees[j + 1].id) {
                        Employee temp = employees[j];
                        employees[j] = employees[j + 1];
                        employees[j + 1] = temp;
                    }
                }
            }
            printf("按工号排序完成!\n");
            break;
            
        case 2:
            // 按部门排序
            for(int i = 0; i < employee_count - 1; i++) {
                for(int j = 0; j < employee_count - 1 - i; j++) {
                    if(strcmp(employees[j].department, employees[j + 1].department) > 0) {
                        Employee temp = employees[j];
                        employees[j] = employees[j + 1];
                        employees[j + 1] = temp;
                    }
                }
            }
            printf("按部门排序完成!\n");
            break;
            
        case 3:
            // 按实发工资排序
            for(int i = 0; i < employee_count - 1; i++) {
                for(int j = 0; j < employee_count - 1 - i; j++) {
                    if(employees[j].actual_salary < employees[j + 1].actual_salary) {
                        Employee temp = employees[j];
                        employees[j] = employees[j + 1];
                        employees[j + 1] = temp;
                    }
                }
            }
            printf("按实发工资排序完成!\n");
            break;
            
        default:
            printf("无效选择!\n");
            return;
    }
}

// 显示所有员工
void display_all() {
    if(employee_count == 0) {
        printf("暂无员工信息!\n");
        return;
    }
    
    printf("\n所有员工信息:\n");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", 
           "部门", "工号", "姓名", "基本工资", "奖金", "扣发", "实发工资");
    
    for(int i = 0; i < employee_count; i++) {
        printf("%-10s %-10d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n", 
               employees[i].department, employees[i].id, employees[i].name, 
               employees[i].base_salary, employees[i].bonus, employees[i].deduction, 
               employees[i].actual_salary);
    }
}