unit ut_EdocuAt163com;
{
分析设计:
求和时总是以修约后的整数部分为准
整数部分,小数部分设计缓存,原值在程序中并不是最根本的,但仍要设计缓存区
和值溢出验证
当A的小数部分小于0.5时,与谁相加呢,这是需求描述的不完整(猜一下,A的整部)
godfa同学虽然还不擅长写代码,其需求工作十分到位准确
}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Math;
type
TNumRec = record
SrcValue: Double; {原值}
NewValue: Double; {新值}
Int: Integer; {整部}
Frac: Double; {小数部}
end;
procedure FixStartUp(A, B, C, D, E: Double); //入口
procedure XInit;
procedure Eval;
procedure Print;
implementation
var
X: array[0..4] of TNumRec;
i: Integer;
Sum: Double;
Index: Integer; {BCDE谁进位}
Msg: string;
{入口}
procedure FixStartUp(A, B, C, D, E: Double);
begin
X[0].SrcValue := A;
X[1].SrcValue := B;
X[2].SrcValue := C;
X[3].SrcValue := D;
X[4].SrcValue := E;
Xinit;
repeat
Eval;
until (Sum = 100);
{打印}
Print;
end;
{初始化}
procedure Xinit;
begin
for i := 0 to 4 do
begin
X.Int := Trunc(X.SrcValue);
X.Frac := X.SrcValue - X.Int;
X.NewValue := X.SrcValue;
end;
{修理A,进位后重设整数部分,小数部分}
if (0.5 <= X[0].Frac) then
begin
X[0].NewValue := Ceil(X[0].NewValue);
X[0].Int := X[0].Int + 1;
X[0].Frac := 0.0;
end;
end;
{评估,修正}
procedure Eval;
begin
{求和}
Sum := 0;
for i := 0 to 4 do
begin
Sum := Sum + X.Int;
end;
if (100 < Sum) then
begin
Application.MessageBox('输入可能有错误,和值溢出100', '', MB_ICONERROR);
Abort;
end;
{若和值不足,选定一个数进位,进位后重设整数部分,小数部分}
if (Sum <> 100) then
begin
Index := 1;
for i := 2 to 4 do
begin
if (X[Index].Frac < X.Frac) then
begin
Index := i;
end;
end;
X[Index].Int := X[Index].Int + 1;
X[Index].Frac := 0.0;
X[Index].NewValue := X[Index].Int;
end;
end;
procedure Print;
begin
Msg := '';
for i := 0 to 4 do
begin
Msg := Msg + Format('%0:s> %1:.1f ==> %2:.1f'#13#10, [Char(65+i), X.SrcValue, X.NewValue]);
end;
ShowMessage(Msg);
end;
end.