怎样用JAVA写出逆波兰表达式求值部分的源代码(提供代码框架)
来源:百度知道 编辑:UC知道 时间:2024/05/31 06:28:25
逆波兰表达式 6 2 5+ * 2 8 7+ * /其中只用到(+ - * /这4中运算符号)
DoubleStack API:
================
Constructor:
------------
DoubleStack (int maxLength)
Mutators:
---------
void push(double elt) - pushes elt onto the top of the stack
double pop() - removes and returns the topmost element of the stack
Instance Methods:
------------------
String toString() - returns a String that formats the contents of the stack */
public class DoubleStack {
// you will need to add instance variables
public DoubleStack (int maxLength) {
// you need to fill in this constructor
}
public void push(double elt) {
// you need to fill this in /* }
public double pop() {
// you need to fill this in
}
public String toString() {
// you need to fill this
DoubleStack API:
================
Constructor:
------------
DoubleStack (int maxLength)
Mutators:
---------
void push(double elt) - pushes elt onto the top of the stack
double pop() - removes and returns the topmost element of the stack
Instance Methods:
------------------
String toString() - returns a String that formats the contents of the stack */
public class DoubleStack {
// you will need to add instance variables
public DoubleStack (int maxLength) {
// you need to fill in this constructor
}
public void push(double elt) {
// you need to fill this in /* }
public double pop() {
// you need to fill this in
}
public String toString() {
// you need to fill this
下面的代码是用来计算表达式的,看看是不是你要的
public class OPNode {
char op;// 运算符号
int level;// 优先级
//设置优先级
public OPNode(String op) {
this.op = op.charAt(0);
if (op.equals("+") || op.equals("-")) {
this.level = 1;
} else if (op.equals("*") || op.equals("/")) {
this.level = 2;
} else if (op.equals("(")) {
this.level = -3;
} else {
this.level = -1;
}
}
}
//主类
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OPText {
public static void main(String[] args) {
String expression = "2+2+(8-2)/3";// 要计算的表达式
List list = new LinkedList();
//正则式
Pattern entryOfExpression = Pattern
.compile("[0-9]+(\\.[0-9]+)?|\\(|\\)|\