java版简易计算器代码易懂(原创)


//参照严的数据结构教材,逻辑简单
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class Calculator {
    private static char precede[][] = new char[256][256];
    private static Set<Character> testifiedOperator = new HashSet<Character>();
    static
    {
    	testifiedOperator.add('+');
    	testifiedOperator.add('-');
    	testifiedOperator.add('*');
    	testifiedOperator.add('/');
    	testifiedOperator.add('(');
    	testifiedOperator.add(')');
    	testifiedOperator.add('#');
    	
    	
    	precede['+']['+'] = '>';
    	precede['+']['-'] = '>';
    	precede['+']['*'] = '<';
    	precede['+']['/'] = '<';
    	precede['+']['('] = '<';
    	precede['+'][')'] = '>';
    	precede['+']['#'] = '>';
    	
    	precede['-']['+'] = '>';
    	precede['-']['-'] = '>';
    	precede['-']['*'] = '<';
    	precede['-']['/'] = '<';
    	precede['-']['('] = '<';
    	precede['-'][')'] = '>';
    	precede['-']['#'] = '>';
    	
    	precede['*']['+'] = '>';
    	precede['*']['-'] = '>';
    	precede['*']['*'] = '>';
    	precede['*']['/'] = '>';
    	precede['*']['('] = '<';
    	precede['*'][')'] = '>';
        precede['*']['#'] = '>';
        
    	precede['/']['+'] = '>';
    	precede['/']['-'] = '>';
    	precede['/']['*'] = '>';
    	precede['/']['/'] = '>';
    	precede['/']['('] = '<';
    	precede['/'][')'] = '>';
        precede['/']['#'] = '>';
        
    	precede['(']['+'] = '<';
    	precede['(']['-'] = '<';
    	precede['(']['*'] = '<';
    	precede['(']['/'] = '<';
    	precede['(']['('] = '<';
    	precede['('][')'] = '=';
    	
     	precede['#']['+'] = '<';
    	precede['#']['-'] = '<';
    	precede['#']['*'] = '<';
    	precede['#']['/'] = '<';
    	precede['#']['('] = '<';
    	precede['#']['#'] = '=';
 

    }
    
     //进行操作运算符
    private static double operate(double a,double b,char op)
    {
    	    double result = 0.0;
            switch (op) {
			case '*':
				result = a * b;
				break;
			case '/':
				result = a * 1.0 / b;
				break;
			case '+':
				result = a + b;
				break;
			case '-':
				result = a -b;
				break;
			default:
				break;
			}
            return result;
    }
    public  static void execute(String expreesion)
    {
    	String ex = expreesion + "#";
    	int index = 0;
    	Stack<Double> OPND = new Stack<Double>();
    	Stack<Character> OPTR = new Stack<Character>();
    	OPTR.push('#');
    	
    	while(index < ex.length())
    	{
    		if(ex.charAt(index) >= '0' && ex.charAt(index) <= '9' )
    		{
    			int  num = 0;
    			while(index < ex.length()&&ex.charAt(index) >= '0' && ex.charAt(index) <= '9')
    			{
    				num = num * 10 + (ex.charAt(index) - '0');
    				index++;
    			}
    			OPND.push(num * 1.0);
    			
    		}else
    		{
    			if(!testifiedOperator.contains(ex.charAt(index)))
    			{
    				//
    				throw new NumberFormatException("包含非法字符");
    			}
    			switch (precede[OPTR.peek()][ex.charAt(index)]) {
				case '<':
				    OPTR.push(ex.charAt(index));
				    index++;
					break;
				case '=':
				    OPTR.pop();
				    index++;
				    break;
				case '>':
					if(OPND.size() < 2)
						throw new NumberFormatException("表达式非法");
					double numb = OPND.pop();
					double numa = OPND.pop();
					double result = operate(numa, numb, OPTR.pop());
					OPND.push(result);
					break;
				default:
					throw new NumberFormatException("表达式非法");
					
				}
    			
    		}
    	}
    	
    	System.out.println(OPND.pop());
    }
	public static void main(String[] args) {
		String expreesion = "32*32/(2+6)-129";
		Calculator.execute(expreesion);
         
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值