Advertisement
Kulas_Code20

Chegg cal

Oct 5th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class Main extends JFrame{
  8.    private GridLayout glm = new GridLayout(3, 1, 5, 20);
  9.    private JPanel jp1 = new JPanel();
  10.    private JPanel jp2 = new JPanel();
  11.    private JPanel jp3 = new JPanel();
  12.    private GridLayout gl1 = new GridLayout (1, 2);
  13.    private JLabel jl1 = new JLabel("Enter Infix Expression", JLabel.CENTER);
  14.    private JTextField jtf1 = new JTextField();
  15.    private GridLayout gl2 = new GridLayout(1, 3, 5, 9);
  16.    private JButton jbEvaluate = new JButton("Evaluate");
  17.    private GridLayout gl3 = new GridLayout(1, 2);
  18.    private JLabel jl2 = new JLabel("Result", JLabel.CENTER);
  19.    private JTextField jtf2 = new JTextField();
  20.    private int result;
  21.  
  22.    public String userInput(){
  23.        return jtf1.getText();
  24.    }
  25.    public Main(){
  26.        setTitle("Infix Expression Evaluator");
  27.        setSize(500, 200);
  28.        setLayout(glm);
  29.        jp1.setLayout(gl1);
  30.        jp1.add(jl1);
  31.        jp1.add(jtf1);
  32.        add(jp1);
  33.        jp2.setLayout(gl2);
  34.        jp2.add(new JLabel(""));
  35.        jp2.add(jbEvaluate);
  36.        jp2.add(new JLabel(""));
  37.        add(jp2);
  38.        jp3.setLayout(gl3);
  39.        jp3.add(jl2);
  40.        jp3.add(jtf2);
  41.        jtf2.setEditable(false);
  42.        jtf2.setBackground(Color.lightGray);
  43.        add(jp3);
  44.        setLocationRelativeTo(null);
  45.        setResizable(true);
  46.        setVisible(true);
  47.    }
  48.  
  49.    class EnterActionListener implements ActionListener{
  50.        public void actionPerformed(ActionEvent ae){
  51.            try {
  52.                result = Evaluation.testPair(userInput());
  53.                jtf2.setText(Integer.toString(result));
  54.            } catch (DivisionException de) {
  55.                new DivisionException();
  56.            }
  57.        }
  58.    }
  59.  
  60.    public static void main (String[] args){
  61.        Main main = new Main();
  62.    }
  63. }
  64.  
  65. ===============
  66.  
  67. //Evaluation.java
  68.  
  69. import java.util.*;
  70. import javax.swing.JOptionPane;
  71.  
  72. public class Evaluation {
  73.  
  74.    //infix to postfix
  75.    private String expression;
  76.    private int expSize;
  77.    private char testChar, openPair;
  78.  
  79.    //testing () pair
  80.    public boolean testPair(String exp){
  81.        expression = exp;
  82.        Stack stack = new Stack();
  83.        expSize = expression.length();
  84.  
  85.        for(int i=0; i<expSize; i++){
  86.            testChar = exp.charAt(i);
  87.            switch(testChar){
  88.            case '(' :
  89.                stack.push(testChar);
  90.                break;
  91.            case ')' :
  92.                if(stack.isEmpty()){
  93.                    return false;
  94.                }else{
  95.                    openPair = (char) stack.pop();
  96.                    if((openPair==')') && (testChar!='(')){
  97.      JOptionPane.showMessageDialog(null, "Paranthesis does not match", "Paranthesis Error", JOptionPane.WARNING_MESSAGE);
  98.      return false;
  99.  
  100.                    }else{
  101.                        break;
  102.                    }
  103.                }
  104.            }
  105.        }
  106.        if(stack.isEmpty()){
  107.            return true;
  108.        }else{
  109.            return false;
  110.        }
  111.        Evaluation.InfixToPostfix(expression);
  112.    }
  113.  
  114.    public static void InfixToPostfix(String infix) throws DivisionException{
  115.        char testChar;
  116.        String expression2 = infix;
  117.        int expSize = expression2.length();
  118.        Stack stack = new Stack();
  119.        String postfix = "";
  120.        for(int i=0; i<expSize; i++){
  121.            testChar = expression2.charAt(i);
  122.            switch(testChar){
  123.            case '(':
  124.                continue;
  125.            case '0' :
  126.            case '1' :
  127.            case '2' :
  128.            case '3' :
  129.            case '4' :
  130.            case '5' :
  131.            case '6' :
  132.            case '7' :
  133.            case '8' :
  134.            case '9' :
  135.                postfix += Character.toString(testChar);
  136.                break;
  137.            case '+' :
  138.            case '-' :
  139.            case '/' :
  140.            case '*' :
  141.                stack.push(testChar);
  142.                break;
  143.            case ')' :
  144.                postfix += stack.pop();
  145.                break;
  146.            case ' ':
  147.                JOptionPane.showMessageDialog(null, "Blank space is unacceptable", "Spacing Error", JOptionPane.WARNING_MESSAGE);
  148.            default:
  149.            }
  150.        }
  151.      
  152.        while(!stack.isEmpty()){
  153.            postfix += stack.pop();
  154.        }
  155.      
  156.        Evaluation eval = new Evaluation();
  157.        eval.evaluate(postfix);
  158.    }//infix to postfix end
  159.  
  160.    //calculate postfix expression
  161.    public static int evaluate(String postfix) throws DivisionException{
  162.        char testChar;
  163.        int expSize = postfix.length();
  164.        int num1=0, num2=0, result=0;
  165.        Stack stack = new Stack();
  166.      
  167.        for(int i=0; i<expSize; i++){
  168.            testChar = postfix.charAt(i);
  169.          
  170.            if(testChar=='+' || testChar=='-' || testChar=='*' || testChar=='/'){
  171.                num2 = (int) stack.pop();
  172.                num1 = (int) stack.pop();
  173.              
  174.                switch(testChar){
  175.                case '+' :
  176.                    result = (int) stack.push(num1+num2);
  177.                    break;
  178.                case '-' :
  179.                    result = (int) stack.push(num1-num2);
  180.                    break;
  181.                case '*' :
  182.                    result = (int) stack.push(num1*num2);
  183.                    break;
  184.                case '/' :
  185.                    if(num2=='0'){
  186.                        throw new DivisionException();
  187.                    }
  188.                    result = (int) stack.push(num1/num2);
  189.                    break;
  190.                }
  191.            }else{
  192.                result = (int) stack.push(testChar-'0');
  193.            }
  194.        }
  195.        return result;
  196.    }
  197. }
  198.  
  199. =============
  200.  
  201. //DivisionException.java
  202.  
  203. import javax.swing.JOptionPane;
  204.  
  205. public class DivisionException extends Exception{
  206.    public DivisionException(){
  207.        JOptionPane.showMessageDialog(null, "Division Error", "Cannot divide a number by 0", JOptionPane.WARNING_MESSAGE);
  208.    }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement