Question: (JAVA) I have come up with infix to postfix calculator. I got some issues need to be corrected in order to run it without any
(JAVA)
I have come up with infix to postfix calculator. I got some issues need to be corrected in order to run it without any errors.
I am trying to change the input String expression to postfix and then calculate the postfix expression.
Thank you in advance.
========================
//Main.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*;
public class Main extends JFrame{ private GridLayout glm = new GridLayout(3, 1, 5, 20); private JPanel jp1 = new JPanel(); private JPanel jp2 = new JPanel(); private JPanel jp3 = new JPanel(); private GridLayout gl1 = new GridLayout (1, 2); private JLabel jl1 = new JLabel("Enter Infix Expression", JLabel.CENTER); private JTextField jtf1 = new JTextField(); private GridLayout gl2 = new GridLayout(1, 3, 5, 9); private JButton jbEvaluate = new JButton("Evaluate"); private GridLayout gl3 = new GridLayout(1, 2); private JLabel jl2 = new JLabel("Result", JLabel.CENTER); private JTextField jtf2 = new JTextField(); private int result; public String userInput(){ return jtf1.getText(); } public Main(){ setTitle("Infix Expression Evaluator"); setSize(500, 200); setLayout(glm); jp1.setLayout(gl1); jp1.add(jl1); jp1.add(jtf1); add(jp1); jp2.setLayout(gl2); jp2.add(new JLabel("")); jp2.add(jbEvaluate); jp2.add(new JLabel("")); add(jp2); jp3.setLayout(gl3); jp3.add(jl2); jp3.add(jtf2); jtf2.setEditable(false); jtf2.setBackground(Color.lightGray); add(jp3); setLocationRelativeTo(null); setResizable(true); setVisible(true); }
class EnterActionListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ try { result = Evaluation.testPair(userInput()); jtf2.setText(Integer.toString(result)); } catch (DivisionException de) { new DivisionException(); } } } public static void main (String[] args){ Main main = new Main(); } }
===============
//Evaluation.java
import java.util.*; import javax.swing.JOptionPane;
public class Evaluation { //infix to postfix private String expression; private int expSize; private char testChar, openPair;
//testing () pair public boolean testPair(String exp){ expression = exp; Stack stack = new Stack(); expSize = expression.length();
for(int i=0; i }else{ break; } } } } if(stack.isEmpty()){ return true; }else{ return false; } Evaluation.InfixToPostfix(expression); } public static void InfixToPostfix(String infix) throws DivisionException{ char testChar; String expression2 = infix; int expSize = expression2.length(); Stack stack = new Stack(); String postfix = ""; for(int i=0; i ============= //DivisionException.java import javax.swing.JOptionPane; public class DivisionException extends Exception{ public DivisionException(){ JOptionPane.showMessageDialog(null, "Division Error", "Cannot divide a number by 0", JOptionPane.WARNING_MESSAGE); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
