200行Java代码实现计算器程序

发现了大学时候写的计算器小程序,还有个图形界面,能够图形化展示表达式语法树,哈哈;)
只有200行Java代码,不但能够计算加减乘除,还能够匹配小括号~

代码点评:

从朴素的界面配色到简单易懂错误提示,无不体现了“用户体验”至上的设计理念;代码异常处理全面合理、滴水不漏,代码缩进优雅大方,变量命名直观易懂;再结合长度适中简单明了的注释,程序整体给人一种清新脱俗之感。背后不难看出作者对学习的热爱以及对设计的苛求,工匠精神可见一斑,真可谓是大学数据结构学以致用的典范!

关于数据结构的干货,可参考博主的《深入理解Java集合框架》系列文章,一定不让你失望。

实现算法参考严蔚敏的《数据结构(C语言版)》第三章“栈和队列”,3.2.5节“表达式求值”。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Stack;

import javax.swing.JFrame;

/** * 图形界面的计算器程序,只能计算加减乘除, * 算式中可以有小括号。数字可以是小数 */
public class CalcGUI extends JFrame{
    private static final long serialVersionUID = 1L;
    private TreeNode resultTree;
    private String textFieldString;
    private boolean calcSuccess = true;
    private char ops[][] = {
            {'>', '>', '<', '', '>'},
            {'>', '>', '<', '', '>'},
            {'>', '>', '>', '>', '', '>'},
            {'>', '>', '>', '>', '', '>'},
            {'<', '<',="" '=", " e'},="" {'e',="" 'e',="" 'e'},="" {'<',="" e'){="" g.setcolor(color.black);="" g.drawstring(string.valueof(node.op),="" pme.x,="" pme.y);="" }else{="" g.drawstring(string.valueof(node.value),="" pme.x="" -="" diameter="" 2,="" }="" drawtree(g,="" node.lft,="" new="" point(pme.x="" width="" pme.y="" +="" levelheight),="" pme);="" node.rt,="" public="" treenode="" calc(string="" instr)="" throws="" exception{="" opsstack.push('#');="" stringbuilder="" buf="new" stringbuilder();="" int="" i="0;" while(i="" <="" instr.length()){="" if(character.isdigit(instr.charat(i))="" ||="" instr.charat(i)="=" '.'){="" number="" buf.delete(0,="" buf.length());="" instr.length()="" &&="" (character.isdigit(instr.charat(i))="" '.'))="" buf.append(instr.charat(i++));="" double="" nodesstack.push(new="" treenode(number));="" }else="" if(instr.charat(i)="=" '){="" i++;="" continue;="" operation="" char="" op="inStr.charAt(i);" subnew="getSub(op);" boolean="" goon="true;" while(goon){="" if(opsstack.isempty())="" throw="" exception("运算符太少!");="" opformer="opsStack.peek();" subformer="getSub(opFormer);" switch(ops[subformer][subnew]){="" case="" ':
                        goOn = true;
                        TreeNode n1 = nodesStack.pop();
                        TreeNode n0 = nodesStack.pop();
                        double rs = doOperate(n0.value, n1.value, opFormer);
                        nodesStack.push(new TreeNode(rs, opFormer, n0, n1));
                        opsStack.pop();
                        break;
                    default:
                        throw new Exception("没有匹配的操作符:" + op);
                    }
                }
                i++;
            }
        }
        return nodesStack.pop();
        
    }
    privatedoubledoOperate(double n0, double n1, char op) throws Exception{
        switch(op){
        case '+': return n0 + n1;
        case '-': return n0 - n1;
        case '*': return n0 * n1;
        case '/': return n0 / n1;
        default: throw new Exception("非法操作符:" + op);
        }
    }
    privateintgetSub(char c){
        switch(c){
            case '+': return 0;
            case '-': return 1;
            case '*': return 2;
            case '/': return 3;
            case '(': return 4;
            case ')': return 5;
            case '#': return 6;
            default : return -1;
        }
    }
}
class TreeNode{
    public double value;
    public char op = 'E';
    public TreeNode lft;
    public TreeNode rt;
    public TreeNode(double value){
        this.value = value;
    }
    public TreeNode(double value, char op, TreeNode lft, TreeNode rt){
        this.value = value;
        this.op = op;
        this.lft = lft;
        this.rt = rt;
    }
    StringBuilder buf = new StringBuilder();
    public String toString(){
        out(this);
        return buf.toString();
    }
    privatevoidout(TreeNode node){
        if(node == null) return;
        out(node.lft);
        if(node.op != 'E')
            buf.append(node.op);
        else
            buf.append(node.value);
        out(node.rt);
    }
}

:http://www.linuxidc.com/Linux/2017-12/149689.htm