如何分割只有十个字符的字符串,例如“12345*45688”到一个数组中
我正在制作一个简单的计算器,您可以在编辑框中输入值。我需要将字符串分成多个数组,具体取决于总和中有多少个 *+-/ 例如
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想阅读类似的内容,特别是如果你想计算数学表达式,你需要的不仅仅是一个数组分割器;你需要一个真正的解析器。正确地做到这一点需要一些编译器理论。我建议您看一下让我们构建一个编译器,该教程涵盖了您将学到的所有内容需要了解表达式解析(以及更多一点,因为他实际上正在构建一个简单的编译器)并使其易于理解。所有示例均采用 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.
Delphi XE 有一个 SplitString 函数,它完全可以满足您的需要。
Delphi XE has a SplitString function that does exactly what you need.
如果您希望获得该方程的结果,您应该尝试使用名为 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
这是一个可以帮助您的功能。
它根据提供的一组预定义字符集将输入字符串分解为子字符串数组。
它将给你一个字符串数组,它将是[“22”,“+”,“22”,“*”,“22”,“-”,“22”,“/”,“22”]。
从那里开始,您必须识别数字和运算符,并且必须根据运算符优先级规则对计算进行分组和执行。
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.