需要帮助来解决这个问题 [英] need help to solve this problem

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

问题描述

表达评估
考虑一个伪代码,该伪代码将以下表达式作为输入并产生给定的输出.

输入输出

[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.

推荐答案

好吧,跳过最外部的"[]",您将具有以下语法(如果我正确):

Well, skipping the most external ''[]'', you have the following grammar (if I''m right):

start: exp

exp: DIGIT | DIGIT '','' exp | ''['' exp '']''    

NUM: ''0'' | ''1'' | .. | ''9''



它看起来很简单,可以手动实现解析器.



It looks simple enough in order to implement manually the parser.


这篇关于需要帮助来解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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