0

I want to write a program that that take a infix string and change it to postfix then evaluate postfix and print the answer ; for +-*/^ it's easy just need precedence of operators but I don't know what should I do for sin cos log and other math function.

private static String infixToPostfix(String infix) {

    String[] exp = infix.split("");
    Stack<String> stack = new Stack<>();
    String result = "";
    for (int i = 0; i < exp.length; i++){

        if (exp[i].equals("(")) {
                stack.push(exp[i]);
        }
        else if (isOperator(exp[i]))
        {
            while (!stack.isEmpty() && precedence(exp[i]) <= precedence(stack.getTop())){
                result += stack.pop() + " ";
            }
            stack.push(exp[i]);
        }
        else if (exp[i].equals(")"))
        {
            while (!stack.isEmpty() && !stack.getTop().equals("(")){
                result += stack.pop() + " ";
            }
            stack.pop();
        }
        else if (Character.isLetterOrDigit(infix.charAt(i)) || exp[i].equals(".")){
            boolean haveDot = exp[i].equals(".");
            String temp = haveDot ? "0." : exp[i];
            while ((i + 1) < exp.length && (Character.isLetterOrDigit(infix.charAt(i + 1)) || exp[i + 1].equals("."))){

                temp += exp[i + 1];
                i++;
            }
            result += temp + " ";
        }
    }
    while (!stack.isEmpty()){
        result += stack.pop() + " ";
    }
    return result;
}

It is working correctly!

But this

private static Double postFixEvaluator(String[] postFix) {
    Stack<Double> operands = new Stack<>();
    double value = 0.0;
    for (int str = 0; str < postFix.length; str++) {
        if (postFix[str].trim().equals("")) {
            continue;
        }
        switch (postFix[str]) {

            case "+":
            case "-":
            case "*":
            case "/":
            case "^":
                Double right = operands.pop();
                Double left = operands.pop();
                long intValue = 0;
                switch (postFix[str]) {
                    case "+":
                        value = left + right;
                        break;
                    case "-":
                        value = left - right;
                        break;
                    case "*":
                        value = left * right;
                        break;
                    case "/":
                        value = left / right;
                        break;
                    case "^":
                        value = Math.pow(left, right);
                        break;
                    default:
                        break;
                }
            case "sin":
            case "cos":
            case "tan":
            case "cot":
                if (Character.isLetterOrDigit(Arrays.toString(postFix).charAt(str + 2))) {
                    str++;
                    break;
                }
                else{
                    Double oper = operands.pop();
                    switch (postFix[str]) {
                        case "sin":
                            value = Math.sin(oper);
                            break;
                        case "cos":
                            value = Math.cos(oper);
                            break;
                        case "tan":
                            value = Math.tan(oper);
                            break;
                        case "cot":
                            value = 1 / Math.tan(oper);
                            break;
                    }
                }

                operands.push(value);
                break;
            default:
                operands.push(Double.parseDouble(postFix[str]));
                break;
        }
    }
    return operands.pop();
}

is not working correctly.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aybab
  • 1
  • 2
  • Check java.lang.Match class if thats what you are looking for...... 1. Java.lang.Math.sin() Method : is an inbuilt method which returns the sine of the value passed as an argument. 2. Java.lang.Math.cos() : is an inbuilt method which returns the cosine of the value passed as an argument. 3. Java.lang.Math.tan() : is an inbuilt method which returns the tangent of the value passed as an argument. – www.hybriscx.com Oct 30 '19 at 08:50
  • You have to treat `sin`, `cos`, etc., as unary operators. You need to find an infix to postfix converter that can handle unary operators. – user207421 Oct 30 '19 at 09:15
  • What problems are you having? Are you having difficulty parsing the expression like `sin(30)`? Or are you having trouble converting that to postfix? As mentioned in the previous comment, you want to modify your converter program so that `sin` (and others) are unary operators. It's not fundamentally different from how you would handle negative numbers, as in `7*(-5)`. Basically, you define new operators and modify your implementation to handle them. See https://stackoverflow.com/a/20248984/56778 – Jim Mischel Oct 30 '19 at 21:42
  • If you edit your question to include the code for your infix-to-postfix converter, we can probably show you the modifications you need to make. – Jim Mischel Oct 30 '19 at 21:44
  • Have you single-stepped the program in your debugger to see where it's going awry? If you don't know how to use the debugger, now is the perfect time to learn. – Jim Mischel Nov 02 '19 at 20:05

0 Answers0