0

I need some calculator with Delphi and sometimes my number include '<' and '>'. I need to store them for after usage. For example:

fnc_ProcessNumber('0.320>','100',multiplication)

This needs to give this output: 320>, but this line fails:

boolControl := ContainsText(strEnteredResult,[lessThan,greaterThan]);

I also try this:

boolControl := IndexStr(strEnteredResult,[lessThan,greaterThan]);

Here's my code and type.

type TMaths  =  (addition,subtraction,multiplication,division);

function fnc_ProcessNumber(strEnteredResult,strProcessedNumber:string;Math:TMaths) : string;
const lessThan : string = '<';
const greaterThan : string = '>';
var
  intIndexCounter,intIndexMath:Integer;
  extNewResult:Extended;
  boolControl,boolLess,boolGreater:Boolean;
begin
  Result := 'null';
  boolControl := ContainsText(strEnteredResult,[lessThan,greaterThan]);
  if boolControl then
  begin
    case intIndexCounter of
      0:
        begin
          intIndexMath := AnsiPos(lessThan,strEnteredResult);
          boolLess := True;
        end;
      1:
        begin
          intIndexMath := AnsiPos(greaterThan,strEnteredResult);
          boolGreater := True;
        end;
    end;
    strProcessedNumber := Copy(strEnteredResult,intIndexMath,1);
  end
  else
  begin
    extNewResult := StrToFloat(strEnteredResult);
  end;
  case Math of
    addition:
      begin
        extNewResult := extNewResult + StrToFloat(strProcessedNumber);
      end;
    subtraction:
      begin
        extNewResult := Abs(extNewResult - StrToFloat(strProcessedNumber));
      end;
    multiplication:
      begin
        extNewResult := extNewResult * StrToFloat(strProcessedNumber);
      end;
    division:
      begin
        extNewResult := extNewResult / StrToFloat(strProcessedNumber);
      end;
  end;
  Result       := FloatToStr(extNewResult);
  if boolLess then Result := lessThan+Result;
  else if boolGreater then Result := Result+greaterThan;
end;
2
  • For your next question, please try to isolate a single question. For instance, "Given a string S and an array Arr of strings, how do I find out whether or not there exists an element A in Arr such that A is a substring of S" without even mentioning the calculator part. Commented Dec 2, 2021 at 0:10
  • Shouldn't it be 32> and not 320> Commented Dec 2, 2021 at 9:06

1 Answer 1

3

Well, ContainsText clearly cannot be used, since its second argument is a single string, not an array of strings.

And IndexStr cannot be used either, since it merely tests whether the text is (exactly) one of the elements of the array; it doesn't do any substring testing. In addition, it returns an integer, the index of the match or -1, so you would need to compare the result against <> -1 to get a boolean. Or, you can use MatchStr which does this test for you.

Finally, ContainsText is case-insensitive while IndexStr is not. It is better to compare ContainsText with IndexText/MatchText and ContainsStr with IndexStr/MatchStr:

Case sensitive Case insensitive
Substring found in string? ContainsStr ContainsText
Index of string in string array. IndexStr IndexText
String found in string array? (Index <> -1) MatchStr MatchText

Hence, none of these functions tests (1) if a string is a substring of any string in a string array or (2) if any string in a string array is a substring of a given string.

But of course, it is completely trivial to write such a function. Here's for the second case:

function ContainsAnyStr(const AText: string; const AStrings: array of string): Boolean;
begin
  for var i := 0 to High(AStrings) do
    if ContainsStr(AText, AStrings[i]) then
      Exit(True);
  Result := False;
end;

function ContainsAnyText(const AText: string; const AStrings: array of string): Boolean;
begin
  for var i := 0 to High(AStrings) do
    if ContainsText(AText, AStrings[i]) then
      Exit(True);
  Result := False;
end;

In addition, since your string array is actually a character array, you can use TStringHelper.IndexOfAny:

'123>'.IndexOfAny(['<', '>']) <> -1
Sign up to request clarification or add additional context in comments.

3 Comments

And, of course, there are several other errors in your code. For instance, intIndexCounter is an uninitialized variable.
Or maybe you are looking for var HasMark := S.EndsWith('>') or S.EndsWith('<'); or var HasGT := S.EndsWith('>'); var HasLT := S.EndsWith('<'); var HasMark := HasGT or HasLT; Or use Contains instead of EndsWith if that is what you want.
IndexOfAny did the job. Thanks for advice either @andreas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.