【发布时间】:2013-04-08 11:33:23
【问题描述】:
我需要这个 C++ 问题的答案,我已经解决了,但显然我遗漏了一些东西,到目前为止我也会发布我的答案......
编写一个程序来计算和打印工资单。
用户输入的是员工姓名、工作小时数和小时工资率。
你必须声明三个函数:
1) 一个用于输入;
2) 一、计算员工工资;和
3) 一张打印工资单
用户必须在变量theEmployee、theHoursWorked和thePayRate中输入员工姓名、工作小时数和小时工资率。变量employee 是string,另外两个变量的类型是float。由于 theEmployee、theHoursWorked 和 thePayRate 的值将在此函数中更改,reference parameters need to be used。
计算函数将接收代表工作小时数和小时工资率的两个参数,进行计算并返回员工的工资。工作时间超过 40 小时的员工的加班费为每小时工资的 1.5 倍。由于函数中的参数没有变化,所以它们应该是值参数。该函数应返回一个代表工资的float 值。
输出函数必须显示用户输入的员工姓名、工作小时数、加班小时数和小时工资率以及员工的工资。对于
例子:
粉红豹的工资单
工作时间:43.5 小时
加班时间:3.5小时
时薪:R125.35
支付:R5672.09
主函数包括一个 for 循环,允许用户重复计算五名员工的工资单。
int main()
{
string theEmployee;
float theHoursWorked;
float thePayRate;
int thePay;
for (int i = 0; i < 5; i++)
{
getData(theEmployee, theHoursWorked, thePayRate);
thePay = calculatePay (theEmployee, theHoursWorked, thePayRate);
printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay);
}
return 0;
}
这就是他们给的,这就是我到目前为止所做的,我想我在参考参数上苦苦挣扎?
#include <iostream>
#include <string>
using namespace std;
int getData (string & theEmployee , float & theHoursWorked, float & thePayRate)
{
cout<< "Enter your name and surname: "<< endl;
getline(cin, theEmployee);
cout << "Include the numbers of hours you worked: " << endl;
cin >> theHoursWorked;
cout << "What is your hourly pay rate?" << endl;
cin >> thePayRate;
return theEmployee, theHoursWorked, thePayRate;
}
float calculatePay( string & theEmployee, float & theHoursWorked, float & thePayRate)
{
float tempPay, thePay, overtimeHours;
if (theHoursWorked > 40)
{
tempPay = 40 * thePayRate;
overtimeHours = theHoursWorked - 40;
thePay = tempPay + overtimeHours;}
else
thePay = theHoursWorked * thePayRate;
return thePay;
}
int printPaySlip( string & theEmployee, float & theHoursWorked, float &
thePayRate, float thePay)
{
float overtimeHours;
cout << "Pay slip for " << theEmployee <<endl;
cout << "Hours worked: "<< theHoursWorked << endl;
if (theHoursWorked > 40)
overtimeHours = theHoursWorked - 40;
else
overtimeHours = 0;
cout << "Overtime hours: "<< overtimeHours << endl;
cout << "Hourly pay rate: " << thePayRate << endl;
cout << "Pay: " << thePay << endl;
cout << endl;
}
int main()
{
string theEmployee;
float theHoursWorked;
float thePayRate;
int thePay;
for (int i = 0; i < 5; i++)
{
getData(theEmployee, theHoursWorked, thePayRate);
thePay = calculatePay (theEmployee, theHoursWorked, thePayRate);
printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay);
}
return 0;
}
【问题讨论】:
-
此外,您没有正确计算加班时间的工资率 - 您应该将
overtimeHours乘以工资率乘以 1.5,而不是简单地将其添加到工资中。
标签: c++ function types parameters return