胡
胡一刀007
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个很简单的程序,中间递归出了个问题,想不明白,请大家帮帮我!先谢了!
目的:想知道所输入的数值在哪两个值之间,程序返回所属数值段中较小的那个值。
这个Project中含两单元:
unit halfform;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Shared;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
function FindComm(Start_Count:integer;End_Count:integer;In_Comm:extended):extended;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.FindComm(Start_Count:integer;End_Count:integer;In_Comm:extended):extended;
var
AimCount:integer;
Div_Count:integer;
begin
AimCount:=Start_Count+End_Count;
Div_Count:=AimCount Div 2;
if Mer_Comm_Arr[Start_Count]>Mer_Comm_Arr[End_Count] then
Result:=-1
//异常
//如果In_Comm刚好在两单元之间,则找到
if (In_Comm < Mer_Comm_Arr[Div_Count+1])
and (In_Comm > Mer_Comm_Arr[Div_Count]) then
Result:=Mer_Comm_Arr[Div_Count];
//如果头尾相逢或In_Comm等于节点值,则找到
if (Div_Count=End_Count)
or (In_Comm = Mer_Comm_Arr[Div_Count]) then
Result:=Mer_Comm_Arr[Div_Count];《—当输入In_Comm为1时,就提示“非法浮点操作”,为什么?
//如果目标值小于中节点值,则中节点以下递归
if In_Comm < Mer_Comm_Arr[Div_Count] then
Result:=FindComm(Start_Count,Div_Count,In_Comm);
//如果目标值大于中+1节点值,则中+1节点以上递归
if In_Comm > Mer_Comm_Arr[Div_Count+1] then
Result:=FindComm(Div_Count+1,End_Count,In_Comm);
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
Aim_Comm:extended;
begin
Aim_Comm:=StrToCurr(Edit1.Text);
StatusBar1.Panels[0].Text:=FloatToStr(FindComm(0,Length(Mer_Comm_Arr)-1,Aim_Comm));
end;
end.
共同单元:
unit Shared;
interface
var
Mer_Comm_Arr:Array[0..3] of extended;
implementation
begin
Mer_Comm_Arr[0]:=0.005;
Mer_Comm_Arr[1]:=0.01;
Mer_Comm_Arr[2]:=0.02;
Mer_Comm_Arr[3]:=0.1;
end.
理想的结果应是:
输入1,会显示:0.1
输入0.001,会显示:0.005
输入0.007,会显示:0.005
可能你会觉得有些奇怪,但因为这程序只是一个初产品,也就是说会进行二次加工,故才这样做。
目的:想知道所输入的数值在哪两个值之间,程序返回所属数值段中较小的那个值。
这个Project中含两单元:
unit halfform;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Shared;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
function FindComm(Start_Count:integer;End_Count:integer;In_Comm:extended):extended;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.FindComm(Start_Count:integer;End_Count:integer;In_Comm:extended):extended;
var
AimCount:integer;
Div_Count:integer;
begin
AimCount:=Start_Count+End_Count;
Div_Count:=AimCount Div 2;
if Mer_Comm_Arr[Start_Count]>Mer_Comm_Arr[End_Count] then
Result:=-1
//异常
//如果In_Comm刚好在两单元之间,则找到
if (In_Comm < Mer_Comm_Arr[Div_Count+1])
and (In_Comm > Mer_Comm_Arr[Div_Count]) then
Result:=Mer_Comm_Arr[Div_Count];
//如果头尾相逢或In_Comm等于节点值,则找到
if (Div_Count=End_Count)
or (In_Comm = Mer_Comm_Arr[Div_Count]) then
Result:=Mer_Comm_Arr[Div_Count];《—当输入In_Comm为1时,就提示“非法浮点操作”,为什么?
//如果目标值小于中节点值,则中节点以下递归
if In_Comm < Mer_Comm_Arr[Div_Count] then
Result:=FindComm(Start_Count,Div_Count,In_Comm);
//如果目标值大于中+1节点值,则中+1节点以上递归
if In_Comm > Mer_Comm_Arr[Div_Count+1] then
Result:=FindComm(Div_Count+1,End_Count,In_Comm);
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
Aim_Comm:extended;
begin
Aim_Comm:=StrToCurr(Edit1.Text);
StatusBar1.Panels[0].Text:=FloatToStr(FindComm(0,Length(Mer_Comm_Arr)-1,Aim_Comm));
end;
end.
共同单元:
unit Shared;
interface
var
Mer_Comm_Arr:Array[0..3] of extended;
implementation
begin
Mer_Comm_Arr[0]:=0.005;
Mer_Comm_Arr[1]:=0.01;
Mer_Comm_Arr[2]:=0.02;
Mer_Comm_Arr[3]:=0.1;
end.
理想的结果应是:
输入1,会显示:0.1
输入0.001,会显示:0.005
输入0.007,会显示:0.005
可能你会觉得有些奇怪,但因为这程序只是一个初产品,也就是说会进行二次加工,故才这样做。