You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++员工薪资程序优化需求:循环控制、薪资计算、输入校验等问题

C++员工薪资程序优化完成版

需求优化点

  • 支持用户输入员工数量,以此控制循环录入次数
  • 修复薪资计算逻辑,确保薪资正确计算并存储
  • 新增输入校验:
    • 姓名字段不允许包含数字
    • 工时、时薪字段仅接受有效数值,拒绝字母等非数字输入
  • 循环录入时添加序号标识,提升用户体验

优化后完整代码

#include <iostream>
#include <cstdlib>
#include <cctype>
#include <string>

using namespace std;

void displayRules()
{
    cout << "=====================" << endl;
    cout << "   EMPLOYEE-SALARY   " << endl;
    cout << "=====================" << endl;
    cout << " " << endl;
}

// 校验姓名是否不含数字
bool isValidName(const string& name) {
    for (char c : name) {
        if (isdigit(c)) {
            return false;
        }
    }
    return !name.empty();
}

// 校验数值输入是否有效
bool isValidNumber(double& num) {
    if (cin.fail() || num < 0) {
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    return true;
}

int main()
{
    char ans;
    do
    {
        system("cls");
        displayRules();

        struct Employee
        {
            string name;
            double hours;
            double rate;
            double salary;
            Employee *next;
            Employee *prev;
        };

        Employee *head = NULL;
        Employee *newEmployee;
        Employee *EmpPointer;

        string inpname;
        int empCount;
        double inphours;
        double inprate;

        // 输入并校验员工数量
        cout << "请输入员工数量: ";
        while (!(cin >> empCount) || empCount <= 0) {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "输入无效,请输入正整数: ";
        }

        for(int ctr = 0; ctr < empCount; ctr++)
        {
            cout << "\n" << ctr + 1 << ". 输入姓名、工时、时薪" << endl;

            // 输入并校验姓名
            do {
                cout << "Enter Name: \t\t";
                cin >> inpname;
                if (!isValidName(inpname)) {
                    cout << "姓名不能包含数字,请重新输入!" << endl;
                }
            } while (!isValidName(inpname));

            // 输入并校验工时
            do {
                cout << "Enter # Hours Worked: \t";
                cin >> inphours;
                if (!isValidNumber(inphours)) {
                    cout << "工时必须是非负数字,请重新输入!" << endl;
                }
            } while (!isValidNumber(inphours));

            // 输入并校验时薪
            do {
                cout << "Enter Rate per Hour: \t";
                cin >> inprate;
                if (!isValidNumber(inprate)) {
                    cout << "时薪必须是非负数字,请重新输入!" << endl;
                }
            } while (!isValidNumber(inprate));

            // 创建员工对象并计算薪资
            newEmployee = new Employee;
            newEmployee->name = inpname;
            newEmployee->hours = inphours;
            newEmployee->rate = inprate;
            newEmployee->salary = inphours * inprate; // 修复薪资计算
            newEmployee->next = NULL;
            newEmployee->prev = NULL;

            // 添加到链表
            if (head == NULL) {
                head = newEmployee;
            } else {
                EmpPointer = head;
                while (EmpPointer->next) {
                    EmpPointer = EmpPointer->next;
                }
                EmpPointer->next = newEmployee;
                newEmployee->prev = EmpPointer;
            }
        }

        cout << endl;
        Employee *displayPointer = head;

        system("cls");

        cout << "------------------------------------------------------------" << endl;
        cout << "                    =Summary of PAYROLL=                    " << endl;
        cout << "------------------------------------------------------------" << endl;
        cout << "Employee Name\t" << "# Hours Worked\t" << "Rate/Hour\t" << "Salary\t" << endl;

        while (displayPointer)
        {
            cout << displayPointer->name << "\t\t";
            cout << displayPointer->hours << "\t\t";
            cout << displayPointer->rate << "\t\t";
            cout << displayPointer->salary << endl;
            displayPointer = displayPointer->next;
        }
        cout << "------------------------------------------------------------" << endl;

        cout << endl;
        cout << "Would you like to run the program again? (Y/N) ";
        cin >> ans;

        // 清理链表内存,避免内存泄漏
        while (head != NULL) {
            Employee* temp = head;
            head = head->next;
            delete temp;
        }
    }
    while (ans == 'y' || ans == 'Y');

    return 0;
}

优化说明

  1. 员工数量控制:新增员工数量输入及校验逻辑,确保输入为正整数,循环次数由该值决定
  2. 薪资计算修复:在创建员工对象时直接计算salary = hours * rate,并赋值给成员变量,解决原程序中薪资未正确计算的问题
  3. 输入校验:
    • 新增isValidName函数遍历姓名字符,确保不含数字
    • 新增isValidNumber函数校验数值输入,处理非数字输入及负数情况,输入错误时清空输入缓冲区并提示重新输入
  4. 序号标识:循环中通过ctr + 1显示当前录入的员工序号,提升交互清晰度
  5. 额外优化:添加链表内存清理逻辑,避免多次运行程序导致的内存泄漏

内容的提问来源于stack exchange,提问作者Otaku Sensei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:30:53