• 大小: 23.44 KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-11-26
  • 语言: Java
  • 标签: 计算器  

资源简介

一个计算器,有多种功能,如取对数、求平方根。最重要的是利用堆栈算法实现运算符优先级。

资源截图

代码片段和文件信息

/*package com.joshua.cal; */ 
  
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.linkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
  
public class Calculator {  
  
    public final Stack numStack = new Stack();  
    private final Stack opStack = new Stack();  
  
    private char currentOperator;  
    private char opStackTop;  
  
    private int i;  
    private String expression;  
  
    @SuppressWarnings(“rawtypes“)  
    public void exec(String expression) {  
        try {  
            clean();  
            if (expression == null || expression.isEmpty()) {  
                throw new IllegalArgumentException(“Blank expression!“);  
            }  
            this.expression = expression;  
            opStack.push(TERMINATE_TOKENS.START_END_MARK);  
            List tokens = TOKENIZER.exec(expression  
                    + TERMINATE_TOKENS.START_END_MARK);  
            for (; i < tokens.size(); i++) {  
                final object token = tokens.get(i);  
                if (token instanceof Double) {  
                    processOperand((Double) token);  
                } else {  
                    processOperator((Character)token);  
                }  
            }  
        } catch (Throwable e) {  
            System.err.println(String.format(  
                    “Incorret expression: %s\nError: %s“ expression  
                    e.getMessage()));  
        }  
    }  
  
    private void processOperand(final double operand) {  
        numStack.push(operand);  
    }  
  
    private void processOperator(final char currentOperator) {  
        this.currentOperator = currentOperator;  
        this.opStackTop = opStack.peek();  
        char calMode = CALCULATE_MODE.getRule(currentOperator opStackTop);  
        switch (calMode) {  
        case ‘>‘:  
            processStackHigerPriorityOperator();  
            break;  
        case ‘<‘:  
            processStackLowerPriorityOperator();  
            break;  
        case ‘=‘:  
            processStackEqualPriorityOperator();  
            break;  
        default:  
            break;  
        }  
    }  
  
    private void processStackLowerPriorityOperator() {  
        opStack.push(currentOperator);  
    }  
  
    private void processStackHigerPriorityOperator() {  
        numStack.push(CALCULATE.exec(opStack.pop() numStack.pop()  
                numStack.pop()));  
        --i; // pointer back to the previous operator.  
    }  
  
    private void processStackEqualPriorityOperator() {  
        if (TERMINATE_TOKENS.START_END_MARK == currentOperator) {  
            System.out.println(expression + “ = “ + numStack.peek());  
        } else if (‘)‘ == currentOperator) {  
            opStack.pop();  
        }  
    }  
  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        301  2014-06-18 09:27  Caculator02\.classpath

     文件        387  2014-06-17 14:06  Caculator02\.project

     文件        629  2014-06-17 14:06  Caculator02\.settings\org.eclipse.jdt.core.prefs

     文件       1344  2014-06-19 17:52  Caculator02\bin\CALCULATE.class

     文件       1859  2014-06-19 17:52  Caculator02\bin\CALCULATE_MODE.class

     文件       3429  2014-06-19 17:52  Caculator02\bin\Calculator.class

     文件       5962  2014-06-19 17:54  Caculator02\bin\Cindy.class

     文件       1724  2014-06-19 17:52  Caculator02\bin\Clear.class

     文件       1160  2014-06-19 17:52  Caculator02\bin\Dot.class

     文件       1537  2014-06-19 17:52  Caculator02\bin\Num.class

     文件       3066  2014-06-19 17:52  Caculator02\bin\Signs.class

     文件       1582  2014-06-19 17:52  Caculator02\bin\SM.class

     文件       2487  2014-06-19 17:52  Caculator02\bin\TERMINATE_TOKENS.class

     文件       2802  2014-06-19 17:52  Caculator02\bin\TOKENIZER.class

     文件       8509  2014-06-18 20:52  Caculator02\src\Calculator.java

     文件       6394  2014-06-19 17:54  Caculator02\src\Cindy.java

     文件       1360  2014-06-17 14:06  Caculator02\src\Clear.java

     文件        444  2014-06-17 14:06  Caculator02\src\Dot.java

     文件        790  2014-06-17 14:06  Caculator02\src\Num.java

     文件       3809  2014-06-18 20:58  Caculator02\src\Signs.java

     文件        842  2014-06-17 14:06  Caculator02\src\SM.java

     目录          0  2014-06-17 14:06  Caculator02\.settings

     目录          0  2014-06-19 17:52  Caculator02\bin

     目录          0  2014-06-18 21:07  Caculator02\src

     目录          0  2014-06-18 09:27  Caculator02

----------- ---------  ---------- -----  ----

                50417                    25


评论

共有 条评论