I have been working on this program for roughly a week, for computer science. I
ID: 3631107 • Letter: I
Question
I have been working on this program for roughly a week, for computer science. I have been getting different errors. and I thought I finally finished it, however this new error popped up and I cant seem to figure out what exactly to do, I know I am very close to completing this. Please HELP! Thanks!This is written in Java. Also I have commented my classes, and also commented where my error lies.
import java.util.*;
public class PostFix //Start - Postfix convert into Infix
{
private Stack<String> stack;
private String infix, postfix = "";
public PostFix(String input)
{
String str="";
infix=input;
stack=new Stack<String>();
for (int i=0;i<infix.length();i++)
{
str = infix.substring(i,i+1);
if(str.matches("[a-zA-Z]|\d"))
postfix += str + " ";
else if (Operation(str))
{
if (stack.isEmpty())
{
stack.push(str);
}
else
{
String Top = stack.peek();
while (getLineUp(Top,str).equals(Top)&& !(stack.isEmpty()))
{
postfix += stack.pop() + " ";
if (!(stack.isEmpty()))
Top = stack.peek();
}
stack.push(str);
}
}
}while(!(stack.isEmpty())) //End - Postfix into Infix
postfix += stack.pop() + " ";
System.out.println("The postfix that was entered transforms into this: " + postfix);
}
private boolean Operation(String ch) //Start - Operation Index
{
String operators ="*/%+-";
if (operators.indexOf(ch) != -1)
return true;
else
return false; // End - Operation Index
}
private String getLineUp(String op1, String op2) // Start - Order of Operation
{
String multiply = "*/%";
String add = "+-";
if ((multiply.indexOf(op1) != -1) && (add.indexOf(op2) != -1))
return op1;
else if ((multiply.indexOf(op2) != -1) && (add.indexOf(op1) != -1))
return op2;
else if((multiply.indexOf(op1) != -1) && (multiply.indexOf(op2) != -1))
return op1;
else
return op1; // End - Order of Operation
}
private static double evaluateExpression(double topStack,String operator,double temp) // Start - Evaluation Expression
{
if(operator.equals("+"))
return topStack+temp;
else if(operator.equals("-"))
return topStack-temp;
else if(operator.equals("/"))
return topStack/temp;
else if(operator.equals("*"))
return topStack*temp;
else return 0; // End - Evaluation Expression
}
public static double Evaluate(String postfixString) // Start - Evaluating
{
Stack<Double> myStack = new Stack();
StringTokenizer tok = new StringTokenizer(postfixString);
while(tok.hasMoreTokens())
{
String tempString = tok.nextToken();
boolean Number = true;
try
{
double temp = Integer.parseInt(tempString);
myStack.push(temp);
Number = true;
}
catch(NumberFormatException e){Number = false;}
if(Number==false)
{
double tempTopOfStack = myStack.pop(); // Here is my error between here
double retVal = evaluateExpression(myStack.pop(),tempString,tempTopOfStack);
myStack.push(retVal); // and here
}
}
return myStack.pop();
}
public static void main(String[] args) // Start - Main Method
{
System.out.println("Enter an expression in the form of an Infix: ");
Scanner scan = new Scanner(System.in);
String expression = scan.nextLine();
new PostFix(expression);
System.out.print("The answer is " + PostFix.Evaluate(expression));
} // End - Main Method
}
Explanation / Answer
import java.util.*; public class PostFix //Start - Postfix convert into Infix { private Stack stack; private String infix, postfix = ""; public PostFix(String input) { String str=""; infix=input; stack=new Stack(); for (int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.