开发者

calculate a process with operator precedence

I am trying to calculate an arithmetic expression, which is entered as a string (for example, ( 5+4*5-1/8 ), which will give the result 3). I enter an expression and convert it into an array. First; the result will start with the first element and it will change in the loop. But the problem is operator precedence. How can I use the operator presedence in a loop? Here is my code:

import java.util.Scanner;


public class HesapMakinesi {

    private char value[];

    private int count;

    private Scanner str = new Scanner(System.in);

    private String process;

    HesapMakinesi() {

        System.out.print("Enter the process ");

        process = str.next();

        //System.out.println(islem);

        Initializer(process);

    }

    private void Initializer(String process) {

        count = process.toCharArray().length;

        value = new char [count];

        int i;

        System.arraycopy(process.toCharArray(), 0, value, 0, count);

        //System.out.println(value);

        if(value[0]=='-' || value[0]=='+' || value[0]=='/' || value[0]=='*' ||  // A process cannot start with an operator
                value[count-1]=='-' || value[count-1]=='+' || value[count-1]=='/' || val开发者_如何学Cue[count-1]=='*') {

            System.out.println("You have entered a wrong process.Please enter again!!!");

            System.out.print("Enter the process: ");

            process = str.next();

            Initializer(process);

        }



        for(i=0; i<count; i++) { // A process cannot include a character except operators

            if( value[i]!='+' && value[i]!='-' && value[i]!='*' && value[i]!='/' && value[i]!='(' && value[i]!=')' && !Character.isDigit(value[i]) ) {

                System.out.println("You have entered a wrong process.Please enter again!!!");

                System.out.print("Enter the process: ");

                process = str.next();

                Initializer(process);

            }


        }


        for(i=0; i<count-1; i++) { // A process cannot have operators sequantially

            if( !Character.isDigit(value[i]) && !Character.isDigit(value[i+1]) ) {

                if( (value[i] == '+' && value[i+1] == '+' ) || (value[i] == '+' && value[i+1] == '-' ) || (value[i] == '+' && value[i+1] == '*' ) || 
                        (value[i] == '+' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }

                else if( (value[i] == '-' && value[i+1] == '+' ) || (value[i] == '-' && value[i+1] == '-' ) || (value[i] == '-' && value[i+1] == '*' ) || 
                        (value[i] == '-' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }

                else if( (value[i] == '*' && value[i+1] == '+' ) || (value[i] == '*' && value[i+1] == '-' ) || (value[i] == '*' && value[i+1] == '*' ) || 
                        (value[i] == '*' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }

                else if( (value[i] == '/' && value[i+1] == '+' ) || (value[i] == '/' && value[i+1] == '-' ) || (value[i] == '/' && value[i+1] == '*' ) || 
                        (value[i] == '/' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }


            }

        }

        //sCount();

    }

    /*private void Count(){

        double result,temp;

        int i;

        for(i=0; i<count; i++) {

            if( value[i]!= )

        }


    }*/

}


That's not how you do it. You need to parse the expression before evaluating it. I suggest you to read the Shunting-yard algorithm.


Following on from my comment... If you're dealing with a simple expression where you can only have numbers and signs +-/* then you can have a simple approach:

  1. split your expression by lowest precedence operators first (+-) remembering the signs.
  2. Compute each piece - as now the precedence isn't important, since everything is of the same level
  3. sum those computed pieces taking into account the sign of the piece from step 1.

In your example, you'll end up with something like this:

  1. (Split by +-) three pieces: (1) 5; (2) 4*5 with sign +; (3) 1/8 with sign -
  2. Compute each of the three pieces: (1) 5; (2) 20; (3) 0.125
  3. Sum the three pieces with their respective signs: 5+20-0.125 = 24.875
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜