我想在 C# 中构建基于文本的计算器
假设我有文本
Add two plus three. its arithmetic expression will be (2+3)
Add five thousand two hundred to four hundred thirty two divided by thirty four.((5200+430)/34).
Add three million to seven million then add five then two.(((3000000+7000000)+5)+2)
等等。
执行此操作的算法是什么?
最佳答案
可能您必须对文本进行解析。这个想法是从你的字符串中找出标记(感兴趣的文本)。假设文本是 Add two plus three,您找到单独的标记,假设您找到的标记是 Add, two, plus, three
。将标记替换为您预定义的数字,例如标记 two
= 2
等等。用算术运算符替换算术文本,例如 Add = +
等。最后执行计算出的表达式得到结果。
关于c# - 如何将文本转换为算术表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089598/