A new format similar to postfix notation is to write +ab instead of usual a + b.
ID: 3563997 • Letter: A
Question
A new format similar to postfix notation is to write +ab instead of usual a + b.
Write a java program for converting a formula to the new format. For example, if the input is (a+b)*c, then the output should be *+abc. Your program should take a string with a formula as input. The operators are +,-,*, and /. Each operand is a single digit or a single letter (a...z or A..Z). The formula may have parenthesis.
Your java program should use 2 stacks and write your own stack class called charStack. Implement the stack using an array. Since both stacks store characters, you may use array char[] or String.
Explanation / Answer
import java.io.*;
import java.util.*;
import java.lang.*;
/*--------------------------- PUBLIC CLASS INFIXTOPREFIX --------------------------------------*/
/*-------------------------- INFIX TO PREFIX Exp<b></b>ressionS --------------------------------------*/
public class infixToPrefix {
private static LinkedStack operators = new LinkedStack();
private static LinkedStack operands = new LinkedStack();
// Class variable for keyboard input
private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
// Repeatedly reads in infix exp<b></b>ressions and evaluates them
public static void main(String[] args) throws IOException {
// variables
String exp<b></b>ression, response = "y";
// obtain input of infix exp<b></b>ression from user
while (response.charAt(0) == 'y') {
System.out.println("Enter a parenthesized infix exp<b></b>ression."); // prompt the user
System.out.println("Example: ( ( 13 + 2 ) * ( 10 + ( 8 / 3 ) ) )");
System.out.print("Or as: ((13+2)*(10+(8/3))): ");
exp<b></b>ression = stdin.readLine(); // read input from the user
// output prefix exp<b></b>ression and ask user if they would like to continue
System.out.println("The Prefix exp<b></b>ression is: " + prefix(exp<b></b>ression)); // output exp<b></b>ression
System.out.println("Evaluate another? y or n: "); // check with user for anymore exp<b></b>ressions
response = stdin.readLine(); // read input from user
if (response.charAt(0) == 'n') { // is user chooses n, output the statement
System.out.println("Thank you and have a great day!");
} // end if statement
} // end while statement
} // end method main
/*------------- CONVERSION OF AN INFIX Exp<b></b>ression TO A PREFIX Exp<b></b>ression ------------*/
/*--------------------------- USING A SWITCH STATEMENT ------------------------------*/
private static String prefix(String exp<b></b>ression) {
// variables
String symbol, operandA, operandB, operator, stringA, outcome;
// initialize tokenizer
StringTokenizer tokenizer = new StringTokenizer(exp<b></b>ression, " +-*/() ", true);
char e = exp<b></b>ression.charAt(0);
while (tokenizer.hasMoreTokens()) {
symbol = tokenizer.nextToken(); // initialize symbol
switch (e) {
case ' ':
break; // accounting for spaces
case '(':
break; // skipping the left parenthesis
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
operands.push(symbol); // push the string onto the stack of operands
break; // case numerics
case '+':
case '-':
case '*':
case '/':
operators.push(symbol); // push the operator onto the stack of operators
break; // case operators
case ')':
operandA = (String)operands.pop(); // pop off first operand
operandB = (String)operands.pop(); // pop off second operand
operator = (String)operators.pop(); // pop off operator
stringA = operator + " " + operandB + " " + operandA; // form the new string
operands.push(stringA);
break; // case ")" right parenthesis
default:
} // end switch statement
} // end while statement
outcome = (String)operands.pop(); // pop off the outcome
return outcome; // return outcome
} // end method prefix
} // end class infixToPrefix
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.