In: Computer Science
Java. Complete the methods for infixToPostfix, postfixToInfix, solvePostfix.
import java.util.Stack;
public class B1Work {
/*
* Grading:
* Correctly converts infix to postfix - 3pts
*/
public static String infixToPostfix(String infix)
{
/*
* Convert an infix math formula to
postfix format
* Infix format will always include
parens around any two values and a symbol
* You will not need to imply any
PEMDAS rules besides parens
* EXAMPLES:
* infix: (1+2) postfix: 12+
* infix: (1+(2-3)) postfix:
123-+
* infix: ((1+2)-3) postfix:
12+3-
* infix: ((1+2)-(3*4)) postfix:
12+34*-
*/
return null;
}
/*
* Grading:
* Correctly converts postfix to infix - 2pts
*/
public static String postfixToInfix(String postfix)
{
/*
* Convert a postfix math formula to
an infix format
* See above for conversion
examples
* Make sure to include parens in
the infix format
*/
return null;
}
/*
* Grading:
* Correctly solves postfix formulas with +-/* -
1pt
*/
public static double solvePostfix(String postfix)
{
Stack<Integer> stack = new
Stack<>();
/*
* Use a Stack to help solve a
postfix format formula for the numeric answer
* Order of operations is implied by
where symbols/numbers exist
* EXAMPLES
* postfix: 12+ = 3
* postfix: 123-+ = 0 :: 2-3 = -1 ::
1 + (-1) = 0
*/
return 0;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class B1Driver {
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
String format = "";
do {
System.out.print("What format will your formula be in
(infix/postfix):");
format =
s.nextLine().toLowerCase();
}while(!(format.equals("infix") ||
format.equals("postfix")));
System.out.print("Enter your
formula:");
String formula =
s.nextLine().replaceAll(" ", "");//removes whitespace
String infix, postfix;
switch(format) {
case "infix":
infix =
formula;
postfix =
B1Work.infixToPostfix(infix);
System.out.println("Infix:"+infix);
System.out.println("Postfix:"+postfix);
System.out.println("Answer:"+B1Work.solvePostfix(postfix));
break;
case "postfix":
postfix =
formula;
infix =
B1Work.postfixToInfix(postfix);
System.out.println("Infix:"+infix);
System.out.println("Postfix:"+postfix);
System.out.println("Answer:"+B1Work.solvePostfix(postfix));
break;
}
}
}
import java.util.Stack;
public class B1Work {
/*
* Grading:
* Correctly converts infix to postfix - 3pts
*/
public static boolean precedence(char first, char
second) {
if (second == '(' || second ==
')')
return
false;
if ((first == '*' || first == '/')
&& (second == '+' || second == '-'))
return
false;
else
return true;
}
public static String infixToPostfix(String infix) {
/*
* Convert an infix math formula to postfix format
* Infix format will always include parens around any two values and
a symbol
* You will not need to imply any PEMDAS rules besides parens
* EXAMPLES:
* infix: (1+2) postfix: 12+
* infix: (1+(2-3)) postfix: 123-+
* infix: ((1+2)-3) postfix: 12+3-
* infix: ((1+2)-(3*4)) postfix: 12+34*-
*/
char character[] = infix.toCharArray();
Stack<Character> st = new
Stack<Character>();
for (int i = 0; i <
character.length; i++) {
if (character[i]
== '(')
st.push(character[i]);
if (character[i]
== ')') {
if (st.isEmpty()) {
return "No matching open
parenthesis error";
}
st.pop();
}
}
if (!st.isEmpty())
return "No
matching close parenthesis error";
// initialize the resulting
postfix string
String postfixString = "";
// initialize the stack
Stack<Character> stack1 = new
Stack<Character>();
// Obtain the character at index
i in the string
for (int i = 0; i <
infix.length(); i++) {
char ch =
infix.charAt(i);
if (Character.isLetter(ch) || Character.isDigit(ch))
postfixString = postfixString + ch;
else if (ch == '(') {
stack1.push(ch);
} else if (ch == ')') {
while (stack1.peek() != '(') {
postfixString = postfixString + stack1.pop();
}
stack1.pop();
} else {
while (!stack1.isEmpty() && !(stack1.peek() == '(') && precedence(ch, stack1.peek()))
postfixString = postfixString + stack1.pop();
stack1.push(ch);
}
}
while (!stack1.isEmpty())
postfixString = postfixString + stack1.pop();
return postfixString;
}
/*
* Grading:
* Correctly converts postfix to infix - 2pts
*/
public static boolean isCharacter(char ch) {
return (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
Character.isDigit(ch);
}
public static String postfixToInfix(String postfix) {
/*
* Convert a postfix math formula to an infix format
* See above for conversion examples
* Make sure to include parens in the infix format
*
*
*/
Stack<String> stack = new
Stack<String>();
for (int i = 0; i < postfix.length(); i++)
{
if (isCharacter(postfix.charAt(i)))
stack.push(postfix.charAt(i) +
"");
else
{
String operator1 = stack.peek();
stack.pop();
String operator2 = stack.peek();
stack.pop();
stack.push("(" + operator2 + postfix.charAt(i) +
operator1 +
")");
}
}
return stack.peek();
}
/*
* Grading:
* Correctly solves postfix formulas with +-/* - 1pt
*/
public static double solvePostfix(String s) {
// Stack<Integer> stack = new Stack<>();
/*
* Use a Stack to help solve a postfix format formula for the
numeric answer
* Order of operations is implied by where symbols/numbers
exist
* EXAMPLES
* postfix: 12+ = 3
* postfix: 123-+ = 0 :: 2-3 = -1 :: 1 + (-1) = 0
*/
Stack<Double> postfix = new Stack<Double>();
for (int i = 0; i < s.length();
i++) {
char c =
s.charAt(i);
if
(Character.isDigit(c))
postfix.push((double) (c - '0'));
else {
double val1 = postfix.peek();
postfix.pop();
double val2 = postfix.peek();
postfix.pop();
switch (c) {
case '+':
postfix.push(val2 +
val1);
break;
case '-':
postfix.push(val2 -
val1);
break;
case '/':
postfix.push(val2 /
val1);
break;
case '*':
postfix.push(val2 *
val1);
break;
}
}
}
return postfix.peek();
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class B1Driver {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String format = "";
do {
System.out.print("What format will your formula be in (infix/postfix):");
format = s.nextLine().toLowerCase();
}while(!(format.equals("infix") || format.equals("postfix")));
System.out.print("Enter your formula:");
String formula = s.nextLine().replaceAll(" ", "");//removes whitespace
String infix, postfix;
switch(format) {
case "infix":
infix = formula;
postfix = B1Work.infixToPostfix(infix);
System.out.println("Infix:"+infix);
System.out.println("Postfix:"+postfix);
System.out.println("Answer:"+B1Work.solvePostfix(postfix));
break;
case "postfix":
postfix = formula;
infix = B1Work.postfixToInfix(postfix);
System.out.println("Infix:"+infix);
System.out.println("Postfix:"+postfix);
System.out.println("Answer:"+B1Work.solvePostfix(postfix));
break;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
Stack<String> stack = new Stack<String>(); for (int i = 0; i < postfix.length(); i++) 116 117 118 119 120 121 122 123 124 if (isCharacter postfix.charAt(i))) stack.push(postfix.charAt(i) + ""); else 125 126 127 128 129 130 String operator1 = stack.peek(); stack.pop(); String operator2 = stack.peek(); stack.pop(); stack.push("(" + operator2 + postfix.charAt(i) + operator1 + ")"); 131 132 133 return stack.peek(); 134 1352 /* 136 * Grading: 137 * Correctly solves postfix formulas with +-/* - 1pt e Console X X X Ex : 9 <terminated> B1 Driver [Java Application] /Library/Java/JavaVirtual Machines/jdk1.8.0_202.jdk/Contents/Home/bin/ja What format will your formula be in (infix/postfix): infix Enter your formula:(1+(2-3)) Infix:(1+(2-3)) Postfix: 123-+ Answer:0.0