i have edit1
can type numbers 2 20 10 -3
, when click button1
brings out max , min numbers max = 20 min = -3. tried make when bring out numbers swaps min
, max
numbers in edit1
2 -3 10 20
tried in ways other entered numbers change place. tried many ways:
edit4.text:= (inttostr(min)+' '+ inttostr(max));
but overwrite other numbers.
then tried use
maxnumb := edit4 edit4.text := stringreplace(maxnumb, inttostr(max), inttostr(min), [rfreplaceall, rfignorecase]); edit1.text := stringreplace(maxnumb, inttostr(min), inttostr(max), [rfreplaceall, rfignorecase]);
but swaped 1st number , when clicked button1
again swaped second number.
code without attempts swap:
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); := 0; while length(s) > 0 begin := + 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]; j := 1 if max < a[j] max := a[j]; // min min := a[1]; j := 1 if min > a[j] min := a[j]; // put out max/min edit3.text := inttostr(max); edit2.text := inttostr(min); end;
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) exit; if not assigned(aeditto) exit; _strarr := splitstring(aeditfrom.text, ' '); setlength(_intarr, length(_strarr)); := 0 length(_strarr) - 1 begin if not trystrtoint(_strarr[i], _intvalue) exit; _intarr[i] := _intvalue; end; aeditto.clear; _min := _intarr[0]; _max := _intarr[0]; := 0 length(_intarr) - 1 begin if _intarr[i] > _max _max := _intarr[i]; if _intarr[i] < _min _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) showmessage('something went wrong. list contains not integer?'); end;
test: 2 20 10 -3, result: 2 -3 10 20
Comments
Post a Comment