Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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<expSize; i++){
- testChar = exp.charAt(i);
- switch(testChar){
- case '(' :
- stack.push(testChar);
- break;
- case ')' :
- if(stack.isEmpty()){
- return false;
- }else{
- openPair = (char) stack.pop();
- if((openPair==')') && (testChar!='(')){
- JOptionPane.showMessageDialog(null, "Paranthesis does not match", "Paranthesis Error", JOptionPane.WARNING_MESSAGE);
- return false;
- }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<expSize; i++){
- testChar = expression2.charAt(i);
- switch(testChar){
- case '(':
- continue;
- case '0' :
- case '1' :
- case '2' :
- case '3' :
- case '4' :
- case '5' :
- case '6' :
- case '7' :
- case '8' :
- case '9' :
- postfix += Character.toString(testChar);
- break;
- case '+' :
- case '-' :
- case '/' :
- case '*' :
- stack.push(testChar);
- break;
- case ')' :
- postfix += stack.pop();
- break;
- case ' ':
- JOptionPane.showMessageDialog(null, "Blank space is unacceptable", "Spacing Error", JOptionPane.WARNING_MESSAGE);
- default:
- }
- }
- while(!stack.isEmpty()){
- postfix += stack.pop();
- }
- Evaluation eval = new Evaluation();
- eval.evaluate(postfix);
- }//infix to postfix end
- //calculate postfix expression
- public static int evaluate(String postfix) throws DivisionException{
- char testChar;
- int expSize = postfix.length();
- int num1=0, num2=0, result=0;
- Stack stack = new Stack();
- for(int i=0; i<expSize; i++){
- testChar = postfix.charAt(i);
- if(testChar=='+' || testChar=='-' || testChar=='*' || testChar=='/'){
- num2 = (int) stack.pop();
- num1 = (int) stack.pop();
- switch(testChar){
- case '+' :
- result = (int) stack.push(num1+num2);
- break;
- case '-' :
- result = (int) stack.push(num1-num2);
- break;
- case '*' :
- result = (int) stack.push(num1*num2);
- break;
- case '/' :
- if(num2=='0'){
- throw new DivisionException();
- }
- result = (int) stack.push(num1/num2);
- break;
- }
- }else{
- result = (int) stack.push(testChar-'0');
- }
- }
- return result;
- }
- }
- =============
- //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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement