C++ 员工工资管理系统

2023-12-27 20:48:57

+员工工资管理系统

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

// 定义员工结构体
typedef struct {
    char name[20];
    int age;
    float salary;
} Employee;

// 声明全局变量
Employee* employees = NULL;  // 员工数组指针
int numEmployees = 0;  // 员工数量

// 函数声明
void login();
void menu();
void addEmployee();
void displayEmployees();
void searchEmployee();
void deleteEmployee();
void modifyEmployee();
void calculateTotalSalary();
void saveData();
void loadData();

int main() {
    login();  // 登录验证
    loadData();  // 加载数据

    menu();  // 显示菜单

    saveData();  // 保存数据
    free(employees);  // 释放内存

    return 0;
}

// 登录验证
void login() {
    char username[20];
    char password[20];
    int attempts = 3;

    printf("=== 员工工资管理系统 ===\n");
    printf("请输入用户名和密码进行登录\n");

    while (attempts > 0) {
        printf("用户名: ");
        scanf("%s", username);
        printf("密码: ");
        scanf("%s", password);

        // TODO: 进行用户名和密码验证逻辑,这里仅作示例直接验证为admin/admin
        if (strcmp(username, "admin") == 0 && strcmp(password, "admin") == 0) {
            printf("登录成功!\n");
            return;
        } else {
            printf("用户名或密码错误,请重试!\n");
            attempts--;
        }
    }

    printf("登录失败,退出系统。\n");
    exit(0);
}

// 显示菜单
void menu() {
    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("7. 退出系统\n");
        printf("请输入选项数字: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addEmployee();
                break;
            case 2:
                displayEmployees();
                break;
            case 3:
                searchEmployee();
                break;
            case 4:
                deleteEmployee();
                break;
            case 5:
                modifyEmployee();
                break;
            case 6:
                calculateTotalSalary();
                break;
            case 7:
                return;
            default:
                printf("无效的选项,请重新输入。\n");
        }
    }
}

// 录入员工信息
void addEmployee() {
    printf("\n=== 录入员工信息 ===\n");

    // 增加员工数量
    numEmployees++;
    employees = (Employee*)realloc(employees, numEmployees * sizeof(Employee));

    Employee* employee = &employees[numEmployees - 1];

    printf("姓名: ");
    scanf("%s", employee->name);
    printf("年龄: ");
    scanf("%d", &(employee->age));
    printf("工资: ");
    scanf("%f", &(employee->salary));

    printf("录入成功!\n");
}

// 显示员工信息
void displayEmployees() {
    printf("\n=== 显示员工信息 ===\n");

    if (numEmployees == 0) {
        printf("没有员工信息。\n");
        return;
    }

    for (int i = 0; i < numEmployees; i++) {
        Employee* employee = &employees[i];
        printf("姓名: %s, 年龄: %d, 工资: %.2f\n", employee->name, employee->age, employee->salary);
    }
}

// 查找员工信息
void searchEmployee() {
    printf("\n=== 查找员工信息 ===\n");

    if (numEmployees == 0) {
        printf("没有员工信息。\n");
        return;
    }

    char name[20];
    printf("请输入要查找的员工姓名: ");
    scanf("%s", name);

    int found = 0;
    for (int i = 0; i < numEmployees; i++) {
        Employee* employee = &employees[i];
        if (strcmp(employee->name, name) == 0) {
            printf("姓名: %s, 年龄: %d, 工资: %.2f\n", employee->name, employee->age, employee->salary);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("未找到该员工。\n");
    }
}

// 删除员工信息
void deleteEmployee() {
    printf("\n=== 删除员工信息 ===\n");

    if (numEmployees == 0) {
        printf("没有员工信息。\n");
        return;
    }

    char name[20];
    printf("请输入要删除的员工姓名: ");
    scanf("%s", name);

    int found = 0;
    for (int i = 0; i < numEmployees; i++) {
        Employee* employee = &employees[i];
        if (strcmp(employee->name, name) == 0) {
            // 将最后一个员工信息替换到当前位置
            employees[i] = employees[numEmployees - 1];
            // 减少员工数量
            numEmployees--;
            // 重新分配内存
            employees = (Employee*)realloc(employees, numEmployees * sizeof(Employee));

            printf("删除成功!\n");
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("未找到该员工。\n");
    }
}

// 修改员工信息
void modifyEmployee() {
    printf("\n=== 修改员工信息 ===\n");

    if (numEmployees == 0) {
        printf("没有员工信息。\n");
        return;
    }

    char name[20];
    printf("请输入要修改的员工姓名: ");
    scanf("%s", name);

    int found = 0;
    for (int i = 0; i < numEmployees; i++) {
        Employee* employee = &employees[i];
        if (strcmp(employee->name, name) == 0) {
            printf("请输入新的员工信息:\n");
            printf("姓名: ");
            scanf("%s", employee->name);
            printf("年龄: ");
            scanf("%d", &(employee->age));
            printf("工资: ");
            scanf("%f", &(employee->salary));

            printf("修改成功!\n");
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("未找到该员工。\n");
    }
}

// 统计工资总额
void calculateTotalSalary() {
    printf("\n=== 统计工资总额 ===\n");

    float totalSalary = 0.0;
    for (int i = 0; i < numEmployees; i++) {
        Employee* employee = &employees[i];
        totalSalary += employee->salary;
    }

    printf("工资总额: %.2f\n", totalSalary);
}

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

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

    fclose(fp);
}

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

    fread(&numEmployees, sizeof(int), 1, fp);
    employees = (Employee*)malloc(numEmployees * sizeof(Employee));
    fread(employees, sizeof(Employee), numEmployees, fp);

    fclose(fp);
}

文章来源:https://blog.csdn.net/laocooon/article/details/135254595
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。