将字符串公式解析为整数结果 [英] Parsing a string formula to an integer result

查看:151
本文介绍了将字符串公式解析为整数结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些解析工作,太复杂,无法进入细节,但有一件简单的事情我需要做(至少简单的概念,也许不是简单的答案)。我可能有一个字符串中的公式,如下面的示例。我需要一个功能,使用一个格式不正确的公式字符串,解析出来并计算结果。



示例:



(10/2)+(10/30)



5+(10/30)



5 + 3



8



或:



(12.5 - (0.5 * 5))/ 2



(12.5 - 2.5)/ 2



10/2



5



规则:




  • 空格将被忽略

  • PEMDAS方法必须完全适用

  • 结果应始终一个四舍五入的整数

  • 没有变量将存在,已经转换为数字

  • 十进制数字可能是输入的一部分



现在的解析我已经在工作,只是得到这些公式字符串中的一个 - 但从那里,我不知道如何执行实际的计算。我正在做的是做一些划线的划线笔画画布,这里有一些示例脚本:

  Var W 50 
Var H 50
笔风格清除
笔颜色$ 000000
笔宽度3
画笔样式固体
画笔颜色{FORM_COLOR}
Rect 0 0 {WIDTH} {HEIGHT}
画笔颜色$ 501010
椭圆@W @H 450 450
画笔颜色$ 602020
椭圆100 100 400 400
画笔颜色$ 703030
椭圆形150 150 350 350
刷子颜色$ 804040
椭圆形200 200 300 300
EllipseG 200 200 300 300 6 2
笔式固体
笔宽2
笔颜色{FONT_COLOR}
MoveTo 0 0
LineTo 500 500
MoveTo 0 500
LineTo 500 0

所有这些都已经有效了,但现在我想说...例如...

 code> Var W 
Var H
设置W 50
设置H 50
画笔颜色$ 602020
椭圆(@W,@H,500 - @W ,500 - @H
设置W 100
设置H 100
画笔颜色$ 703030
Ell ipse(@W,@H,500 - @W,500 - @H
设置W 150
设置H 150
画笔颜色$ 804040
椭圆(@W,@H, 500 - @W,500 - @H

所以 500 - @W 我已将其转换为 500 - 50 ,但现在我需要发送字符串 500 - 50 进入此功能以获得结果 450



编辑:解决 / p>

我按照推荐使用 parser10 ,它完美适用于我需要做的工作。以下是我的脚本代码示例代码:

  Var S 4 
Var D @S
Var L 0
Var T 0
Var R {WIDTH}
Var B {HEIGHT}
笔风格清除
笔颜色$ 00000000
笔宽3
画笔风格固体
画笔颜色{FORM_COLOR}
矩形0 0 {宽度} {HEIGHT}

设置D @ D + @ S
画笔颜色$ 00400000
Ellipse @ L + @ D @ T + @ D @R - (@ D * 2)@B - (@ D * 2)
设置D @ D + @ S
画笔颜色$ 00501010
椭圆@ L + @ D @ T + @ D @R - (@ D * 2)@B - (@ D * 2)
设置D @ D + @ S
画笔颜色$ 00602020
椭圆@ L + @D @ T + @ D @R - (@ D * 2)@B - (@ D * 2)
设置D @ D + @ S
画笔颜色$ 00703030
椭圆@ L + @ D @ T + @ D @R - (@ D * 2)@B - (@ D * 2)
设置D @ D + @ S
画笔颜色$ 00804040
椭圆@ L + @ D @ T + @D @R - (@ D * 2)@B - (@ D * 2)
设置D @ D + @ S
画笔颜色$ 00905050
椭圆@ L + @ D @ T + @ D @R - (@ D * 2)@B - (@ D * 2)
设置D @ D + @ S
画笔颜色$ 00FB6060
椭圆@ L + @ D @ T + @ D @ R - (@ D * 2)@B - (@ D * 2)

@是一个变量,我使用 StringReplace()根据 Var 部分中声明的内容进行转换。

解决方案

Parser10 ,最初由Renate Schaaf为Delphi 1编写,后来由Alin Flaider和Stefan Hoffmeister升级到Delphi2和3,这是一个相当简单的数学解析器。它将为简单的表达做好工作,可以作为更复杂工作的样板。



它不是为了速度而建。



更新:



Hallvard Vassbotn更新了Parser10,并添加了一些文档。从此处载入。


I'm doing some parsing work, too complex to get into details, but there's one simple thing I need to do (at least simple concept, maybe not simple answer). I might have a formula in a string such as the samples below. I need a function that will take a loosly-formatted formula string, parse it out, and calculate the result.

Sample:

(10 / 2)+(10/30)

5+(10/30)

5+3

8

Or:

(12.5 - (0.5 * 5)) / 2

(12.5 - 2.5) / 2

10 / 2

5

Rules:

  • Spaces are to be ignored
  • PEMDAS method must entirely apply
  • Result shall be always a rounded Integer
  • No variables will exist, already converted to numbers
  • Decimal numbers might be part of the input

The existing parsing I already have working just gets one of these formula strings - but from there, I have no clue how to perform the actual calculations. What I'm doing specifically is doing a little line deliminated scripter to draw to a canvas, here's some sample script:

Var W 50
Var H 50
Pen Style Clear
Pen Color $000000
Pen Width 3
Brush Style Solid
Brush Color {FORM_COLOR}
Rect 0 0 {WIDTH} {HEIGHT}
Brush Color $501010
Ellipse @W @H 450 450
Brush Color $602020
Ellipse 100 100 400 400
Brush Color $703030
Ellipse 150 150 350 350
Brush Color $804040
Ellipse 200 200 300 300
EllipseG 200 200 300 300 6 2
Pen Style Solid
Pen Width 2
Pen Color {FONT_COLOR}
MoveTo 0 0
LineTo 500 500
MoveTo 0 500
LineTo 500 0

All that already works, but now I want to say for example...

Var W
Var H
Set W 50
Set H 50
Brush Color $602020
Ellipse(@W, @H, 500 - @W, 500 - @H
Set W 100
Set H 100
Brush Color $703030
Ellipse(@W, @H, 500 - @W, 500 - @H
Set W 150
Set H 150
Brush Color $804040
Ellipse(@W, @H, 500 - @W, 500 - @H

So the 500 - @W I already convert it to 500 - 50 but now I need to send the string 500 - 50 into this function to get the result 450.

EDIT: Solved

I'm using the parser10 as recommended, and it works perfectly for what I need to do. Here's a sample snippet of my script code:

Var S 4
Var D @S
Var L 0
Var T 0
Var R {WIDTH}
Var B {HEIGHT}
Pen Style Clear
Pen Color $00000000
Pen Width 3
Brush Style Solid
Brush Color {FORM_COLOR}
Rect 0 0 {WIDTH} {HEIGHT}

Set D @D+@S
Brush Color $00400000
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)
Set D @D+@S
Brush Color $00501010
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)
Set D @D+@S
Brush Color $00602020
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)
Set D @D+@S
Brush Color $00703030
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)
Set D @D+@S
Brush Color $00804040
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)
Set D @D+@S
Brush Color $00905050
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)
Set D @D+@S
Brush Color $00FB6060
Ellipse @L+@D @T+@D @R-(@D*2) @B-(@D*2)

Anything starting with an @ is a variable, which I do a StringReplace() to convert all those based on what was declared in the Var section.

解决方案

Parser10, written originally by Renate Schaaf for Delphi 1 and later upgraded to Delphi2 and 3 by Alin Flaider and Stefan Hoffmeister, is a fairly simple math parser. It will do the job for simple expressions and can serve as a boilerplate for more complex work.

It is not built for speed though.

Update :

Hallvard Vassbotn made an update of Parser10 and added some documentation. Load it from here.

这篇关于将字符串公式解析为整数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆