问题描述
我正在尝试使用递归方法从中缀字符串中创建表达式树,但是当解析器抛出空指针异常但不知道为什么,所以如果有人能帮助我,我会感谢你。
每个带有操作数的运算符都用括号括起来,每个数字都用括号括起来
Algorithm
Input: expression string
output: root of expression subtree for string
1. node = new treeNode
2. if string is a number
3. node.value = number extracted
4. return node
5. endif
6. node.op = extract operator
7. node.left = parse (substring of string to the left of op)
7. node.right = parse (substring of string to the right of op)
/**
* ExpressionTree class
*/
public class ExpressionTree {
private static class Node {
int value;
int op;
Node left,right;
boolean isNumber;
}
Node root;
/**
* Start parsing the equation
* @param str equation
*/
public void parsing(String str){
root = parse(str);
}
/**
* Empty constructor
*/
ExpressionTree(){
}
Node parse(String str){
Node node = new Node();
try{
int n = findRight(str,1);
String left = str.substring(1,n+1);
if (n == str.length()-1){
String op = str.substring(1,str.length()-1);
node.isNumber = true;
node.value = getValue(op);
System.out.println(" Returned Node" + " value " + node.value);
return node;
}
int n2 = findLeft(str,str.length()-2);
String right = str.substring(n2,str.length()-1);
node.left = parse(left);
node.right = parse(right);
node.op = str.charat(n+1);
return node;
}catch (indexoutofboundsexception | NullPointerException exception){
System.out.println("Error parse : " + exception.getCause().toString());
return null;
}
}
/**
* Find the matching left parent
* @param str equation
* @param pos index to start searching
* @return position of left parent
*/
public int findLeft(String str,int pos) {
Stack<Character> stack = new Stack<>();
try {
stack.push(str.charat(pos));
for (int i = pos + 1; i < str.length(); i++) {
char c = str.charat(i);
if (c == '(')
stack.push(c);
else if (c == ')') {
stack.pop();
if (stack.isEmpty()) {
System.out.println("Left i " + i);
return i;
}
}
}
}catch (indexoutofboundsexception | NullPointerException exception) {
System.out.println("ERROR findLeft: str " + str + " right = " + pos);
System.out.println("Error :" + exception.getCause().toString());
}
return -1;
}
/**
* Converts String operand to his value
* @param op string operand
* @return value of operand
*/
public int getValue(String op) {
try{
return Integer.parseInt(op);
}catch(NumberFormatException e){
return -1;
}
}
/**
* Find the matching right parent
* @param str equation
* @param pos index to start searching
* @return position of right parent
*/
public int findRight(String str,int pos) {
Stack<Character> stack = new Stack<>();
stack.push(str.charat(pos));
//System.out.println("findRight: str "+ str + " left = "+ pos);
try {
for (int i = pos+1; i < str.length(); i++){
char c = str.charat(i);
if(c == '(')
stack.push(c);
else if (c == ')'){
stack.pop();
if (stack.isEmpty()){
System.out.println("Right i " + i);
return i;
}
}
}
}catch (indexoutofboundsexception | NullPointerException exception) {
System.out.println("ERROR findRight: str "+ str + " left = "+ pos);
System.out.println("Error findRight : " + exception.getCause().toString());
}
return -1;
}
/**
* Start solving
* @return solved expression tree
*/
public int computeValue(){
return compute(root);
}
/**
* Solve the expression tree recursively
* @param node node
* @return value of expression tree
*/
public int compute(@org.jetbrains.annotations.NotNull Node node){
if (node.isNumber){
return node.value;
}
int left = compute(node.left);
int right = compute(node.right);
if (node.op == '+'){
return left + right;
}else if (node.op == '-'){
return left - right;
}else if (node.op == '*'){
return left * right;
}else if (node.op == '^'){
return (int)Math.pow(left,right);//Temporal cast
}else{
return left / right;
}
}
}
主类
public class App
{
public static void main( String[] args )
{
String str;
str = "((((35)-((3)*((3)+(2))))/4))";
ExpressionTree expressionTree = new ExpressionTree();
expressionTree.parsing(str);
int v = expressionTree.computeValue();
System.out.println(str + " = " + v);
}
}
当我跑步时,我得到:
返回节点值 35 和 NullPointerException
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)