数学EX pressions解析 [英] parsing of mathematical expressions

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

问题描述

(以C90)(Linux)的

输入:

 的sqrt(2  - 罪(3 * A / B)^ 2.5)+ 0.5 *(C *〜(D)+ 3.11 + B)
一个
B / *有一个值,B,C,D * /
C
ð
 

输入:

  COS(2  -  ASIN(3 * A / B)^ 2.5)+ COS(0.5 *(C *〜(D))+ 3.11 + B)
一个
B / *有一个值,B,C,D * /
C
ð
 

输入:

 的sqrt(2  - 罪(3 * A / B)^ 2.5)/(0.5 *(C *〜(D))+罪(3.11)+ LN(B))
 / *公式的最大lenght为250个字符* /
一个
B / *有一个值,B,C,D * /
C / *每个变量与设定浮动号码* /
ð
 

正如你可以看到在输入公式中缀取决于用户。 我的程序将采取一个公式和正元组值。 然后,它计算结果的a,b,c和d的每个值。 如果你想知道我的意思;程序的结果是图形。 / 有时候,我想我会接受输入并存储在字符串。 那么另外一个想法是产生我应该存储公式中的结构 但我不知道我怎么可以构建 的code结构的基础上。的/

说真的,我不知道怎样的方​​式来存储配方程序code,使 我可以做我的工作。 你能告诉我吗?

  / * A,B,C,D为信
 COS,罪恶,开方,LN是功能* /
 

解决方案

您需要写一个词法分析器来标记输入(它分解成它的组成部分 - 运营商,标点符号,标识符等)。不可避免地,你将最终获得令牌的一些序列。

在此之后,有许多方法来评价输入。一要做到这一点最简单的方法是使用调车场算法转换前pression为后缀 (一前后缀pression评价是很容易用大写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).

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

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