中缀,后缀和前缀 [英] infix , postfix and prefix

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

问题描述

你好,我要完成从c转换为infix方程到后缀和前缀的任务,我们的老师给了我们算法,但是已经一个月了,没人能做,所以请给我看看代码其中,我在网上寻找代码,但是所有代码都在使用库的地方,而且我不想使用它们,所以请我需要您的帮助...
关于..
ahmed yousify ...:)

hello , i have this assignment of converting from infix equation to a postfix and prefix in c++ , our teacher gave us the algorithm , but it has been a month and no one could make one , so could you please show me the code of it , i looked for codes in the net , but all of them where using libraries , and i dont want to use them , so please , i need your help ...
with regards ..
ahmed yousify ... :)

推荐答案

在提出问题之前,请先阅读 ^ ]
Before asking question please read this[^]


我在搜索后发现了几个程序.
检查他们,看看它们是否如您所愿地工作.

http://ds4beginners.wordpress.com/2006/10/18/infix-to -postfix-conversion/ [ ^ ]
http://www.cfanatic.com/topic2385/ [ http://www.physicsforums.com/showthread.php?t=159713 [ ^ ]
I found several programs after a search.
Check them to see if they work as you want it to.

http://ds4beginners.wordpress.com/2006/10/18/infix-to-postfix-conversion/[^]
http://www.cfanatic.com/topic2385/[^]
http://www.physicsforums.com/showthread.php?t=159713[^]


首先,定义问题,以指定所有未清项目,例如您可能会承担一些约束
-让我们定义+和-作为运算符
-让我们将double定义为操作数类型
-假设运算符和操作数之间用空格隔开
现在,确定您需要的对象
-堆栈< double>用于推/弹出操作
-存储初始表达式的字符串
-由表达式字符串初始化并由>>读取(扫描)的字符串流.运算符转换成单独的字符串符号
-一个while循环,只要有可用符号就读取字符串流
-while循环主体中的一个开关,该开关决定-执行-,在+上执行+,以及将其转换为两次压入堆栈的操作
-在while块之后,将栈顶写入输出
-"do-"和"do +"是a = stack的两倍.流行音乐();双b = stack.pop(); stack.push(a-b); (分别是a + b)
后缀就是这样.例如. "2 3 + 4-"的结果为"1".

对于前缀:
-带有前缀表达式的字符串
-包含后缀表达式的目标
-使用前缀表达式string
初始化的字符串流 -包含最后一个运算符的字符串(最初为空)
-在>>
中读取每个符号的while循环 -切换为-和+:将空格和最后一个运算符添加到目标字符串,并将新运算符存储为最后一个运算符.其他值将直接附加到目标字符串(值前有一个空格).
-在while块之后,将最后一个运算符附加到目标字符串
-最后,从上方对目标字符串调用Postfix函数
就是这样,伙计们!

玩得开心!
干杯
安迪
First, define the problem such that all open items are specified, e.g. you might assume some constraints
- let''s define + and - as operator
- let''s define double as operand type
- let''s assume that the operators and operands are separated by spaces
Now, identify the objects you need
- stack<double> for push/ pop operations
- string to store the initial expression
- string stream that gets initialized by the expression string and that is read (scanned) by the >> operator into individual string symbols
- a while loop that reads the string stream as long as there are symbols available
- a switch in the while loop body that decides on - to do -, on + to do +, and else to convert to double an push to the stack
- after the while block, write the top of the stack to the output
- the "do -" and the "do +" is double a = stack. pop(); double b = stack.pop(); stack.push(a - b); (respectively a+b)
That''s it for postfix. E.g. "2 3 + 4 -" results in "1".

For Prefix:
- string with prefix expression
- target that will hold postfix expression
- string stream that gets initialized with prefix expression string
- string that holds the last operator (initially empty)
- while loop that reads in each symbol by >>
- switch for - and +: append space plus the last operator to the target string and store the new operator as last operator. The other values are appende directly to the target string (with a space before the value).
- after the while block, append the last operator to the target string
- finally, call the Postfix function from above on the target string
That''s all, folks!

Have fun!
Cheers
Andi


这篇关于中缀,后缀和前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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