-3

我想在不使用 API的情况下评估表格-4-12-2*12-3-4*5中给出的表达式,因为我是初学者并且想要掌握逻辑。String

下面给出的是我对这个问题的不成功尝试,如果您愿意,请忽略并建议适当的逻辑。当然也欢迎您的代码:-)

public class SolveExpression3 {

static String testcase1 = "-4-12-2*12-3-4*5";


public static void main(String args[]){
    SolveExpression3 testInstance= new SolveExpression3();
    int result = testInstance.solve(testcase1);
    System.out.println("Result is : "+result);
}

public int solve(String str){

    int sum = 1;
    int num1 = 0;
    int num2 = 0;
    String num = "";        
    int len = str.length();
    System.out.println(str);
    for (int i = len-1 ; i >= 0; i--)
    {
        char ch = str.charAt(i);            
        if(ch == '*')
        {
            String s = "";
            num1 = num2 = 0;
            //to get the number on left of *
            for (int j = i; j >= 0; j--)
            {
                char c = str.charAt(j);                 
                if(c == '+' || c == '-' || j == 1)
                {
                    num1 = stringToInt(s);
                    s = "";
                    break;
                }
                else
                {
                    s = c + s;
                }
            }
            //to get the number on right of *
            for (int j = i; j <= len; j++)
            {
                char c = str.charAt(j);                 
                if(c == '+' || c == '-' || j == len-1)
                {
                    num2 = stringToInt(s);
                    s = "";
                    break;
                }
                else
                {
                    s = c + s;
                }
            }
            sum = sum + num1*num2;

        }
        else
        {
            num = ch + num;             
        }
    }
    len = str.length();
    for (int i = len-1; i >= 0; i--)
    {
        char ch = str.charAt(i);
        if(ch==' ')
        {}
        else if(ch=='+')
        {
            sum = sum + stringToInt(num);               
            num = "";
        }
        else if(ch=='-')
        {
            sum = sum - stringToInt(num);               
            num = "";
        }
        else
        {
            num = ch + num;             
        }
    }
    return sum;
}

public int stringToInt(String str)
{
    int number=0;
    for(int i = 0; i < str.length(); i++)
    {
        int num = str.charAt(i) - 48;
        number = number*10+num;
    }
    return number;
}

}
4

3 回答 3

1
        found=true;
        static String testcase1 = "-4-12-2*12-3-4*5";
        Pattern SEGMENT_PATTERN = Pattern.compile("(\\d+(\\.\\d+)?|\\D+)");
        /*\\d-means digit,
        \\.-point,
        +-one or more times,
        ?-optional and 
        \\D-non digit ch*/
        Matcher matcher = SEGMENT_PATTERN.matcher(testcase1);
        while (found) {
                    boolean Found = matcher.find();
                    String segment = matcher.group();//representing a number or an operator

                        if (Character.isDigit(segment.toCharArray()[0])) {
                            //is digit
                        }
                        else {
                            //is operator

                        }
                    }

这是一个使用模式来确定您是否有数字或运算符的解决方案,您只需根据您的情况对其进行一些调整以计算结果。

You can add all the matches found to an array list than traverse it and test the operators and computer the result.

它也适用于浮点数,例如:“它匹配 5.10”。

于 2013-08-22T22:33:10.340 回答
0

尝试实现递归下降解析器,描述如何实现计算器的教程(在 Python 中,但相同的概念适用于 java)可以在这里找到http://blog.erezsh.com/how-to-write-a-计算器-in-70-python-lines-by-writing-a-recursive-descent-parser/

于 2013-08-22T22:56:24.933 回答
0

I would suggest a different logic for your purpose.

Usually the logic behind programs algorithms is not different from the logic that you will apply if you have to do the task by hand.

For an expression like your example you would usually do:

  1. Find all the *
  2. For each * compute the result of the operation
  3. Repeat steps 1 and 2 for + and -
于 2013-08-22T22:36:05.357 回答