解析数学表达式 [英] parsing of mathematical expressions

查看:301
本文介绍了解析数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(c90)(linux)



输入:

  sqrt(2-sin(3 * A / B)^ 2.5)+ 0.5 *(C *〜(D)+ 3.11 + B)
a
b / * a,b,c ,d * /
c
d

输入:

$ b $ (2 * asin(3 * A / B)^ 2.5)+ cos(0.5 *(C *〜(D))+ 3.11 + B)
a
b / *有一个值为a,b,c,d * /
c
d

输入:

  sqrt(2  -  sin(3 * A / B)^ 2.5 )/(0.5 *(C *〜(D))+ sin(3.11)+ ln(B))
/ * max lenght的公式是250个字符* /
a
b / * there是a,b,c,d * /
c / *的值,每个变量带有一组浮点数* /
d

正如你可以看到输入中的公式取决于用户。
我的程序将采用公式和n元组值。
然后计算a,b,c和d的每个值的结果。
如果你想我在说,程序的结果是图形。
/ 有时候,我想我将输入并存储在字符串。
然后出现另一个想法我应该在结构中存储公式
但是不知道如何在结构的基础上构造
代码。
/



真的,我不知道如何在程序代码中存储公式,以便
我可以做我的工作。
可以显示我吗?

  / * a,b,c,d是字母
cos, sin,sqrt,ln是函数* /


解决方案

编写一个词汇分析器来对输入进行标记(将其分解为其组件 - 运算符,标点符号,标识符等)。不可避免地,你会得到一些令牌序列。



之后,有很多方法来评估输入。执行此操作的最简单方法之一是使用分流码算法(后缀表达式的评估易于用资本E)。


(in c90) (linux)

input:

sqrt(2 - sin(3*A/B)^2.5) + 0.5*(C*~(D) + 3.11 +B)
a
b   /*there are values for a,b,c,d */
c
d

input:

cos(2 - asin(3*A/B)^2.5) +cos(0.5*(C*~(D)) + 3.11 +B)
a
b   /*there are values for a,b,c,d */
c
d

input:

sqrt(2 - sin(3*A/B)^2.5)/(0.5*(C*~(D)) + sin(3.11) +ln(B))
 /*max lenght of formula is 250 characters*/
a
b   /*there are values for a,b,c,d */
c   /*each variable with set of floating numbers*/
d

As you can see infix formula in the input depends on user. My program will take a formula and n-tuples value. Then it calculate the results for each value of a,b,c and d. If you wonder I am saying ;outcome of program is graph. /sometimes,I think i will take input and store in string. then another idea is arise " I should store formula in the struct" but ı don't know how I can construct the code on the base of structure./

really, I don't know way how to store the formula in program code so that I can do my job. can you show me?

/* a,b,c,d is letters
 cos,sin,sqrt,ln is function*/

解决方案

You need to write a lexical analyzer to tokenize the input (break it into its component parts--operators, punctuators, identifiers, etc.). Inevitably, you'll end up with some sequence of tokens.

After that, there are a number of ways to evaluate the input. One of the easiest ways to do this is to convert the expression to postfix using the shunting yard algorithm (evaluation of a postfix expression is Easy with a capital E).

这篇关于解析数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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