How to split a string of only ten characters e.g.“12345*45688” into an array

后端 未结 4 439
梦毁少年i
梦毁少年i 2021-01-13 05:41

I\'m making a simple calculator where you type values into an edit box. I need to split the string into a number of arrays depending on how many *+-/ there are in the sum fo

相关标签:
4条回答
  • 2021-01-13 06:29

    If you wish to get the result of that equation, you should try a non-visual component, called CalcExpress. It's free and you can get it from here: CalcExpress

    Download link is at the end of the page text

    0 讨论(0)
  • 2021-01-13 06:30

    If you want to read something like that, especially if you want to evaluate mathematical expressions, you need more than just an array-splitter; you need a real parser. Doing it right requires a bit of compiler theory. I'd recommend you take a look at Let's Build A Compiler, a tutorial that covers everything you'll need to know about expression parsing (and a bit more, since he's actually building a simple compiler) and makes it easy to understand. All examples are in Turbo Pascal, so it should be easy for a Delphi coder to read.

    0 讨论(0)
  • 2021-01-13 06:36

    Delphi XE has a SplitString function that does exactly what you need.

    0 讨论(0)
  • 2021-01-13 06:42

    Here's a function which may help you on the way.

    It breaks down an input string into an array of sub-strings, based upon a provided set of pre-defined character sets.

    It will give you an array of strings, which will be ["22", "+", "22", "*", "22", "-", "22", "/", "22"].

    From there on you'll have to identify the numbers and the operators, and you'll have to group and execute the calculations according to the rules for operator precedence.

    TCharSet = Set of Char;
    TStringArray = Array of String;
    
    function GetSubStrings(InputString: String; CharacterSets: Array of TCharSet): TStringArray;
    // Get Sub-strings
    var
      Index: Integer;
      Character: Char;
      SubString: String;
      SubStringArray: TStringArray;
      CharacterSetIndex: Integer;
      PreviousCharacterSetIndex: Integer;
    begin
      // Get
      SubString := '';
      SetLength(SubStringArray, 0);
      PreviousCharacterSetIndex := -1;
      for Index := 1 to Length(InputString) do
      begin
        // Character
        Character := InputString[Index];
    
        // Character Set Index
        CharacterSetIndex := GetCharacterSet(Character, CharacterSets);
    
        // Add
        if (CharacterSetIndex = PreviousCharacterSetIndex) or (Index = 1) then
          // Add Character to SubString
          SubString := SubString + Character
        else
        begin
          // Add SubString To SubString Array
          SetLength(SubStringArray, Length(SubStringArray) + 1);
          SubStringArray[Length(SubStringArray) - 1] := SubString;
    
          // New SubString
          SubString := Character;
        end;
    
        // Previous Character Set Index
        PreviousCharacterSetIndex := CharacterSetIndex;
    
        // Add last SubString
        if Index = Length(InputString)  then
        begin
          // Add SubString To SubString Array
          SetLength(SubStringArray, Length(SubStringArray) + 1);
          SubStringArray[Length(SubStringArray) - 1] := SubString;
        end;
      end;
    
      // Result
      Result := SubStringArray;
    end; 
    
    function GetCharacterSet(Character: Char; CharacterSets: Array of TCharSet): Integer;
    // Get Character Set
    var
      Index: Integer;
      CharacterSet: TCharSet;
    begin
      // Get
      Result := -1;
      for Index := 0 to Length(CharacterSets) - 1 do
      begin
        // Character Set
        CharacterSet := CharacterSets[Index];
    
        // Check
        if Character in CharacterSet then
        begin
          // Result
          Result := Index;
    
          // Break
          Break;
        end;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题