使用运算符优先级计算进程

时间:2011-07-20 11:00:43

标签: java

我正在尝试计算一个算术表达式,它以字符串形式输入(例如,( 5+4*5-1/8 ),它将给出结果3)。我输入一个表达式并将其转换为数组。第一;结果将从第一个元素开始,它将在循环中更改。但问题是运营商优先。如何在循环中使用运算符presedence?这是我的代码:

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]=='/' || value[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]!= )

        }


    }*/

}

2 个答案:

答案 0 :(得分:1)

这不是你怎么做的。您需要在评估表达式之前解析表达式。我建议你阅读Shunting-yard algorithm

答案 1 :(得分:0)

继续我的评论...如果你正在处理一个简单的表达式,你只能有数字和符号+ - / *那么你可以采用一种简单的方法:

  1. 首先用最低优先级运算符(+ - )分割你的表达式,记住符号。
  2. 计算每一件 - 因为现在优先级并不重要,因为一切都是同一级别
  3. 考虑到步骤1中作品的符号,对这些计算的作品求和。
  4. 在你的例子中,你最终会得到这样的东西:

    1. (拆分+ - )三件:(1)5; (2)4 * 5带符号+; (3)1/8带标志 -
    2. 计算三件中的每一件:(1)5; (2)20; (3)0.125
    3. 将这三个部分用各自的符号相加:5 + 20-0.125 = 24.875