【问题标题】:Swapping elements in Edit1在 Edit1 中交换元素
【发布时间】:2016-04-06 13:11:34
【问题描述】:

我有一个Edit1,您可以在其中输入像2 20 10 -3 这样的数字,当您单击Button1 时,它会显示最大和最小数字 max = 20 min = -3。 尝试这样做时,当我拿出数字时,它会在 Edit1 中交换 minmax 数字,就像 2 -3 10 20 一样,我以我的方式尝试了它,但其他输入的数字改变了位置。 我尝试了很多方法:

 Edit4.Text:= (inttostr(min)+' '+ inttostr(max)); 

但它会覆盖其他数字。

然后我尝试使用

maxnumb := Edit4

Edit4.Text  := StringReplace(maxnumb, inttostr(max), inttostr(min),
                          [rfReplaceAll, rfIgnoreCase]); 

Edit1.Text  := StringReplace(maxnumb, inttostr(min), inttostr(max),
                          [rfReplaceAll, rfIgnoreCase]); 

但它只交换了第一个数字,当我再次单击Button1 时,它交换了第二个数字。

没有尝试交换的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  oSL: TStringlist;
  s, ss: string;
  a: array [1 .. 15] of integer;
  i, j, k, p, code: integer;
  max, min: integer;
  before, after: string;
begin
  s := Edit1.Text;
  s := concat(s, #32);
  i := 0;
  while Length(s) > 0 do
  begin
    i := i + 1;
    p := pos(#32, s);
    ss := copy(s, 1, p - 1);
    Val(ss, k, code);
    a[i] := k;
    delete(s, 1, p);
  end;
  // Max
  max := a[1];
  For j := 1 to i do
    if max < a[j] then
      max := a[j];
  // Min
  min := a[1];
  For j := 1 to i do
    if min > a[j] then
      min := a[j];
  // Put out Max/Min
  Edit3.Text := IntToStr(max);
  Edit2.Text := IntToStr(min);
end;

【问题讨论】:

  • edit你的问题,不清楚:交换2 20 10 -3中的最小值和最大值不会返回2 -3 20 10而是2 -3 10 20
  • 为了解决这个问题,请停止您的 StringReplace 尝试。只需解析 Edit.Text 中的所有数字,对整数数组进行处理,然后将它们全部连接回 Edit.text
  • maxnumb 是什么类型?如果它是一个字符串,那么第一行 "maxnumb := edit4" 将不会编译。如果它是一个 Editbox,那么 stringreplace 将不会编译。请检查您的问题中是否包含正确的代码
  • 是的,maxnumb 是一个字符串,我会尝试在没有字符串替换的情况下做一些事情。
  • 现在我得到一个错误 unit1.pas(72,23) 错误:为调用“CompareReal”function CompareReal(List: TStringList; Index1, Index2: Integer): Integer; begin // make use of the comparer in unit Math Result := CompareValue(StrToFloat(List[Index1]), StrToFloat(List[Index2])); end; 指定的参数数量错误@

标签: delphi pascal lazarus


【解决方案1】:
uses
  Types, StrUtils;

function Arrange(const AEditFrom, AEditTo: TEdit): Boolean;
var
  _StrArr: TStringDynArray;
  i: integer;
  _IntArr: array of integer;
  _IntValue: integer;
  _Min: integer;
  _Max: integer;
begin
  Result := False;

  if not Assigned(AEditFrom) then
    Exit;
  if not Assigned(AEditTo) then
    Exit;

  _StrArr := SplitString(AEditFrom.Text, ' ');
  SetLength(_IntArr, Length(_StrArr));

  for i := 0 to Length(_StrArr) - 1 do
  begin
    if not TryStrToInt(_StrArr[i], _IntValue) then
      Exit;

    _IntArr[i] := _IntValue;
  end;

  AEditTo.Clear;
  _Min := _IntArr[0];
  _Max := _IntArr[0];
  for i := 0 to Length(_IntArr) - 1 do
  begin
    if _IntArr[i] > _Max then
      _Max := _IntArr[i];

    if _IntArr[i] < _Min then
      _Min := _IntArr[i];
  end;

  AEditTo.Text := StringReplace(AEditFrom.Text, ' ' + IntToStr(_Min),
    '...' + IntToStr(_Max), [rfReplaceAll, rfIgnoreCase]);

  AEditTo.Text := StringReplace(AEditTo.Text, ' ' + IntToStr(_Max),
    ' ' + IntToStr(_Min), [rfReplaceAll, rfIgnoreCase]);

  AEditTo.Text := StringReplace(AEditTo.Text, '...', ' ',
    [rfReplaceAll, rfIgnoreCase]);

  Result := True;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if not Arrange(Edit1, Edit2) then
    ShowMessage('Something went wrong. List contains not a integer?');
end;

测试:2 20 10 -3,结果:2 -3 10 20

【讨论】:

  • 一旦我使用代码,就找不到标识符 SplitString
  • SplitString 应该在这个单元中找到,如答案所示:System.StrUtils.SplitString
猜你喜欢
  • 1970-01-01
  • 2012-09-16
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多