gpt4 book ai didi

delphi - 我怎么知道为什么我的程序计算和输出不正确?

转载 作者:行者123 更新时间:2023-12-03 18:53:51 26 4
gpt4 key购买 nike

我正在处理一个编程问题。

注意:这不是学生项目。我正在为网站 Try My Quest Dot Com 开发一个新的 Quest,我是该网站的管理员。

问题:

Jenny 刚开始担任 Justine 的 Java Workshop 的程序员。她得到 10 美元
一个小时,除了少数异常(exception)。在一天中工作超过 8 小时的任何时间,她每小时可额外赚取 1.50 美元,而在任何一周内工作超过 40 小时的时间,每小时可额外赚取 2.50 美元。此外,她周六工作可获得 125% 的奖金,周日工作可获得 50% 的奖金。周六和周日的奖金是根据当天的工作时间计算的;它们不用于计算每周工作超过 40 小时的任何奖金。您将获得 Jenny 在一周(周日、周一等)中每天工作的小时数,并且您需要计算她本周的工资。输入将是小于或等于 24 的正整数。输出必须使用美元符号格式化并四舍五入到最接近的一分钱。例如,$2"和 $2.136666"是错误答案;正确的版本分别是 $2.00"和 $2.14"。

无论如何,我正在尝试用 Delphi 写这个(无表单项目)。我向程序传递了一个命令行参数 - timecard.dat

输入

0, 8, 8, 8, 8, 8, 0
0, 10, 10, 10, 10, 10, 0
0, 0, 8, 8, 8, 8, 8
0, 0, 0, 10, 10, 10, 10
10, 10, 10, 9, 9, 9, 9

输出
Output #1: $400.00
Output #2: $540.00
Output #3: $500.00
Output #4: $540.75
Output #5: $905.88

然而,我的输出是:
Output #1: $400.00
Output #2: $540.00
Output #3: $500.00

Output #4: $537.00
Output #5: $902.50

我的最后两个输出值与实际结果不同。不知道为什么,我盯着代码看的越多,我看到的就越少

谁能告诉我我做错了什么?
program ACSL_Time_Cards;
{assumes Sunday = 1, Monday 3, etc}
uses
SysUtils,
Dialogs;

const
HourlyWage = 10.00;
OverEightWage = 1.50;
OverFortyWage = 2.50;
var
F: TextFile;
I, ArrayIndex: Integer;
WeeklyHours: Array[0..6] of Integer; //weekly hours
HourStr, LineStr: String;
TotalHours, TotalOverFortyHours, TotalOverEightHours, TotalSatHours, TotalSunHours: Integer;
TotalWages: Real;
begin
//initialize variables
TotalHours:= 0;
TotalOverEightHours:= 0;
TotalOverFortyHours:= 0;
TotalSatHours:= 0;
TotalSunHours:= 0;
TotalWages:= 0.00;
ArrayIndex:= 0;
//open file "timecard.dat" for input
if FileExists(ParamStr(1)) then
begin
AssignFile(F, ParamStr(1));
Reset(F);
//step through file and extract each line and store in hoursStr
while not EOF(F) do
begin
Readln(F, LineStr);
//step through hours string and fill Array with weekly hours
for I:= 1 to length(LineStr) do
begin
//if character is not a ',' then add it to hourStr
if LineStr[I] <> ',' then
HourStr:= HourStr + LineStr[I]
else
begin
//add HourStr to Array
WeeklyHours[ArrayIndex]:= StrToInt(HourStr);
//reset the variable
HourStr:= '';
//increment Variable
Inc(ArrayIndex);
end; //else
end; //for I:= 1 to length(HoursStr) do
//clean up by adding the last remaining one
WeeklyHours[ArrayIndex]:= StrToInt(HourStr);
//step through array and figure out overtime Daily and Weekly
for I:= Low(WeeklyHours) to High(WeeklyHours) do
begin
TotalHours:= TotalHours + WeeklyHours[I];
if WeeklyHours[I] > 8 then
TotalOverEightHours:= TotalOverEightHours + WeeklyHours[I]-8;
//get sunday hours
if I + 1 = 1 then
TotalSunHours:= TotalSunHours + WeeklyHours[I];
//get saturday hours
if I + 1 = 7 then
TotalSatHours:= TotalSatHours + WeeklyHours[I];
end;
//get total over 40 hours
if TotalHours > 40 then
TotalOverFortyHours:= TotalHours-40;
//compute Regular Hours
TotalWages:= TotalWages + TotalHours * 10.00;
//compute overtime hours

TotalWages:= TotalWages + TotalOverEightHours * 1.50;
TotalWages:= TotalWages + TotalOverFortyHours * 2.50;
//compute bonuses
TotalWages:= TotalWages + (TotalSatHours * 10.00) * 1.25;
TotalWages:= TotalWages + (TotalSunHours * 10.00) * 0.50;

ShowMessage('TotalWages: ' + FormatFloat('$0.00', TotalWages));
//reset variables
TotalWages:= 0.00;
TotalHours:= 0;
TotalOverEightHours:= 0;
TotalOverFortyHours:= 0;
TotalSatHours:= 0;
TotalSunHours:= 0;
HourStr:= '';
ArrayIndex:= 0;
end; //while not EOF(F) do
CloseFile(F);
end
else
ShowMessage('File does not exist!');
end.

我相信有很多方法可以写得更好。我真的只是对为什么我的值与预期值不同感兴趣。谢谢!

最佳答案

对于像这样的简单问题,您可能希望将其手写出来,然后查看您的代码是否遵循与您相同的步骤。

对于输出 4,周六 125% 的奖金不包括 8 点后每小时 1.50 美元的额外费用:

她应该挣

Wed: $103    | $100 for 10 hours plus $3 for 2 hours over 8
Thu: $103 | $100 for 10 hours plus $3 for 2 hours over 8
Fri: $103 | $100 for 10 hours plus $3 for 2 hours over 8
Sat: $231.75 | ($100 for 10 hours, $3 for 2 hours over 8), $128.75 for 125% bonus

总计 540.75

关于delphi - 我怎么知道为什么我的程序计算和输出不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5423030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com