C ++加班和薪资计算

时间:2014-04-11 01:27:57

标签: c++

当您执行我的代码时,您会注意到第一个示例的常规工资将计算为500美元,而它只能计算到400美元。我还有一个问题,即将常规工资和加班工资的总和相加,以获得在一个cout声明中显示的总工资单。 95%的代码是正确的,我只是有这两个问题,任何帮助都表示赞赏。为了更清楚听到我的任务:

员工的正常工作时间是每周40小时。员工有时候会工作。当员工工作40小时或更短时间时,常规工资按小时工作小时数计算。当员工工作时间超过40小时后,员工的工资有两个组成部分:a)定期工资(40小时X小时费率),2)加班工资(加班时间超过40倍时间和一小时费率的一半)。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std; 

struct incomeInfo {
    string id;
    string name;
    int hours;
    double hRate;
    double regPay = 0;
    double otPay = 0;
};


const int ARRAY_SIZE = 25; // Array size 
incomeInfo income[ARRAY_SIZE]; // array variable declaration

void getIncome(incomeInfo[], int&);
void compute(incomeInfo *, int);
void display(incomeInfo[], int);
void summary(incomeInfo[], int);

int main()
{

    incomeInfo income[ARRAY_SIZE];
    int count = 0;                          //Initialize count to 0

    getIncome(income, count);
    compute(income, count);
    display(income, count);
    summary(income, count);
    return 0;
}

void getIncome(incomeInfo income[], int &count)
{

    ifstream inputFile;                 // declare input file variable
    char line[50];                      // Variable to read data

    // Open data File to read data
    inputFile.open("Payroll.txt");

    // test if data file opened correctly
    if (inputFile.fail())
    {
        cout << "\n\n\tError openning file: " << "\n\n\t";
        system("pause");
        exit(1);
    }

    else
    {
        while (!inputFile.eof())    //Check end of file
        {
            inputFile.getline(line, 50, ',');       // The data are separated by comma 
            income[count].id = line;
            inputFile.getline(line, 50, ',');
            income[count].name = line;
            inputFile.getline(line, 50, ',');
            income[count].hours = atoi(line);       // Convert string to integer
            inputFile.getline(line, 50, ',');
            income[count].hRate = atof(line);           // Convert string to float

            count++;
        }
    }
    inputFile.close();

    return;
}


void compute(incomeInfo *ptrI, int count)    
{                                                
    for (int i = 0; i<count; i++)

    if (ptrI->hours <= 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI++;
    }

    else if (ptrI->hours > 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
        ptrI++;
    }
        return;
}

void display(incomeInfo income[], int count)
{
    cout << fixed << showpoint << setprecision(2);
    cout << setw(15) << left << "ID" << setw(16) << "Name";
    cout << left << setw(8) << "Hours" << setw(14) << "Hourly Rate" << setw(14) << "Regular Pay" << setw(14) << "Overtime Pay" <<endl;


    for (int i = 0; i < count; i++)
    {
        cout << setw(14) << left << income[i].id << setw(15) << income[i].name;
        cout << right << setw(6) << income[i].hours << setw(12) << income[i].hRate;
        cout << setw(14) << income[i].regPay << setw(14) << income[i].otPay << endl;
    }

    return;
}


void summary(incomeInfo income[], int count)
{

    for (int i = 0; i < count; i++)
        cout << endl << endl << "Total payroll amount for the company = " << income[i].regPay + income[i].otPay <<endl;

    system("PAUSE");
}

1 个答案:

答案 0 :(得分:1)

我认为这是您想要的解决方案。在else if的{​​{1}}部分,您需要撰写compute()。我已经评论了你上一行。如果您愿意,也可以仅使用ptrI->regPay = 40 * ptrI->hRate;代替else。请检查并告诉我们:

else if (ptrI->hours > 40)