更新:修复了愚蠢的“.equals()”错误。但是,我的程序卡while
在infix2postfix()
. 为什么运算符堆栈没有清空?
原文:我目前正在尝试为后缀转换器制作中缀。但是,运算符不会附加到后缀字符串中。
我一直在看我的代码一段时间,我认为它可能只需要一双新的眼睛。谁能告诉我else if
, 对于运营商有什么问题?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class old{
public static void main(String[] args){
//File to scan
File file = new File("input.infix");
//Scanner for the file
Scanner scanf = null;
try {
scanf = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File Not Found: 'input.infix'");
}
//Convert each line of Infix to Postfix
while(scanf.hasNextLine()){
//Store Postfix and its Evaluation
String postfix;
int postEval;
//Convert Infix to Postfix
postfix = infix2postfix(scanf.nextLine());
//Evaluate Postfix
postEval = evalPostfix(postfix);
//Print the Postfix Equation and Evaluation
System.out.println(postfix + " = " + postEval);
}
}
//Takes Line of File (each Infix) as a String and Returns a postfix String
public static String infix2postfix(String infix){
//Postfix String to Return
String postfix = "";
//Store Equation in Stack
Stack<String> stack = new Stack<String>();
//Append a Right Parentheses to the Input Expression
infix = infix + " " + ")";
//Push a Left Parentheses on to the Stack
stack.push("(");
//Split the Infix String into String Tokens
String[] tokens = infix.split("\\s+");
//Starting at the first Token, for each token
for(int t = 0; t < tokens.length; t++){
//System.out.println(stack);
String token = tokens[t];
try{
Integer.parseInt(token);
postfix += token + " ";
}catch(NumberFormatException e){
//If "(" -- Push to stack
if(token.equals("(")){
stack.push(tokens[t]);
}//If ")" -- Pop Operators from stack and Append them to postfix
else if(token.equals(")")){
//Do this until a "(" is reached
while(!stack.peek().toString().equals("(") && !stack.isEmpty()){
postfix += " " + stack.pop();
}
stack.pop(); //Pop the "("
}//If Operator -- Determine Precedence
else if(token.equals("+") || token.equals("-") || token.equals("*") ||token.equals("/") || token.equals("%")){
String element = stack.peek().toString(); //Pass stack element as String
while(!stack.isEmpty()){
//INFINITE LOOP
boolean gte = opPrec(element, token); //Is element >= token
if(gte){
postfix += " " + stack.pop();
}
}
stack.push(token);
}
}
}
return postfix;
}
private static boolean opPrec(String element, String token) {
//Greater than or Equal to
boolean gte = false;
//Precedence Values
int sVal = 0;
int tVal = 0;
//Stack Value
if(element.equals("+") || element.equals("-")){
sVal = 1;
}
else if(element.equals("*") || element.equals("/") || element.equals("%")){
sVal = 2;
}
//Token Value
if(token.equals("+") || token.equals("-")){
tVal = 1;
}
else if(token.equals("*") || token.equals("/") || token.equals("%")){
tVal = 2;
}
//Evaluate Precedence
if(sVal >= tVal){
gte = true;
}
//System.out.println(gte);
return gte;
}
//Takes Postfix String, Parses it, Evaluates it, Returns postEval
public static int evalPostfix(String postfix){
//Evaluation of Equation to Return
int eval = 0;
//NOT YET CONSTRUCTED
return eval;
}
}