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

IEl*_*ite 1 delphi debugging

我正在研究编程问题.

注意:这不是学生项目.我正在为这个网站Try My Quest Dot Com的新Quest工作,我是管理员.

问题:

Jenny刚开始担任Justine Java Workshop的程序员.除少数例外,她每小时收费10美元.她每天工作超过8小时,每小时额外收取1.50美元,而在任何一周内,每小时额外收费2.50美元,超过40小时.此外,她在周六工作时获得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
Run Code Online (Sandbox Code Playgroud)

产量

Output #1: $400.00
Output #2: $540.00
Output #3: $500.00
Output #4: $540.75
Output #5: $905.88
Run Code Online (Sandbox Code Playgroud)

然而,我的出局是:

Output #1: $400.00
Output #2: $540.00
Output #3: $500.00

Output #4: $537.00
Output #5: $902.50
Run Code Online (Sandbox Code Playgroud)

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

谁能告诉我我做错了什么?

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.
Run Code Online (Sandbox Code Playgroud)

我确信有很多方法可以更好地编写.我真的只是为什么我的价值观与预期值不同而感兴趣.谢谢!

Dav*_*nan 6

代码将受益于I/O和分离的计算.你的问题与计算有关.我写的是这样的:

uses
  Math;

type
  TDay = (
    daySunday,
    dayMonday,
    dayTuesday,
    dayWednesday,
    dayThursday,
    dayFriday,
    daySaturday
  );
  TDayArray = array [TDay] of Integer;

function Wage(const Hours: TDayArray): Double;
const
  BasicRate = 10.0;
  DailyOvertimeRate = 1.5;
  WeeklyOvertimeRate = 2.5;
  DailyOvertimeThreshold = 8;
  WeeklyOvertimeThreshold = 40;
  DailyBonus: array [TDay] of Double = (1.5, 1.0, 1.0, 1.0, 1.0, 1.0, 2.25);
var
  Day: TDay;
  DailyOvertimeHours, WeeklyOvertimeHours, TotalHours: Double;
  DailyPay: array [TDay] of Double;
begin
  TotalHours := 0.0;
  for Day := low(Day) to high(Day) do begin
    TotalHours := TotalHours + Hours[Day];
    DailyOvertimeHours := Max(Hours[Day]-DailyOvertimeThreshold, 0.0);
    DailyPay[Day] := Hours[Day]*BasicRate;
    DailyPay[Day] := DailyPay[Day] + DailyOvertimeHours*DailyOvertimeRate;
    DailyPay[Day] := DailyPay[Day]*DailyBonus[Day];
  end;
  WeeklyOvertimeHours := Max(TotalHours-WeeklyOvertimeThreshold, 0.0);
  Result := Sum(DailyPay) + WeeklyOvertimeHours*WeeklyOvertimeRate;
end;
Run Code Online (Sandbox Code Playgroud)

这仍然有点粗糙,我对薪资率,加班等的变量名称不太满意.

一旦有了这样的实用功能,那么将它与程序的其余部分放在一起就会变得容易多了.

你当前计划中最大的弱点是所有东西都被安置在一个巨大的例程中.将它分解成小块,你就能够比在一个大型例程中寻找问题更容易验证这些小块.


jas*_*nny 6

对于像这样的简单问题,您可能希望手动编写它,然后查看您的代码是否遵循您执行的相同步骤.

对于输出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
Run Code Online (Sandbox Code Playgroud)

共计 540.75