java method for parsing nested expressions

后端 未结 5 1201
Happy的楠姐
Happy的楠姐 2020-12-16 04:37

lets say I\'ve written a function to evaluate a simple mathematical operation, and I have some user input in a string such as: \"1 + [2 + [3+4]]\" How can I parse these squa

5条回答
  •  旧巷少年郎
    2020-12-16 05:06

    Use a stack. When you encounter an open bracket, push whatever you're working on onto the stack and start the new expression. When you hit a closing bracket, pop the stack and use the expression you just calculated as the next item. Or, as previous posters have said, use recursion or a tree.

提交回复
热议问题