我应该在以下代码中实现3个函数。
Functions:
1. evaluate arithmetic expression in postfix
2. convert arithmetic expression from infix to postfix
3. convert arithmetic expression from postfix to infix
我根本不知道我可以从哪里开始以及功能需要如何查看/弥补。如果你能帮我解决这3个功能中的一个,我可能会自己做另外两个。
以下是代码:
import java.util.Stack;
public class Postfix {
public static int evalPostfix(String postfix) {
// TODO
}
public static String infixToPostfix(String infix) {
// TODO
}
public static String postfixToInfix(String postfix) {
// TODO
}
public static void main(String[] args) {
String infix1 = "(3-(7*2))";
String postfix1 = "372*-";
int eval1 = -11;
String infix2 = "((7+1)*((3-6)*(5-2)))";
String postfix2 = "71+36-52-**";
int eval2 = -72;
System.out.println(" postfix1: " + postfix1);
int n = evalPostfix(postfix1);
System.out.println("evalPostfix(postfix1): " + n);
if (n == eval1) {
System.out.println(" Right!");
} else {
System.out.println(" Wrong!");
}
System.out.println();
System.out.println(" infix1: " + infix1);
String s = infixToPostfix(infix1);
System.out.println("infixToPostfix(infix1): " + s);
if (s.equals(postfix1)) {
System.out.println(" Right!");
} else {
System.out.println(" Wrong!");
}
System.out.println();
System.out.println(" postfix1: " + postfix1);
s = postfixToInfix(postfix1);
System.out.println("postfixToInfix(postfix1): " + s);
if (s.equals(infix1)) {
System.out.println(" Right!");
} else {
System.out.println(" Wrong!");
}
System.out.println();
System.out.println(" postfix2: " + postfix2);
n = evalPostfix(postfix2);
System.out.println("evalPostfix(postfix2): " + n);
if (n == eval2) {
System.out.println(" Right!");
} else {
System.out.println(" Wrong!");
}
System.out.println();
System.out.println(" infix2: " + infix2);
s = infixToPostfix(infix2);
System.out.println("infixToPostfix(infix2): " + s);
if (s.equals(postfix2)) {
System.out.println(" Right!");
} else {
System.out.println(" Wrong!");
}
System.out.println();
System.out.println(" postfix2: " + postfix2);
s = postfixToInfix(postfix2);
System.out.println("postfixToInfix(postfix2): " + s);
if (s.equals(infix2)) {
System.out.println(" Right!");
} else {
System.out.println(" Wrong!");
}
System.out.println();
}
}
答案 0 :(得分:0)
以下是一些不完整的伪代码可以帮助您:
function int evalPostfix(string postfix)
repeat
// Fetch the next item.
item <- get next item from postfix
// Process the current item.
if (item is operand)
push item onto stack
else if (item is operator)
pop required number of operands from the stack
push operator(operands) onto stack
else
throw error "Unrecognised item: " + item + " found."
end if
until (no more items in postfix)
return pop from stack
end function
您的代码需要处理的每个运算符都需要单独的运算符函数。
答案 1 :(得分:0)
这是一款经典的Stack&#39;数据结构问题。
可能你已经尝试过Stacks(你有一个堆栈导入。),你也可以用正则表达式或二叉树等解决它,
如果你只是想要一个想法来解决这个问题,希望这可能有所帮助:
使用堆栈进行后缀转换:
算法:
- 从左到右扫描输入中缀表达式。
- 如果是操作数,请将其添加到临时字符串中。
- 否则,如果运算符的优先顺序大于堆栈中的运算符或堆栈为空,则将其推送到堆栈。
- 如果是&#39;(&#39;,请将其添加到堆栈。
- 如果是&#39;),则弹出并将项目添加到输出字符串,直到&#39;(&#39;来了。
- 继续做2-5 util字符串有更多字符。
醇>
static Stack<String> stack;
private static String doInfixToPostfix(String exp) {
stack = new Stack<String>();
String output = "";
for(int i=0;i<exp.length();i++){
if(exp.charAt(i) == '('){
stack.push("(");
}
else if(exp.charAt(i) == ')'){
while(stack.size()>0 && stack.peek() != "("){
output+= stack.pop();
}
if(stack.size()>0 && stack.peek() != "(")
return "INVALID";
else
stack.pop();
}
else if(isOperand("" + exp.charAt(i))){
//Its a Number
//It could be replaced to get more than one digits.....
output+= exp.charAt(i);;
}
else{
//Its a operator
while(stack.size()>0 && (priority("" + exp.charAt(i)) < priority(stack.peek()))){
output += stack.pop();
}
stack.push("" + exp.charAt(i));
}
}
while(stack.size()>0){
output+=stack.pop();
}
return output;
}
private static int priority(String value) {
if(value == "+" || value == "-") return 1;
else if(value == "*" || value == "/") return 2;
else if(value == "^") return 3;
return 0;
}
//This could be modified to accept more than one digits...
private static boolean isOperand(String value) {
try{
Integer.parseInt(value);
}
catch(NumberFormatException e){return false;}
return true;
}
而且,比较那些非常长的代码块的输出,你可以简单地使用assertEquals()测试用例。