首页 > 解决方案 > 如何使用中缀后缀表示法在计算器中添加三角函数?爪哇

问题描述

我想做的是一个计算器,他们让我创建所有代码,我只能使用堆栈或队列或数学之类的库,但我不能使用已经解决练习的库作为脚本,那就是为什么我选择使用中缀 PostFix 表示法,但最后我不知道如何添加三角函数

代码:

import java.util.Stack;
public class InfixPostfixEvaluator
{
    private static final String operators = "-+/*^";
    private static final String operands = "0123456789";
    public static void main(String[] args)
    {
        InfixPostfixEvaluator infix = new InfixPostfixEvaluator();
        String a = "((1 + 2)^2 / (2 * 3)) + (2 ^ (1 / 2))";
        System.out.println("Result: " + infix.evalInfix(a));
    }
    public double evalInfix(String infix) 
    {
        return evaluatePostfix(convert2Postfix(infix));
    }
    public String convert2Postfix(String infixExpr)
    {
        char[] chars = infixExpr.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        StringBuilder out = new StringBuilder(infixExpr.length());
        for (char c : chars) 
        {
            if (isOperator(c)) 
            {
                while (!stack.isEmpty() && stack.peek() != '(') 
                    if (operatorGreaterOrEqual(stack.peek(), c)) 
                        out.append(stack.pop());
                    else 
                        break;
                stack.push(c);
            }
            else if (c == '(') 
                stack.push(c);
            else if (c == ')') 
            {
                while (!stack.isEmpty() && stack.peek() != '(') 
                    out.append(stack.pop());
                if (!stack.isEmpty())
                    stack.pop();
            }
            else if (isOperand(c)) 
                out.append(c);
        }
        while (!stack.empty()) 
            out.append(stack.pop());
        return out.toString();
    }

    public double evaluatePostfix(String postfixExpr) 
    {
        char[] chars = postfixExpr.toCharArray();
        Stack<Double> stack = new Stack<Double>();
        for (char c : chars) 
        {
            if (isOperand(c)) 
                stack.push((double)(c - '0')); // convert char to int val
            else if (isOperator(c)) 
            {
                double op1 = stack.pop();
                double op2 = stack.pop();
                double result;
                switch (c)
                {
                    case '*':
                        result = op1 * op2;
                        stack.push(result);
                        break;
                    case '/':
                        result = op2 / op1;
                        stack.push(result);
                        break;
                    case '+':
                        result = op1 + op2;
                        stack.push(result);
                        break;
                    case '-':
                        result = op2 - op1;
                        stack.push(result);
                        break;
                    case '^':
                        result = Math.pow(op2, op1);
                        stack.push(result);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private double getPrecedence(char operator) 
    {
        double ret = 0;
        if (operator == '-' || operator == '+')
            ret = 1;
        else if (operator == '*' || operator == '/') 
            ret = 2;
        else if (operator == '^')
            ret = 3;
        return ret;
    }
    private boolean operatorGreaterOrEqual(char op1, char op2) 
    {
        return getPrecedence(op1) >= getPrecedence(op2);
    }
    private boolean isOperator(char val) 
    {
        return operators.indexOf(val) >= 0;
    }
    private boolean isOperand(char val) 
    {
        return operands.indexOf(val) >= 0;
    }
}

我想到给 cos、sen、tan 赋予特殊字符为 cos -> #、sin -> $ 和 tan -> %,然后我想到要改变这一点,我在其中编写了我将使用的运算符:

private static final String operators = "-+/*^#$%";

这是我或多或少想要评估的字符串,考虑到它必须用任何字符串解析:

String a = "((1 + 2)^2 / (2 * 3)) + #(2 ^ (1 / 2))";

这与写作相同:

String a = "((1 + 2)^2 / (2 * 3)) + cos(2 ^ (1 / 2))";

最后在哪里进行了操作,我添加了一个案例,假设 # 是 cos,我希望它解决它,但事实是我不知道如何:

public double evaluatePostfix(String postfixExpr) 
{
    char[] chars = postfixExpr.toCharArray();
    Stack<Double> stack = new Stack<Double>();
    for (char c : chars) 
    {
        if (isOperand(c)) 
            stack.push((double)(c - '0')); // convert char to int val
        else if (isOperator(c)) 
        {
            double op1 = stack.pop();
            double op2 = stack.pop();
            double result;
            switch (c)
            {
                case '*':
                    result = op1 * op2;
                    stack.push(result);
                    break;
                case '/':
                    result = op2 / op1;
                    stack.push(result);
                    break;
                case '+':
                    result = op1 + op2;
                    stack.push(result);
                    break;
                case '-':
                    result = op2 - op1;
                    stack.push(result);
                    break;
                case '^':
                    result = Math.pow(op2, op1);
                    stack.push(result);
                    break;
                case '#':
                    result = Math.cos(op1);
                    stack.push(result);
                    break;
            }
        }
    }
    return stack.pop();
}

但它根本不像那样工作。还告诉他们,如果有办法直接以我选择的特殊字符的形式编写 cos(),那对我的帮助会更大。有什么需要的请告诉我,非常感谢。

标签: java

解决方案


推荐阅读