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

📄 parser.java

📁 Java编写的表达式计算器, 即可以像我们书写表达式那样直接输入计算表达式, 程序自动进行运算, 支持加减乘除幂运算以及判断表达式如A?B C, 程序包含完整的Document和测试运行环境
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                TypeMismatchedException
        {
            boolean already = false;
            Token r = stack.remove(stack.size()-1);
            if(!r.toString().equals(")"))throw new MissingRightParenthesisException();
            int i,j,l = stack.size()-1;
            for(i = l;i > -1;i--)
            {
                if(stack.get(i).toString().equals("(") && !already)
                {    
                        already = true;
                        stack.remove(i);
                        if(i > 0)
                        {
                            Token temp = stack.get(i-1);
                            if(temp.getType().equals("Function"))
                            {
                                j = i;
                                if(temp.toString().equalsIgnoreCase("max"))
                                {   
                                    double maxn;
                                    if((stack.size()-j) == 1) throw new MissingOperandException();
                                    if(j >= stack.size()) throw new MissingOperandException();
                                    Token ttt = stack.remove(j);
                                    if(!ttt.getType().equals("Decimal")){
                                        if(ttt.getType().equals("Operator"))throw new MissingOperandException();
                                        else throw new TypeMismatchedException();
                                    }
                                    DecimalToken tt = (DecimalToken) ttt;
                                    maxn = tt.getResult();
                                    for(j = i;j < l-1;j += 2)
                                    {
                                        Token td = stack.remove(i);
                                        if(!td.toString().equals(",")) throw new FunctionCallException();
                                        ttt = stack.remove(i);
                                        if(!ttt.getType().equals("Decimal")){
                                        if(ttt.getType().equals("Operator"))throw new MissingOperandException();
                                        else throw new TypeMismatchedException();
                                    }
                                        tt = (DecimalToken) ttt;
                                        if(maxn < tt.getResult())
                                            maxn = tt.getResult();
                                    }
                                    stack.remove(i-1);
                                    return maxn;
                                }
                                else if(temp.toString().equalsIgnoreCase("min"))
                                {   
                                    double minn;
                                    if((stack.size()-j) == 1) throw new MissingOperandException();
                                    if(j >= stack.size()) throw new MissingOperandException();
                                    Token ttt = stack.remove(j);
                                    if(!ttt.getType().equals("Decimal")){
                                        if(ttt.getType().equals("Operator"))throw new MissingOperandException();
                                        else throw new TypeMismatchedException();
                                    }
                                    DecimalToken tt = (DecimalToken) ttt;
                                    minn = tt.getResult();
                                    for(j = i;j < l-1;j += 2)
                                    {
                                        Token td = stack.remove(i);
                                        if(!td.toString().equals(",")) throw new FunctionCallException();
                                        ttt = stack.remove(i);
                                        if(!ttt.getType().equals("Decimal")){
                                            if(ttt.getType().equals("Operator"))throw new MissingOperandException();
                                            else throw new TypeMismatchedException();
                                        }
                                        tt = (DecimalToken) ttt;
                                        if(minn > tt.getResult())
                                            minn = tt.getResult();
                                    }
                                    stack.remove(i-1);
                                    return minn;
                                }
                                else if(temp.toString().equalsIgnoreCase("sin"))
                                {   
                                    if((stack.size()-i) != 1) throw new FunctionCallException();
                                    if(i >= stack.size()) throw new MissingOperandException();
                                    Token ttt = stack.remove(i);
                                    if(!ttt.getType().equals("Decimal")){
                                        if(ttt.getType().equals("Operator"))throw new MissingOperandException();
                                        else throw new TypeMismatchedException();
                                    }
                                    DecimalToken tt = (DecimalToken) ttt;
                                    stack.remove(i-1);
                                    return Math.sin(tt.getResult());
                                }
                                else if(temp.toString().equalsIgnoreCase("cos"))
                                {   
                                    if((stack.size()-i) != 1) throw new FunctionCallException();
                                    if(i >= stack.size()) throw new MissingOperandException();
                                    Token ttt = stack.remove(i);
                                    if(!ttt.getType().equals("Decimal")){
                                        if(ttt.getType().equals("Operator"))throw new MissingOperandException();
                                        else throw new TypeMismatchedException();
                                    }
                                    DecimalToken tt = (DecimalToken) ttt;
                                    stack.remove(i-1);
                                    return Math.cos(tt.getResult());
                                }
                            }
                        }
                }
            }
            if(i == -1 && !already) throw new MissingLeftParenthesisException();
            return 2;
        }
        /**
         * 规约负号操作
         * @return 运算结果
         * @throws exceptions.MissingOperandException 缺少操作量异常
         * @throws exceptions.MissingOperatorException 缺少操作符异常
         * @throws exceptions.TypeMismatchedException 类型不匹配异常
         */
        double neg() throws 
                MissingOperandException,
                MissingOperatorException,
                TypeMismatchedException
        {
                double r = 0;
		Token test = stack.remove(stack.size()-1);
		if(test.type.equals("Decimal"))
		{	
			DecimalToken d = (DecimalToken)test;	
			r = 0 - d.getResult();
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
                if(!stack.remove(stack.size()-1).getType().equals("Operator"))throw new MissingOperatorException();
                return r;
        }
        /**
         * 规约非关系操作
         * @return 运算结果
         * @throws exceptions.MissingOperandException 缺少操作量异常
         * @throws exceptions.MissingOperatorException 缺少操作符异常
         * @throws exceptions.TypeMismatchedException 类型不匹配异常
         */
        String not() throws
                MissingOperatorException,
                MissingOperandException,
                TypeMismatchedException
        {
                String r = "";
		Token test = stack.remove(stack.size()-1);
		if(test.type.equals("Boolean"))
		{	
			BooleanToken d = (BooleanToken)test;	
			if(d.getValue())
                            r = "false";
                        else 
                            r = "true";
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
                if(!stack.remove(stack.size()-1).getType().equals("Operator"))throw new MissingOperatorException();
                return r;
        }
        /**
         * 规约与或非运算操作
         * @return 运算结果
         * @throws exceptions.MissingOperandException 缺少操作量异常
         * @throws exceptions.MissingOperatorException 缺少操作符异常
         * @throws exceptions.TypeMismatchedException 类型不匹配异常
         */
        String realation() throws
                MissingOperatorException,
                MissingOperandException,
                TypeMismatchedException
        {
                BooleanToken r,l;
		Token test = stack.remove(stack.size()-1);
		if(test.type.equals("Boolean"))
		{	
			r = (BooleanToken)test;	
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
                Token temp = stack.remove(stack.size()-1);
                if(!temp.getType().equals("Operator"))throw new MissingOperatorException();
                test = stack.remove(stack.size()-1);
		if(test.type.equals("Boolean"))
		{	
			l = (BooleanToken)test;
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
                if(temp.getToken().equals("|"))
                {
                    if(r.getValue() || l.getValue()) return "true";
                    else return "false";
                }                    
                else if(temp.getToken().equals("&"))
                {
                    if(r.getValue() && l.getValue()) return "true";
                    else return "false";
                }     
                return "";
        }
        /**
         * 规约关系运算操作
         * @return 运算结果
         * @throws exceptions.DividedByZeroException 除数为0异常
         * @throws exceptions.MissingOperandException 缺少操作量异常
         * @throws exceptions.MissingOperatorException 缺少操作符异常
         * @throws exceptions.TypeMismatchedException 类型不匹配异常
         */
        String cmp() throws
                MissingOperandException,
                DividedByZeroException,
                MissingOperatorException,
                TypeMismatchedException
        {
                double r = 0,l = 0;
		Token test = stack.remove(stack.size()-1);
		if(test.type.equals("Decimal"))
		{	
			DecimalToken right = (DecimalToken)test;	
			r = right.getResult();
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
		Token temp = stack.remove(stack.size()-1);
		test = stack.remove(stack.size()-1);
		if(test.type.equals("Decimal"))
		{
			DecimalToken left = (DecimalToken)test;
			l = left.getResult();
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
                if(!temp.getType().equals("Operator"))throw new MissingOperatorException();
                if(temp.toString().equals(">"))
                {
                    if(l>r) return "true";
                    else return "false";
                }
                else if(temp.toString().equals("<"))
                {
                    if(l<r) return "true";
                    else return "false";
                }
                else if(temp.toString().equals(">="))
                {
                    if(l>=r) return "true";
                    else return "false";
                }
                else if(temp.toString().equals("<="))
                {
                    if(l<=r) return "true";
                    else return "false";
                }
                else if(temp.toString().equals("<>"))
                {
                    if(l!=r) return "true";
                    else return "false";
                }
                return "";
        }
        /**
         * 规约三元运算操作
         * @return 运算结果
         * @throws exceptions.TrinaryOperationException 三元运算异常
         * @throws exceptions.MissingOperandException 缺少操作量异常
         * @throws exceptions.MissingOperatorException 缺少操作符异常
         * @throws exceptions.TypeMismatchedException 类型不匹配异常
         */
        double trinary() throws
                TrinaryOperationException,
                MissingOperandException,
                TypeMismatchedException,
                MissingOperatorException
        {
                double r = 0,l = 0;
		Token test = stack.remove(stack.size()-1);
		if(test.type.equals("Decimal"))
		{	
			DecimalToken right = (DecimalToken)test;	
			r = right.getResult();
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
		Token temp = stack.remove(stack.size()-1);
                if(!temp.toString().equals(":"))throw new TrinaryOperationException();
		test = stack.remove(stack.size()-1);
		if(test.type.equals("Decimal"))
		{
			DecimalToken left = (DecimalToken)test;
			l = left.getResult();
		}else if(test.getType().equals("Operator"))throw new MissingOperandException();
                else throw new TypeMismatchedException();
                temp = stack.remove(stack.size()-1);
                if(!temp.toString().equals("?"))throw new TrinaryOperationException();
                test = stack.remove(stack.size()-1);
                if(!test.getType().equals("Boolean"))throw new TypeMismatchedException();
                BooleanToken boole = (BooleanToken)test;
                if(boole.getValue())return l;
                else return r;
        }
        
}

⌨️ 快捷键说明

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