C - 带操作数的算法 [英] C - algorithm with operands
本文介绍了C - 带操作数的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello,
I have an issue, i have a string, e.g: `"!!!T&(!F)"` and i want those operations to be executed and the output should be in a variable which i will show in console.
Any idea how could i do that??
T = true ( a constant defined by me )
F = false ( same as T )
R = result ( the variable where i want the output)
< br $> b $ b
我尝试过的事情:
好吧,我陷入困境这一点,我不知道我该如何接近它。我在等待建议,解决方法..
What I have tried:
Well, i'm stuck in this point, i do not know how i should approach it. I'm waiting for suggestions, ways of solving it..
推荐答案
你要做的是评估一个逻辑表达式。 为了做到这一点,你应该首先为你想要解析的表达式系列定义一个简单的语法(你的例子只是一个这样的表达式),然后从输入字符串构建一个解析树,或者至少转换为你在RPN中的中缀表达式(反向波兰表示法)。 一旦你完成了,你可以使用一个简单的堆栈机器来执行表达式的RPN形式。
如果这对你没有任何意义,你已经咬过的东西超过了你可以咀嚼的(现在),我建议阅读Dijkstra的 Shunting-yard算法 [ ^ ]。  ;一旦你了解它是如何工作的,就不那么难了。
/ ravi
What you are trying to do is evaluate a logical expression. In order to do this, you should first define a simple grammar for the family of expressions you want to be able to parse (your example is just one such expression), then build a parse tree from the input string, or at least convert the infix expression you have into RPN (Reverse Polish Notation). Once you've done that, you can use a simple stack machine to execute the RPN form of the expression.
If this doesn't make any sense to you, you've bitten off more than you can chew (for now) and I suggest reading up on Dijkstra's Shunting-yard algorithm[^]. It's not that hard once you understand how it works.
/ravi
这篇关于C - 带操作数的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文