问题描述
表达评估
考虑一个伪代码,该伪代码将以下表达式作为输入并产生给定的输出.
输入输出
[0] 0
[1] 1
[0,1] 1
[1,1] 2
[1,[1,2]] -2
[1,2,[8]] -5
[1,[2],[3]] -4
[9,[3,4,[[2]]] 0
通过将原型作为
,设计一个函数/方法来实现上述伪代码
对于CPP:int ExpressEval(char * expression);
对于C Sharp:public int ExpressEval(string expression);
输入:
接受要作为字符串求值的表达式.
输出:
以整数形式返回评估结果.
对于例如
认为输入字符串为"[1、2,[5、3]]"
那么您的函数必须返回-5.
Expression Evaluation
Consider a pseudo code which takes following expression as input and produces given outputs.
Input Output
[ 0 ] 0
[ 1 ] 1
[ 0, 1 ] 1
[ 1, 1 ] 2
[ 1, [ 1, 2 ] ] -2
[ 1, 2, [ 8 ] ] -5
[ 1, [ 2 ], [ 3 ] ] -4
[ 9, [ 3, 4, [ [ 2 ] ] ] ] 0
Design a function/methos to implement above pseudo code by following prototype as
For CPP :int ExpressEval(char * expression);
For C Sharp :public int ExpressEval(string expression);
Input:
Accept expression to be evaluated as a string.
Output :
Return result of evaluation as an integer.
For e.g
consider input string to be "[1, 2 , [5 , 3 ] ]"
then your function must return -5.
推荐答案
start: exp
exp: DIGIT | DIGIT '','' exp | ''['' exp '']''
NUM: ''0'' | ''1'' | .. | ''9''
它看起来很简单,可以手动实现解析器.
It looks simple enough in order to implement manually the parser.
这篇关于需要帮助来解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!