开发者

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

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 开发者_如何学Cin the sum for instance

I have 22+22*22-22/22 I want to break that into five different arrays because there are five different groups of numbers. Then later I am going to add array1 to array two multiply that by array3 and subtract that by array4 and divide that by array 5.


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜