⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calcengine.java

📁 A calculator source code written in java
💻 JAVA
字号:
/** * The main part of the calculator doing the calculations. nnnn *  * @author  David J. Barnes and Michael Kolling  * @version 1.0 */public class CalcEngine{    // The calculator's state is maintained in three fields:    //     buildingDisplayValue, haveLeftOperand, and lastOperator.        // Are we already building a value in the display, or will the    // next digit be the first of a new one?    private boolean buildingDisplayValue;    // Has a left operand already been entered (or calculated)?    private boolean haveLeftOperand;    // The most recent operator that was entered.    private char lastOperator;    // The current value (to be) shown in the display.    private int displayValue;    // The value of an existing left operand.    private int leftOperand;    /**     * Create a CalcEngine instance.     */    public CalcEngine()    {        clear();    }    /**     * Return the value that should currently be displayed     * on the calculator display.     */    public int getDisplayValue()    {        return displayValue;    }    /**     * A number button was pressed.     * Either start a new operand, or incorporate this number as     * the least significant digit of an existing one.     */    public void numberPressed(int number)    {        if(buildingDisplayValue) {            // Incorporate this digit.            displayValue = displayValue*10 + number;        }        else {            // Start building a new number.            displayValue = number;            buildingDisplayValue = true;        }    }    /**     * The 'plus' button was pressed.      */    public void plus()    {        applyOperator('+');    }    /**     * The 'minus' button was pressed.     */    public void minus()    {        applyOperator('-');    }        /**     * The '=' button was pressed.     */    public void equals()    {        // This should completes the building of a second operand,        // so ensure that we really have a left operand, an operator        // and a right operand.        if(haveLeftOperand &&                lastOperator != '?' &&                buildingDisplayValue) {            calculateResult();            lastOperator = '?';            buildingDisplayValue = false;        }        else {            keySequenceError();        }    }    /**     * The 'C' (clear) button was pressed.     * Reset everything to a starting state.     */    public void clear()    {        lastOperator = '?';        haveLeftOperand = false;        buildingDisplayValue = false;        displayValue = 0;    }    /**     * Return the title of this calculation engine.     */    public String getTitle()    {        return "Java Calculator";    }    /**     * Return the author of this engine.     */    public String getAuthor()    {        return "David J. Barnes and Michael Kolling";    }    /**     * Return the version number of this engine.     */    public String getVersion()    {       return "Version 1.0";    }    /**     * Combine leftOperand, lastOperator, and the     * current display value.     * The result becomes both the leftOperand and     * the new display value.     */    private void calculateResult()    {        switch(lastOperator) {            case '+':                displayValue = leftOperand + displayValue;                haveLeftOperand = true;                leftOperand = displayValue;                break;            case '-':                displayValue = leftOperand - displayValue;                haveLeftOperand = true;                leftOperand = displayValue;                break;            default:                keySequenceError();                break;        }    }        /**     * Apply the given operator.     */    private void applyOperator(char operator)    {        // If we are not in the process of building a new operand        // then it is an error, unless we have just calculated a        // result using '='.        if(!buildingDisplayValue &&                    !(haveLeftOperand && lastOperator == '?')) {            keySequenceError();            return;        }        if(lastOperator != '?') {            // First apply the previous operator.            calculateResult();        }        else {            // The displayValue now becomes the left operand of this            // new operator.            haveLeftOperand = true;            leftOperand = displayValue;        }        lastOperator = operator;        buildingDisplayValue = false;    }    /**     * Report an error in the sequence of keys that was pressed.     */    private void keySequenceError()    {        System.out.println("A key sequence error has occurred.");        // Reset everything.        clear();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -