解析数学表达式

编程入门 行业动态 更新时间:2024-10-19 04:23:51
本文介绍了解析数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

(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).

更多推荐

解析数学表达式

本文发布于:2023-11-10 16:12:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   数学

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!