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

标签 delphi debugging

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

注意:这不是学生项目。我正在为网站 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/

相关文章:

c# - Silverlight -- Debug.WriteLine() 不适用于长字符串

javascript - 如何将 TCanvas->Arc 值转换为 SVG Arc

mysql - 如何在delphi中解压mysql字符串

json - Delphi - 在修复 VCL 错误时,单元 x 是用不同版本的 x 编译的

r - 如何安装包以便我可以使用调试器单步调试它的 R 代码?

powershell - 如何在交互式 Powershell 远程处理 session 中使用 cdb.exe

delphi - 如何在非 unicode Delphi 版本中构造带有变音符号的 WideString?

Delphi XE2 DataSnap - 访问服务器方法模块中的 REST 连接属性

c++ - 使用 clang 时在 gdb 中评估 libc++ 的方法

android - 无法落入框架?