Answer:
Explanation:
Note: In case of any queries, just comment in box I would be very happy to assist all your queries
SourceCode:
// MyGUI.java:
// Import packages
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.EmptyStackException;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
// Declaare and define the class MyGUI
abstract class MyGUI extends JFrame implements ActionListener {
JTextField userInput;
JLabel inputDescLbl, resultLbl;
JPanel inputPanel, resultPanel;
JButton evlBtn;
Stack<Object> stk;
// Define the constructor MyGUI
MyGUI() {
super("Tree Address Generator");
inputPanel = new JPanel(new FlowLayout());
resultPanel = new JPanel(new FlowLayout());
setLayout(new GridLayout(2, 1));
userInput = new JTextField(20);
inputDescLbl = new JLabel("Enter Postfix Expression:");
evlBtn = new JButton("Construct Tree");
evlBtn.addActionListener(this);
resultLbl = new JLabel("Infix Expression:", SwingConstants.LEFT);
add(inputPanel);
add(resultPanel);
inputPanel.add(inputDescLbl);
inputPanel.add(userInput);
inputPanel.add(evlBtn);
resultPanel.add(resultLbl);
stk = new Stack<Object>();
}
}
//Stack.java:
// Declare and define the class Stack
class Stack {
private int[] a;
private int top, m;
public Stack(int max) {
m = max;
a = new int[m];
top = -1; }
public void push(int key) {
a[++top] = key; }
public int pop() {
return (a[top--]); }
}
// Declare and define the class Evaluation()
class Evaluation {
public int calculate(String s) {
int n, r = 0;
n = s.length();
Stack a = new Stack(n);
for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
if (ch >= '0' && ch <= '9')
a.push((int) (ch - '0'));
else if (ch == ' ')
continue;
else {
int x = a.pop();
int y = a.pop();
switch (ch) {
case '+':
r = x + y;
break;
case '-':
r = y - x;
break;
case '*':
r = x * y;
break;
case '/':
r = y / x;
break;
default:
r = 0;
}
a.push(r);
}
}
r = a.pop();
return (r);
}
}
// PostfixToInfix.java:
// Import packages
import java.util.Scanner;
import java.util.Stack;
// Declare and define the class PostfixToInfix
class PostfixToInfix {
// Determine whether the character entered is an operator or not
private boolean isOperator(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
return true;
return false;
}
// Declare and define the convert()
public String convert(String postfix) {
Stack<String> s = new Stack<>();
for (int i = 0; i < postfix.length(); i++) {
char c = postfix.charAt(i);
if (isOperator(c)) {
String b = s.pop();
String a = s.pop();
s.push("(" + a + c + b + ")");
} else
s.push("" + c);
}
return s.pop();
}
// Program starts from main()
public static void main(String[] args) {
PostfixToInfix obj = new PostfixToInfix();
Scanner sc = new Scanner(System.in);
// Prompt the user to enter the postfix expression
System.out.print("Postfix : ");
String postfix = sc.next();
// Display the expression in infix expression
System.out.println("Infix : " + obj.convert(postfix));
}
}
Output:
e Console X terminated PostfixTolnfix [Java Application] C:\Program Files\Java\jrel.8.0_121\bin\javaw.exe Postfix : ABD++C-D/
.