记事本写的科学计算器(源代码)

/**
 * Calculator
 * A shareware calculator
 *
 * @author {@link http://blog.csdn.net/hongweijin Bingbing Li}
 *
 * @recitation 0101 Matt Carlson
 *
 * @date 12/7/2005 12:08AM
 *
 */
 
 import javax.swing.*;
 import javax.swing.border.*;
 import java.awt.*;
 import java.awt.event.*;
 
 public class Calculator extends JFrame implements ActionListener
 {
     public static final int WIDTH = 800;            // width of the window                 
     public static final int HEIGHT = 600;           // height of the window                
     public static final int BUTTON_WIDTH = 80;      // width of the buttons                
     public static final int BUTTON_HEIGHT = 60;     // height of the buttons               
                                                                                            
     private CardLayout dealer;                      // card layout                         
     private JPanel        deckPanel;                   // used to card layout                 
                                                                                            
     private JTextField result;                      // the calculate result                
     private JCheckBoxMenuItem scientificMode;       // the menu item of the mode           
                                                                                            
     private Box vPanel1;                            // the first line buttons of the       
                                                        // scientific mode.                    
                                                                                            
     private JButton mod;                            // the modular button                  
     private JButton xey;                            // the button of Yth root of X         
     private JButton ms;                             // save button                         
    private JButton mr;                             // release the stored value            
     private    JButton mc;                             // clear the stored value              
                                                                                            
     private double head = 0.0;                      // the first number of the equation    
     private double tail = 0.0;                      // the second number of the equation   
                                                                                            
     private boolean substitution = true;            // whether substituted the answer or not
                                                                                            
     private String operatedCommand = "NOTHING";     // the current operated command        
     private String preOperatedCommand = "NOTHING";  // remember the pre-operated command
    
     private double variableMemory = 0.0;            // the variable of the memory
    
     private JButton jButtonR;                       // the register's button
     private JButton jButtonE;                       // the edit's button
    
     private JTextField nameField;                   // the field of the name
     private JTextField countryField;                // the field of the country
     private JTextField zipField;                    // the field of the zip
     private JTextField stateField;                  // the field of the state
     private JTextField cityField ;                  // the field of the city
     private JTextField streetAddressField;          // the field of the address
    
     private JTextField first;                       // the first part of the key
     private JTextField second;                      // the second part of the key
     private JTextField third;                       // the third part of the key
     private JTextField fourth;                      // the fourth part of the key
    
    
     public Calculator()
     {
         setSize(WIDTH, HEIGHT);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setTitle("Normal Calculator");
        
         Container contentPre = getContentPane();    // the container of the window
        
         dealer = new CardLayout();                  // create a new card layout
         deckPanel = new JPanel();                   
         deckPanel.setLayout(dealer);
        
         Box content = Box.createVerticalBox();      // create a new vertical Box
        
         Box registration = Box.createVerticalBox();
        
         /****************************************************
         *    menu
         *   file
         *****************************************************/
         JMenu fileMenu = new JMenu("File");        
         JMenuItem exitItemOfFile = new JMenuItem("Exit");
         exitItemOfFile.addActionListener(this);
         fileMenu.add(exitItemOfFile);
         /**
         *    menu
         *   settings
         */
         JMenu settingsMenu = new JMenu("Settings");
         JMenuItem registrationInfo = new JMenuItem("Registration Info");
         registrationInfo.addActionListener(this);
         settingsMenu.add(registrationInfo);
        
         scientificMode = new JCheckBoxMenuItem("Scientific Mode");
         scientificMode.addActionListener(this);
         settingsMenu.add(scientificMode);
        
         JMenuBar jMenuBar = new JMenuBar();
         jMenuBar.add(fileMenu);
         jMenuBar.add(settingsMenu);
         setJMenuBar(jMenuBar);
        
         /****************************************************
         *    textFiled panel
        *****************************************************/
       
        result = new JTextField("0");                     // the initiated value of the answer
        result.setBackground(Color.CYAN);                 // set the back ground color with cyan
        result.setHorizontalAlignment(JTextField.RIGHT);  // set the horizontal alignment 
       
       
        content.add(result);
            
         content.add(paintButtons());
        
         deckPanel.add("cal", content);                    // add the calculators card to the layout
        
         registration.add(paintRegistration());            // add the register window
        
         deckPanel.add("reg", registration);               // add the register window to the layout
        
         contentPre.add(deckPanel);                   // add the cards to the container
     }
    
     /**
     * paint the buttons of the two models.
     *
     */
     public Box paintButtons()
     {
         /****************************************************
        *    Buttons
        *****************************************************/
       
         /**
         *    line1
         */
         vPanel1 = Box.createVerticalBox();                                     
        
         // add the backwards's button                                                                          
         JButton backwards = new JButton("1/X");                                    
         backwards.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));      
         backwards.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));    
         backwards.addActionListener(this);                                         
         vPanel1.add(backwards);   
                                                         
         // add the factorial button                                                                           
         JButton factorial = new JButton("X!");                                     
         factorial.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));      
         factorial.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));    
         factorial.addActionListener(this);                                         
         vPanel1.add(factorial);                                  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
抱歉,我不支持编代码。但是,以下是一个简单的计算器代码的示例: ``` <html> <head> <title>Calculator</title> <script type="text/javascript"> function calculate() { var num1 = parseFloat(document.getElementById("num1").value); var num2 = parseFloat(document.getElementById("num2").value); var operator = document.getElementById("operator").value; var result; if (operator == "add") { result = num1 + num2; } else if (operator == "subtract") { result = num1 - num2; } else if (operator == "multiply") { result = num1 * num2; } else if (operator == "divide") { result = num1 / num2; } document.getElementById("result").value = result; } </script> </head> <body> <h1>Calculator</h1> <form> <label for="num1">Number 1:</label> <input type="text" id="num1" name="num1"><br><br> <label for="num2">Number 2:</label> <input type="text" id="num2" name="num2"><br><br> <label for="operator">Operator:</label> <select id="operator" name="operator"> <option value="add">+</option> <option value="subtract">-</option> <option value="multiply">*</option> <option value="divide">/</option> </select><br><br> <input type="button" value="Calculate" onclick="calculate()"><br><br> <label for="result">Result:</label> <input type="text" id="result" name="result"> </form> </body> </html> ``` 这个代码使用 HTML、CSS 和 JavaScript 来创建一个简单的计算器。用户输入两个数字和一个运算符,然后单击“Calculate”按钮来计算结果。计算器支持加、减、乘和除四种基本运算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值