Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RPNevaluator {
- private StringBuffer outputBuffer;
- private Stack <Double> myStack = new Stack <Double> ();
- RPNevaluator( String inputString ) {
- outputBuffer = new StringBuffer();
- StringTokenizer sTok = new StringTokenizer ( inputString, "+-/* ", true);
- String temp;
- while (sTok.hasMoreTokens()) {
- temp = ( String ) sTok.nextElement();
- switch ( temp.charAt(0)) {
- case '+':
- double valueA = ( myStack.pop() + " " );
- double valueB = ( myStack.pop() + " " );
- myStack.push(temp);
- break;
- case '-':
- outputBuffer.append( myStack.pop() + " " );
- myStack.push(temp);
- break;
- case '*':
- outputBuffer.append( myStack.pop() + " " );
- myStack.push(temp);
- break;
- case '/':
- outputBuffer.append( myStack.pop() + " " );
- myStack.push(temp);
- break;
- case ' ':
- break;
- default:
- outputBuffer.append( temp + " " );
- break;
- }
- }
- }
- public static void main(String[] args) {
- }
- }
- // This is my Keypad Class!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.UIManager.*;
- public class KeyWindow extends JFrame implements ActionListener{
- JTextField display;
- JPanel showPanel;
- JPanel numPanel;
- JPanel opPanel;
- JButton[] numButton = new JButton[13];
- JButton[] opButton = new JButton[4];
- private class Terminator extends WindowAdapter {
- public void windowClosing(WindowEvent e) {
- System.out.println("Window closing selected: terminating program");
- System.exit(0);
- } // End of windowClosing
- } //End of Adapter Class
- KeyWindow() {
- try {
- for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
- System.out.println(info.getClassName());
- if ("Nimbus".equals(info.getName())) {
- UIManager.setLookAndFeel(info.getClassName());
- break;
- } // End of if
- } // End of for
- } catch (UnsupportedLookAndFeelException e) { // handle exception
- } catch (ClassNotFoundException e) { // handle exception
- } catch (InstantiationException e) { // handle exception
- } catch (IllegalAccessException e) { // handle exception}
- } // end of try
- setTitle("Keypad Window");
- setSize(450, 200);
- Container content = getContentPane();
- content.setLayout ( new FlowLayout() );
- // Building the display field + jpanel
- display = new JTextField(15);
- JPanel showPanel = new JPanel();
- showPanel.setSize(100,100);
- showPanel.setLayout ( new GridLayout(1,1) );
- showPanel.setBorder( BorderFactory.createLineBorder ( Color.BLUE, 5 ) );
- showPanel.add( display );
- content.add(showPanel);
- // Initialize numButton buttons using a for loop
- for ( int c = 0; c < 10 ; c++ ){
- numButton[c] = new JButton ( Integer.toString ( c ) );
- } // end of for loop
- // Buttons 9 - 12 contain names not able to easily be defined in a for loop. Need to manually initialize them.
- numButton[10] = new JButton ( "." );
- numButton[11] = new JButton ( "Clear" );
- numButton[12] = new JButton ( "Enter" );
- // Jpanel for number buttons
- JPanel numPanel = new JPanel();
- numPanel.setSize(200, 300);
- numPanel.setLayout ( new GridLayout(5,2) );
- numPanel.setBorder( BorderFactory.createLineBorder ( Color.BLUE, 5 ) );
- // Use a for loop to add each numbutton to the panel
- for ( int c = 0; c < 13; c++ ){
- numButton[c].addActionListener ( this );
- numPanel.add ( numButton[c] );
- } // End of for loop
- content.add(numPanel);
- // opButtons cannot be done using a loop because their names aren't able to be sequenced. Need to initialize manually
- opButton[0] = new JButton ("+");
- opButton[1] = new JButton ("-");
- opButton[2] = new JButton ("*");
- opButton[3] = new JButton ("/");
- // Jpanel for operator buttons
- JPanel opPanel = new JPanel();
- opPanel.setSize(200, 300);
- opPanel.setLayout ( new GridLayout(4,1) );
- opPanel.setBorder( BorderFactory.createLineBorder ( Color.BLUE, 5 ) );
- // Use a for loop to add each button to the panel
- for (int c = 0; c < 4; c++){
- opButton[c].addActionListener( this );
- opPanel.add ( opButton[c]);
- } // end of for loop
- content.add(opPanel);
- setVisible(true);
- addWindowListener(new Terminator());
- } // end of key window method
- public void actionPerformed(ActionEvent evt) {
- // If statement to clear the display field when "clear" is pressed
- if (evt.getSource() == numButton[11]){
- display.setText("");
- } // End if
- // Displays the buttons in the display area when pressed
- for(int c = 0; c < numButton.length; c++){
- if (evt.getSource()== numButton[c]&& evt.getSource()!= numButton[11]){
- display.setText(display.getText()+numButton[c].getText());
- } // End of if statement
- } // End of for loop
- // (WIP) Operator buttons
- // Each operator stores the data before it in a stack
- // Clear display screen to allow addition of new data
- for(int c = 0; c < opButton.length; c++){
- if (evt.getSource()== opButton[c]){
- display.setText(display.getText()+numButton[c].getText());
- } // End of if statement
- } // End of for loop
- // (WIP) Enter button
- // Enter needs to evaluate expression and display the result
- // Converts infix to reverse polish notation
- // Evaluate reverse polish notation
- // Display result
- } // End of action performed method
- public static void main(String[] args) {
- new KeyWindow();
- } // End of main method
- } // End of key window class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement