Guest User

Untitled

a guest
Jan 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. //package v1ch09.Calculator;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.util.ArrayList;
  7. /*import java.util.Arrays;
  8. import java.util.Collection;
  9. import java.util.Scanner;*/
  10.  
  11.  
  12. /**
  13.  * @version 1.33 2007-06-12
  14.  * @author Cay Horstmann
  15.  */
  16. public class Calculator
  17. {
  18.    public static void main(String[] args)
  19.    {
  20.       EventQueue.invokeLater(new Runnable()
  21.          {
  22.             public void run()
  23.             {
  24.                CalculatorFrame frame = new CalculatorFrame();
  25.                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26.                frame.setVisible(true);
  27.             }
  28.          });
  29.    }
  30. }
  31.  
  32. /**
  33.  * A frame with a calculator panel.
  34.  */
  35. class CalculatorFrame extends JFrame
  36. {
  37.    public CalculatorFrame()
  38.    {
  39.       setTitle("Calculator");
  40.       CalculatorPanel panel = new CalculatorPanel();
  41.       add(panel);
  42.       pack();
  43.    }
  44. }
  45.  
  46. /**
  47.  * A panel with calculator buttons and a result display.
  48.  */
  49. class CalculatorPanel extends JPanel
  50. {
  51.    public CalculatorPanel()
  52.    {
  53.       setLayout(new BorderLayout());
  54.  
  55.       result = 0;
  56.       lastCommand = "=";
  57.       start = true;
  58.  
  59.       // add the display
  60.  
  61.       display = new JButton("0");
  62.       display.setEnabled(false);
  63.       add(display, BorderLayout.NORTH);
  64.  
  65.       ActionListener insert = new InsertAction();
  66.       ActionListener command = new CommandAction();
  67.  
  68.       // add the buttons in a 4 x 4 grid
  69.  
  70.       panel = new JPanel();
  71.       panel.setLayout(new GridLayout(4, 4));
  72.  
  73.       addButton("7", insert);
  74.       addButton("8", insert);
  75.       addButton("9", insert);
  76.       addButton("/", command);
  77.  
  78.       addButton("*", command);
  79.       addButton("4", insert);
  80.       addButton("5", insert);
  81.       addButton("6", insert);
  82.      
  83.       addButton("+", command);
  84.       addButton("-", command);
  85.       addButton("1", insert);
  86.       addButton("2", insert);
  87.  
  88.       addButton("3", insert);
  89.       addButton(".", insert);
  90.       addButton("=", command);
  91.       addButton("0", insert);
  92.       addButton("MS", command);
  93.       addButton("MR", command);
  94.      
  95.  
  96.       add(panel, BorderLayout.CENTER);
  97.    }
  98.  
  99.    /**
  100.     * Adds a button to the center panel.
  101.     * @param label the button label
  102.     * @param listener the button listener
  103.     */
  104.    private void addButton(String label, ActionListener listener)
  105.    {
  106.       JButton button = new JButton(label);
  107.       button.addActionListener(listener);
  108.       panel.add(button);
  109.    }
  110.  
  111.    /**
  112.     * This action inserts the button action string to the end of the display text.
  113.     */
  114.    private class InsertAction implements ActionListener
  115.    {
  116.       public void actionPerformed(ActionEvent event)
  117.       {
  118.          String input = event.getActionCommand();
  119.          if (start)
  120.          {
  121.             display.setText("");
  122.             start = false;
  123.          }
  124.          display.setText(display.getText() + input);
  125.       }
  126.    }
  127.  
  128.    /**
  129.     * This action executes the command that the button action string denotes.
  130.     */
  131.    public class CommandAction implements ActionListener
  132.    {
  133.       // int flag = 0;
  134.        
  135.       public void actionPerformed(ActionEvent event)
  136.       {
  137.          String command = event.getActionCommand();
  138.  
  139.          if (start)
  140.          {
  141.             //String command = event.getActionCommand();
  142.             if (command.equals("-"))
  143.             {
  144.                display.setText(command);
  145.                start = false;
  146.             }
  147.             else if (command.equals("MS"))
  148.                 {
  149.                 lastCommand = command;
  150.                 calculate(Double.parseDouble(display.getText()));
  151.                
  152.                 }
  153.             else lastCommand = command;
  154.          }
  155.          else
  156.          {
  157.             if (command.equals("MS"))
  158.             {
  159.             lastCommand = command;
  160.             //calculate(Double.parseDouble(display.getText()));
  161.            
  162.             }
  163.             calculate(Double.parseDouble(display.getText()));
  164.             lastCommand = command;
  165.             start = true;
  166.          }
  167.       }
  168.    }
  169.  
  170.    /**
  171.     * Carries out the pending calculation.
  172.     * @param x the value to be accumulated with the prior result.
  173.     */
  174.    public void calculate(double x)
  175.    {
  176.       Stack_10080 obj = new Stack_10080();
  177.       if (lastCommand.equals("+"))
  178.           {
  179.             result += x;
  180.             display.setText("" + result);
  181.           }
  182.       else if (lastCommand.equals("-"))
  183.           {
  184.             result -= x;
  185.             display.setText("" + result);
  186.           }
  187.       else if (lastCommand.equals("*"))
  188.           {
  189.             result *= x;
  190.             display.setText("" + result);
  191.           }
  192.       else if (lastCommand.equals("/"))
  193.           {
  194.             result /= x;
  195.             display.setText("" + result);
  196.           }
  197.       else if (lastCommand.equals("="))
  198.           {
  199.             result = x;
  200.             display.setText("" + result);
  201.           }
  202.       else if (lastCommand.equals("MS"))
  203.           {
  204.             obj.push(x);
  205.             lastCommand = "=";
  206.             result=x;
  207.             //lastCommand = "=";
  208.             //result = 0;
  209.             //display.setText("" + "0.0");
  210.           }
  211.       else if (lastCommand.equals("MR"))
  212.           {
  213.           Double temp = obj.pop();
  214.           result = temp;
  215.           }
  216.      
  217.       //display.setText("" + result);
  218.    }
  219.  
  220.    private JButton display;
  221.    private JPanel panel;
  222.    private double result;
  223.    private String lastCommand;
  224.    private boolean start;
  225. }
  226.  
  227.  
  228. class Stack_10080 {
  229.    
  230.     ArrayList<Double>str = new ArrayList<Double>(1);
  231.     public void push(Double str2)
  232.     {
  233.             str.add(str2);
  234.             System.out.println(str2);
  235.             /*for(int i=0;i<str.size();i++)
  236.             {
  237.                 System.out.println(str.get(i));
  238.             }*/
  239.        
  240.     }
  241.     public Double pop()
  242.     {
  243.         Double temp1,temp3=0.0;
  244.         if(str.size()==0)
  245.         {
  246.             return temp3;
  247.         }
  248.         temp1 = str.get(str.size()-1);
  249.         str.remove(str.size()-1);
  250.         System.out.println(temp1);
  251.         return temp1;      
  252.     }
  253. }
Add Comment
Please, Sign In to add comment