Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Stack<T>
- {
- private T [ ] spaces;
- private int TOS;
- private int capacity;
- public Stack ( )
- {
- capacity = 50;
- spaces = (T [ ]) new Object [capacity];
- TOS = capacity;
- }
- public boolean overflow ()
- {
- return (TOS == 0);
- }
- public boolean empty ()
- {
- return (TOS == capacity);
- }
- public boolean push (T newMem)
- {
- boolean ok = true;
- if (overflow ())
- {
- System.out.println ("stack overflow");
- ok = false;
- }
- else
- {
- spaces[--TOS] = newMem;
- System.out.println (newMem + " pushed");
- }
- return ok;
- }
- public T pop ()
- {
- T popped = null;
- if (empty ())
- System.out.println ("empty");
- else
- popped = spaces[TOS++];
- return popped;
- }
- public T peekTOS ( )
- {
- T atTOS = null;
- if (!empty ())
- atTOS = spaces [TOS];
- return atTOS;
- }
- }
- ======================================================================================================================
- public class InfixToPostfix
- {
- public static String convert (String inString)
- {
- Stack<String> oprStack = new Stack<String> ( );
- String postString = "";
- inString = inString.trim ( );
- int len = inString.length ( );
- int i = 0;
- String oprString = "";
- String oprAtTop = "";
- char c;
- for ( ;i < len; i++)
- {
- //get one char from the infix string
- c = inString.charAt (i);
- //skip spaces
- if (c == ' ');
- //push ( to the operator stack and treat that as the lowest priority operator
- else if (c == '(')
- {
- //convert a single char to a string for push
- oprString = "" + c;
- oprStack.push (oprString);
- }
- //write every operand directly to the postfix string
- else if ((c >= '0') && (c <= '9'))
- postString = postString + c;
- //pop all the operators and write them to the postfix string till a matching (
- else if (c == ')')
- {
- while (!oprStack.empty ( ))
- {
- oprString = oprStack.pop ( );
- if (!oprString.equals ("("))
- postString = postString + oprString;
- else
- break;
- }
- }
- //pop from the operator stack all the operators which have a higher or same priority then the one from the infix string
- //and write them to the postfix string
- //lastly, push the operator from the ininfix string to the operator stack
- else
- {
- oprString = "" + c;
- while (!oprStack.empty( ))
- {
- oprAtTop = oprStack.peekTOS ( );
- if (priority (oprString) <= priority (oprAtTop))
- {
- oprAtTop = oprStack.pop ( );
- postString = postString + oprAtTop;
- }
- else
- break;
- }
- oprStack.push (oprString);
- }
- }
- //clear the operator stack and write every popped operaotr to the postfix string
- while (!oprStack.empty ( ))
- {
- oprAtTop = oprStack.pop ( );
- postString = postString + oprAtTop;
- }
- // System.out.println ("postfix = " + postString);
- return postString;
- }
- private static int priority (String aString)
- {
- int value = -1;
- if (aString.equals ("("))
- value = 0;
- if ((aString.equals ("+")) || (aString.equals ("-")))
- value = 1;
- if ((aString.equals ("/")) || (aString.equals ("*")))
- value = 2;
- return value;
- }
- }
- ===================================================================================================================
- public class PostfixSolver {
- String inString = "";
- int n1, n2;
- String opr;
- int solvedN;
- String outString = "";
- Stack inStack = new Stack();
- public String solve(String inString) {
- int len = inString.length();
- int n;
- for (int i = 0; i < len; i++) {
- n = inString.charAt(i);
- if(n >= 0 || n <= 9) {
- inStack.push(n);
- }
- if(Integer.toString(n) == "=" || Integer.toString(n) == "-" || Integer.toString(n) == "/" ||
- Integer.toString(n) == "*") {
- opr = Character.toString(inString.charAt(i));
- n1 = (int)inStack.pop();
- n2 = (int)inStack.pop();
- switch (opr) {
- case "*": solvedN = n1*n2;
- break;
- case "/": solvedN = n1/n2;
- break;
- case "+": solvedN = n1+n2;
- break;
- case "-": solvedN = n1-n2;
- break;
- }
- inStack.push(solvedN);
- }
- }
- return Integer.toString((int)inStack.pop());
- }
- }
- ===================================================================================================================
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Calculator extends JFrame implements ActionListener
- {
- private JButton[] buttons;
- private JTextField display;
- private JPanel buttonPanel;
- private Container contentPane;
- InfixToPostfix converter = new InfixToPostfix();
- PostfixSolver solve = new PostfixSolver();
- public static void main (String[] args)
- {
- Calculator myCalculator = new Calculator ( );
- myCalculator.setVisible (true);
- }
- public Calculator ( )
- {
- setSize (300,600);
- setTitle ("simple calculator");
- contentPane = getContentPane ( );
- contentPane.setLayout (new BorderLayout ( ));
- display = new JTextField (30);
- contentPane.add (display,BorderLayout.NORTH);
- buttonPanel = new JPanel ( );
- contentPane.add (buttonPanel, BorderLayout.CENTER);
- buttonPanel.setLayout (new GridLayout (4,4));
- buttons = new JButton[18];
- for (int i = 0; i < 10; i++)
- {
- buttons[i] = new JButton((new Integer (i)).toString ( ));
- buttons[i].addActionListener (this);
- buttonPanel.add (buttons[i]);
- }
- buttons[10] = new JButton ("+");
- buttons[10].addActionListener (this);
- buttonPanel.add (buttons[10]);
- buttons[11] = new JButton ("-");
- buttons[11].addActionListener (this);
- buttonPanel.add (buttons[11]);
- buttons[12] = new JButton ("/");
- buttons[12].addActionListener (this);
- buttonPanel.add (buttons[12]);
- buttons[13] = new JButton ("*");
- buttons[13].addActionListener (this);
- buttonPanel.add (buttons[13]);
- buttons[14] = new JButton ("(");
- buttons[14].addActionListener (this);
- buttonPanel.add (buttons[14]);
- buttons[15] = new JButton (")");
- buttons[15].addActionListener (this);
- buttonPanel.add (buttons[15]);
- buttons[16] = new JButton ("=");
- buttons[16].addActionListener(this);
- buttonPanel.add(buttons[16]);
- buttons[17] = new JButton ("C");
- buttons[17].addActionListener(this);
- buttonPanel.add(buttons[17]);
- setDefaultCloseOperation (EXIT_ON_CLOSE);
- }
- public void actionPerformed (ActionEvent x)
- {
- String which = x.getActionCommand ( );
- if (which.equals ("C"))
- display.setText ("");
- else if (which.equals ("=")) {
- String postFix = converter.convert(display.getText());
- String solution = solve.solve(postFix);
- display.setText(postFix + " = " + solution);
- }
- else
- display.setText (display.getText() + which);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement