简单Java 程序问题请教

来源:百度知道 编辑:UC知道 时间:2024/05/13 00:47:55

import java.util.Stack;

public class Test {

public static void main(String[] args) {
System.out.println(new Translate().translate("-1+2- 3 + 4 * 5-6+8/2"));
System.out.println((-1+2-3+4*5-6+8/2));
}
}

class Translate{
Stack<String> numbers ;
Stack<String> ops ;
int idx = 0;
String content;
public Translate() {
idx = 0;
content = "";
numbers = new Stack<String>();
ops = new Stack<String>();
}
public String translate(String string) {
content = string.replaceAll("[\\s]+","");
idx = 0;
numbers.clear();
ops.clear();
return translate();
}
public String translate() {
String cur = null;

while(true) {
cur = getNext();
System.out.println(cur);
if(!isOp(cur)) {
numbers.push(cur);
} else {
if(ops.isEmpty()