我知道我的if语句在某处不正确,但我不知道在哪里?还是只是逻辑错误?
8小时后,任何工作时间将被支付一半时间。也就是说,给定每小时工资乘以1.5。该工资是在8小时后的几个小时内支付的。
10小时后,任何工作时间将加倍支付。也就是说,给定每小时工资乘以2.0。该工资在10小时后数小时内支付。 请显示:(示例输出)
每小时工资:12.37 工作时间:10.3
付款(0到8小时):98.96
支付8至10小时的费用):37.11
每小时支付费用(10或更多):7.42
总工资总额:143.49
// Example program
#include <iostream>
#include <string>
using namespace std;
double HoursWorked;
double WagePerHour;
double TotalWages;
double TimeAndHalf;
double Overtime;
char ContinueChar;
//test cases: 10.5 hours @ 12/hour = $96.00, 2 hours at 1.5 rate = $36.00, .5 hour at 2.0 rate = $12.00
// 6.3 hours @ 12/hour = 75.6, no hours of overtime or double time
//12.5 hours @ 14.34/ hour = $114.72, 2 hours at 1.5 rate = 43.02, 2.5 hours at 2.0 rate = $71.70
//3.7 hours @ 19/hour = $70.30
// 14 hours @ 23.50/hour = $188, 2 hours at 1.5 rate = $70.50, 4 hours at 2.0 rate = $188
//I tested with test test cases and the program had the same results.
int main()
{
cout << "Ticket #64220\n";
cout << "CMPR-120\n";
cout << "Instructor : Joel Kirscher\n";
cout << "Student: Seyed Shariat\n";
cout << "Payroll Overtime";
cout << "\n\n";
do {
cout << "How many hours did you work this pay period: \n";
cin >> HoursWorked;
cout << "What wage do you get paid per hour?: \n";
cin >> WagePerHour;
cout << "So you your paycheck will be: " << HoursWorked * WagePerHour << " before taxes are taken out. \n";
if (HoursWorked > 8 && <= 10)
cout << "For the hours you worked over 8, and less than or equal to 10 you made: " << HoursWorked * 1.5 * WagePerHour << " \n";
else (HoursWorked >10);
cout << "For the hours you worked over 10: " << HoursWorked * 2.0 * WagePerHour << " \n";
cout << "Do you want this to run again? (y=Yes): ";
cin >> ContinueChar;
} while (ContinueChar == 'y' || ContinueChar == 'Y');
cin.get();
return 0;
}
答案 0 :(得分:1)
有很多解决方法。这是一个。这些评论可以帮助您遵循我的推理。幸运的是,其背后的数学不是很复杂。
/*
We need to find the area under this pay rate / hours worked graph.
The following approach divides the graph into three rectangles as shown below
2.0 | +--+
| | |
1.5 | +-+--|
| | |
1.0 +-------+----|
| |
0.5 | |
| |
0.0 +------------+
0 8 10
*/
// Calculate basic pay before any overtime consideration
Pay = HoursWorked * HourlyRate;
// Hours above 8 earn half-again more than the standard wage.
// (This will be only positive if more than 8 hours were worked.)
OvertimePay = (HoursWorked - 8.0) * HourlyRate * 0.5;
if(OvertimePay > 0.0)
Pay += OvertimePay;
// Hours above 10 earn an additional 50% extra
// (This will be only positive if more than 10 hours were worked.)
OvertimePay = (HoursWorked - 10.0) * HourlyRate * 0.5;
if(OvertimePay > 0.0)
Pay += OvertimePay;