如何分割只有十个字符的字符串,例如“12345*45688”到一个数组中

发布于 2024-11-05 14:45:08 字数 162 浏览 0 评论 0原文

我正在制作一个简单的计算器,您可以在编辑框中输入值。我需要将字符串分成多个数组,具体取决于总和中有多少个 *+-/ 例如

我有 22+22*22-22/22 我想将其分成五个不同的数组,因为有五个不同的数字组。然后我将把数组 1 添加到数组 2 中,乘以数组 3,减去数组 4,再除以数组 5。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽容 2024-11-12 14:45:08

如果你想阅读类似的内容,特别是如果你想计算数学表达式,你需要的不仅仅是一个数组分割器;你需要一个真正的解析器。正确地做到这一点需要一些编译器理论。我建议您看一下让我们构建一个编译器,该教程涵盖了您将学到的所有内容需要了解表达式解析(以及更多一点,因为他实际上正在构建一个简单的编译器)并使其易于理解。所有示例均采用 Turbo Pascal 编写,因此 Delphi 编码人员应该很容易阅读。

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.

明天过后 2024-11-12 14:45:08

Delphi XE 有一个 SplitString 函数,它完全可以满足您的需要。

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

云归处 2024-11-12 14:45:08

如果您希望获得该方程的结果,您应该尝试使用名为 CalcExpress 的非可视组件。它是免费的,您可以从这里获取:
CalcExpress

下载链接位于页面文本末尾

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

魂ガ小子 2024-11-12 14:45:08

这是一个可以帮助您的功能。

它根据提供的一组预定义字符集将输入字符串分解为子字符串数组。

它将给你一个字符串数组,它将是[“22”,“+”,“22”,“*”,“22”,“-”,“22”,“/”,“22”]。

从那里开始,您必须识别数字和运算符,并且必须根据运算符优先级规则对计算进行分组和执行。

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文