Write a Java program that will implement a stack object to convert from either infix notation to postfix notation or postfix notation to infix notation. The program will also implement a link list data structure to track all the conversions done. The Program should have a menu like the following as its output:
"Please select what type of conversion you would like to do:
Screenshot
Program
MyLinkedlist.java
//Create a class node
class MyNode{
String exp1,exp2;
char op;
MyNode next;
public MyNode(String exp1,String exp2) {
this.exp1=exp1;
this.exp2=exp2;
op='=';
next=null;
}
}
//Create orderedr list class
public class MyLinkedList {
//Instance variable
MyNode head;
//Constructor
public MyLinkedList() { // constructors always have
the same name as the class
head=null;
}
//Insert an element last into list
public void insertItem(String exp1,String exp2)
{
MyNode newNode=new
MyNode(exp1,exp2);
if(head==null) {
head=newNode;
}
else {
MyNode
temp=head;
while (temp.next
!= null) {
temp = temp.next;
}
// Insert the new_node at last
node
if(temp.next==null) {
temp.next =
newNode;
}
}
}
@Override
public String toString() {
String tempStr = "";
MyNode temp=head;
while(temp.next!=null) {
tempStr = tempStr+temp.exp1+" "+temp.op+"
"+temp.exp2+"\n";
temp = temp.next;
}
tempStr = tempStr+temp.exp1+" "+temp.op+" "+temp.exp2;
return tempStr;
}
}
Convertor.java
import java.util.Scanner;
import java.util.Stack;
/**
* Program to convert inf to post and post to inf
* Get user has choice for convertion type
* Prompt for expression
* store each conversion in linkedlist
* Display all evaluated expressions
* @author deept
*
*/
public class Convertor {
//Scanner object for read
public static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
//Call menu
int op=menu();
//Create a linkeslist object
MyLinkedList list=new
MyLinkedList();
//Loop until exit
while(op!=4) {
if(op==1)
{
System.out.println("\nEnter an infix expression:
");
String inf=sc.nextLine();
String postfix=infixToPostfix(inf);
list.insertItem(inf,postfix);
System.out.println(inf+" = "+postfix);
}
else if(op==2)
{
System.out.println("\nEnter an postfix
expression: ");
String postfix=sc.nextLine();
String inf=postfixToInfix(postfix);
list.insertItem(postfix,inf);
System.out.println(postfix+" = "+inf);
}
else {
System.out.println("\nExpressions
evaluated:-\n"+list);
}
System.out.println();
op=menu();
}
System.out.println("\nGoodbye!!!");
}
/*
* Method to get user options
* Display options
* Return option
*/
public static int menu() {
System.out.println("Please select
what type of conversion you would like to do:\n"+
"1. Infix to postfix\n2. Postfix to infix\n3. Print
Equations\n"+
"4. Exit");
int opt=sc.nextInt();
//Error check
while(opt<1 || opt>4) {
System.out.println("\nERRROR!!!Option should be 1-4.Please
Re-enter....\n");
System.out.println("Please select what type of conversion you would
like to do:"+
"1. Infix to postfix\n2. Postfix to infix\n3. Print
Equations\n"+
"4. Exit");
opt=sc.nextInt();
}
sc.nextLine();
return opt;
}
/*
*Method to get precedence of operator here
* multiplication division has higher precedence
than
* Addition and subtraction
*/
public static int precedenceFinder(char op) {
if(op=='+' || op=='-') {
return 1;
}
else if(op=='*' || op=='/') {
return 2;
}
else {
return -1;
}
}
/*
* Method to convert infix to postfix
* Take an infix expression as input
* Convert into postfix and return
*/
public static String infixToPostfix(String infix)
{
String postfix ="";
//Stack for temporary save
Stack<Character> stack = new Stack<>();
//Loop through characters of infix expression
for
(int i = 0; i<infix.length(); ++i)
{
char c = infix.charAt(i);
//Is it digit add into postfix
if (Character.isLetterOrDigit(c)) {
postfix+=c;
}
//Check for opening brace
else if (c == '(') {
stack.push(c);
}
//Check for closing brace
else if (c == ')')
{
//The take elements from stack to add into
postfix
while (!stack.isEmpty() && stack.peek() != '(') {
postfix+=stack.pop();
}
//If nothing inside stack means invalid expression
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid
Expression";
else
stack.pop();
}
//If operator get then find out precedence
else
{
while (!stack.isEmpty() && precedenceFinder(c) <=
precedenceFinder(stack.peek())){
if(stack.peek() == '(') {
return "Invalid Expression";
}
postfix += stack.pop();
}
stack.push(c);
}
}
//Pop until
end of stack
while
(!stack.isEmpty()){
if(stack.peek() == '(') {
return "Invalid Expression";
}
postfix += stack.pop();
}
return postfix;
}
/**
* Method to generate infix from given postfix
expression
* Loop through each character to seperate operator and
operand
* Use stack for temporary storage and evaluate infix
and return
* @param exp
* @return
*/
public static String postfixToInfix(String post)
{
//Temporary storage
Stack<String>
stack = new Stack<String>();
//Loop until end of th
estring
for (int i = 0; i <
post.length(); i++)
{
//Check fo roperand
if (Character.isLetterOrDigit(post.charAt(i))) {
stack.push(post.charAt(i) + "");
}
//Check for operator
else
{
String op1 = stack.peek();
stack.pop();
String op2 = stack.peek();
stack.pop();
stack.push("(" + op2 + post.charAt(i) + op1 + ")");
}
}
//last element from
stack
return
stack.peek();
}
}
--------------------------------------------------------------
Output
Please select what type of conversion you would like to
do:
1. Infix to postfix
2. Postfix to infix
3. Print Equations
4. Exit
1
Enter an infix expression:
2+5
2+5 = 25+
Please select what type of conversion you would like to
do:
1. Infix to postfix
2. Postfix to infix
3. Print Equations
4. Exit
2
Enter an postfix expression:
25+
25+ = (2+5)
Please select what type of conversion you would like to
do:
1. Infix to postfix
2. Postfix to infix
3. Print Equations
4. Exit
3
Expressions evaluated:-
2+5 = 25+
25+ = (2+5)
Please select what type of conversion you would like to
do:
1. Infix to postfix
2. Postfix to infix
3. Print Equations
4. Exit
4
Goodbye!!!
Write a Java program that will implement a stack object to convert from either infix notation...
write a java program that takes infix notation equations and changes it to postfix notation. you will have to solve the postfix equation after. (make sure you are making your own stack class)
Write a program in c++ to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b....
Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...
Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...
Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression try{ Scanner inf = new Scanner (System.in); // Read input from KB/ File while(inf.hasNext()){ // read next infix expression iexp = inf.next(); // Assume method name to convert infix...
This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...
a) Show the steps that a stack uses to convert the algebraic expression a*(b+c/d from infix to postfix notation. Indicate each intermediate change in the stack and postfix output. (Be sure to identify how operator precedence is determined. b) show the steps a stack uses to evaluate the postfix expression from part (a) when (a-6, b-4, c-2, d 5) c) Show the steps a stack uses to produce an expression tree with the postfix expression from part (a). a) Show...
Code should be written in java Write a program to convert an infix expression to postfix expression using stack. Algorithm: a) Create a stack b) For each character t in the input stream If(t is an operand) append t to the output. Else if (t is a right parenthesis) Pop and append to the output until a left parenthesis is popped (but do not append this parenthesis to the output). Else if(t is an operator or left parenthesis) Pop and...
tack Assignment Objective: to be familiar with stack operations and applications Requirements: (a) Implement a Java program that take in an infix expression from the user and output the expression in postfix notation. (b) [15 points bonusl If all the operands in the input are numeric, then your program should evaluate the expression and display its value Hints: deta algorithm for converting an infix expression to the equivalent postf x listed at CSIS ace.edu/n wolf/CS122/infix ostfix.htm
Complete the following java program in which infix expressions should be converted to postfix expressions /** * An algebraic expression class that has operations such as conversion from infix * expression to postfix expression, * and evaluation of a postfix expression */ public class Expression { /** * The infix of this expression */ private String infix; /** * Constructs an expression using an infix. */ public Expression(String infix){ this.infix = infix; } /** * Converts an infix expression into...