我现在有六个edit控件。我现在需要是在里面分别填入数值。然后求出最大值和最小值。请问应该怎么实现在?
解决方案 »
- 关于delphi的1stclass4000ProVcl7组件使用,
- combobox的style小问题
- 急问一个小问题:能防止 DBGrid 自动取下一个记录吗?
- 如何设置打印机成脱机状态?--------菜鸟的问题
- DBLookupComboboxEh的下拉列表问题
- MSCOMM 收到btye型数据后不会显示
- 高分求解数据库问题,一表对多表的问题。敬请大家赐教!!(200分哟)
- 50分寻求一个身份验证程序:在Edit2中输入的数据是否与表Table1匹配的查询程序。(不用SQL语句)
- fastreport自定义函数的删除
- 转盘游戏(高手指教)
- dbexpress 中连sybase没有connectionstring?
- 请教大家一个关于释放远程进程模块的问题
minValue,maxValue:Integer;
begin
minValue := StrToInt(Edit1.text);
if StrToInt(Edit2.text)< minValue then minValue := StrToInt(Edit2.text);
if StrToInt(Edit3.text)< minValue then minValue := StrToInt(Edit3.text);
...
maxValue:= StrToInt(Edit1.text);
if StrToInt(Edit2.text)> minValue then minValue := StrToInt(Edit2.text);
if StrToInt(Edit3.text)> minValue then minValue := StrToInt(Edit3.text);
...
end;也可使用 TList 来管理界面edit控件
minValue,maxValue:Integer;
begin
minValue := StrToInt(Edit1.text);
maxValue := StrToInt(Edit1.text);
minValue := Min(minvalue,StrToInt(Edit2.text));
maxValue := Max(minvalue,StrToInt(Edit2.text));
...
end;
..........
beginend
//取窗体所有edit组件的最大值,最小值.
procedure TForm1.Button1Click(Sender: TObject);
var
iCount,i : integer; //组件数量
minValue,MaxValue:Double; //最小值,最大值
editValue: Double; //当前Edit值
begin
i := 0;
for iCount:=0 to ComponentCount-1 do
begin
// 遍历组件.
if Components[iCount] is TEdit then
begin
try
editValue:= StrToFloat(Tedit(Components[iCount]).Text);
except
editValue := 0;
Application.MessageBox('数据只能为数字!', '提示', MB_OK +
MB_ICONWARNING);
exit;
end;
//查询到是否是每一个Edit组件
if i = 0 then
begin
minValue := editValue;
MaxValue := editValue;
Inc(i);
end
else
begin
if editValue= 0 then
Continue; if minValue> editValue then
minValue := editValue;
if MaxValue < editValue then
MaxValue:= editValue;
end;
end;
end;
Application.MessageBox(
PChar('MaxValue='+FormatFloat('###0.##',MaxValue )+#13#10 +'MinValue='+FormatFloat('###0.##',minValue ) ),
'提示', MB_OK + MB_ICONINFORMATION);
end;
maxNumber,minNumber,number:integer;
begin
maxNumber:=0;
number:=0;
if (eidt1.text<>'0') and (eidt1.text<>' ') then
//不为空;不为0 注意只有Edit1.text 有值时才可以得出,要想它为空其它不为空时有值就重写
//其它edit就行了
begin
maxNumber:=strtoint(trim(eidt1.text));
minNumber:=strtoint(trim(eidt1.text));
if (eidt2.text<>'0') and (eidt2.text<>' ')
maxNumber:=Max(maxNumber,trim(eidt2.text))
maxNumber:=Max(maxNumber,trim(eidt2.text))if (eidt3.text<>'0') and (eidt3.text<>' ')
maxNumber:=Max(maxNumber,trim(eidt3.text))
maxNumber:=Max(maxNumber,trim(eidt3.text))if (eidt4.text<>'0') and (eidt4.text<>' ')
maxNumber:=Max(maxNumber,trim(eidt4.text))
maxNumber:=Max(maxNumber,trim(eidt4.text))if (eidt5.text<>'0') and (eidt5.text<>' ')
maxNumber:=Max(maxNumber,trim(eidt5.text))
maxNumber:=Max(maxNumber,trim(eidt5.text))if (eidt6.text<>'0') and (eidt6.text<>' ')
maxNumber:=Max(maxNumber,trim(eidt6.text))
maxNumber:=Max(maxNumber,trim(eidt6.text))end;end;
//最大值
function getmax(a,b:Double):Double
begin
result := (a+b)/2 +(a-b)/2;
end;
//最小值
function getmin(a,b:Double):Double
begin
result := (a+b)/2 -(a-b)/2;
end;
这个在大数据量的时候运算速度比IF判断要快的多.也不需要写那么多的IF判断直接
procedure TForm1.Button1Click(Sender: TObject);
var
iCount,i : integer; //组件数量
minValue,MaxValue:Double; //最小值,最大值
editValue: Double; //当前Edit值
begin
i := 0;
for iCount:=0 to ComponentCount-1 do
begin
// 遍历组件.
if Components[iCount] is TEdit then
begin
try
editValue:= StrToFloat(Tedit(Components[iCount]).Text);
except
editValue := 0;
exit;
end;
//查询到是否是每一个Edit组件
if i = 0 then
begin
minValue := editValue;
MaxValue := editValue;
Inc(i);
end
else
begin
if editValue= 0 then
Continue;
MaxValue := getmax(editValue,MaxValue);
minValue := getmin(editValue,MinValue)
end;
end;
end;
Application.MessageBox(
PChar('MaxValue='+FormatFloat('###0.##',MaxValue )+#13#10 +'MinValue='+FormatFloat('###0.##',minValue ) ),
'提示', MB_OK + MB_ICONINFORMATION);
end;
因为你不确定你是肯定有六个参与运算.所以只能写到这里了. 否则的话还有别的算法可以优化.不要写IF判断都可以.