赞
踩
Context 编写测试代码
expression
Expression interface
四则运算Expression implement
NumberExpression implement
ExpressionParser 核心实现类
package com.example.demo; import com.example.demo.expression.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExpressionParser { private String input; private int index; public ExpressionParser(String input) { this.input = input.replaceAll("\\s", ""); // 移除空格 this.index = 0; } public Expression parse() { return parseExpression(); } private Expression parseExpression() { Expression left = parseTerm(); while (index < input.length()) { char operator = input.charAt(index); if (operator == '+' || operator == '-') { index++; Expression right = parseTerm(); if (operator == '+') { left = new AddExpression(left, right); } else { left = new SubtractExpression(left, right); } } else { break; } } return left; } private Expression parseTerm() { Expression left = parseFactor(); while (index < input.length()) { char operator = input.charAt(index); if (operator == '*' || operator == '/') { index++; Expression right = parseFactor(); if (operator == '*') { left = new MultiplyExpression(left, right); } else { left = new DivideExpression(left, right); } } else { break; } } return left; } private Expression parseFactor() { if (index < input.length()) { char currentChar = input.charAt(index); if (Character.isDigit(currentChar)) { return parseNumber(); } else if (currentChar == '(') { index++; Expression expression = parseExpression(); if (index < input.length() && input.charAt(index) == ')') { index++; return expression; } else { throw new IllegalArgumentException("Mismatched parentheses"); } } } throw new IllegalArgumentException("Invalid expression"); } private Expression parseNumber() { Pattern numberPattern = Pattern.compile("\\d+"); Matcher matcher = numberPattern.matcher(input.substring(index)); if (matcher.find()) { String numberStr = matcher.group(); index += numberStr.length(); return new NumberExpression(Integer.parseInt(numberStr)); } else { throw new IllegalArgumentException("Invalid number"); } } }
package com.example.demo;
import com.example.demo.expression.Expression;
public class Context {
public static void main(String[] args) {
String inputExpression = "(3 + 55) * 2 - 4 / 2";
ExpressionParser parser = new ExpressionParser(inputExpression);
Expression expression = parser.parse();
int result = expression.interpret();
System.out.println("Result: " + result);
}
}
package com.example.demo.expression;
public interface Expression {
int interpret();
}
package com.example.demo; import com.example.demo.expression.Expression; public class NumberExpression implements Expression { private int number; public NumberExpression(int number) { this.number = number; } @Override public int interpret() { return number; } }
package com.example.demo.expression; public class AddExpression implements Expression { private Expression left; private Expression right; public AddExpression(Expression left, Expression right) { this.left = left; this.right = right; } @Override public int interpret() { return left.interpret() + right.interpret(); } }
package com.example.demo.expression; // 非终结符表达式:减法表达式 public class SubtractExpression implements Expression { private Expression left; private Expression right; public SubtractExpression(Expression left, Expression right) { this.left = left; this.right = right; } @Override public int interpret() { return left.interpret() - right.interpret(); } }
package com.example.demo.expression; // 非终结符表达式:乘法表达式 public class MultiplyExpression implements Expression { private Expression left; private Expression right; public MultiplyExpression(Expression left, Expression right) { this.left = left; this.right = right; } @Override public int interpret() { return left.interpret() * right.interpret(); } }
package com.example.demo.expression; // 非终结符表达式:除法表达式 public class DivideExpression implements Expression { private Expression left; private Expression right; public DivideExpression(Expression left, Expression right) { this.left = left; this.right = right; } @Override public int interpret() { int divisor = right.interpret(); if (divisor != 0) { return left.interpret() / divisor; } else { throw new ArithmeticException("Division by zero"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。