0

我正在处理字符串中的逻辑表达式。到目前为止,我已经研究了以下方法。

public static String modify(String expression)
    {
        String temp = expression;
        String validated = "";

        for(int idx=0; idx<temp.length(); idx++)
        {
            if(idx!=temp.length()-1)
            {
                if((Character.isAlphabetic(temp.charAt(idx))) && (Character.isAlphabetic(temp.charAt(idx+1))))
                {
                    validated+=temp.substring(idx,idx+1);
                    validated+="*";
                }
                else
                    validated+=temp.substring(idx,idx+1);
            }
            else
                validated+=temp.substring(idx);
        }

        return validated;
    }

以下是假定输入/输出的示例输入:AB+BC+ABC/输出:(A*B)+(B*C)+(A*B*C) 输入:(A+B)+ABC/输出:(A+B)+(A*B*C) 输入:(A+B)*(B+C)*(AB)/输出:(A+B)*(B+C)*(A*B)

4

3 回答 3

0

一种方法是简单地使用布尔信号量跟踪括号

public static String modify(String expression)
    {
        String temp = expression;
        StringBuilder validated = new StringBuilder();
        boolean inBrackets=false;

        for(int idx=0; idx<temp.length()-1; idx++)
        {
            if((Character.isLetter(temp.charAt(idx))) && (Character.isLetter(temp.charAt(idx+1))))
            {
                if(!inBrackets){
                    inBrackets = true;
                    validated.append("(");
                }
                validated.append(temp.substring(idx,idx+1));
                validated.append("*");
            }
            else{
                validated.append(temp.substring(idx,idx+1));
                if(inBrackets){
                    validated.append(")");
                    inBrackets=false;
                }
            }
        }
        validated.append(temp.substring(temp.length()-1));
        if(inBrackets){
            validated.append(")");
            inBrackets=false;
        }
        return validated.toString();
    }

如果您正在寻求线程安全的解决方案,也不要使用字符串连接,而是使用 StringBuilder 或其前身 StringBuffer。

于 2013-11-06T15:43:10.723 回答
0

这是我要做的,使用 StringBuilder 和拆分:

public static String modify(String expression)
{        
    StringBuilder finalString = new StringBuilder();

    String[] subExpressions = expression.split("\\+");
    List<String> formattedSubExpressions = new ArrayList<String>();

    for (String subExpression : subExpressions) {
        if (subExpression.length() > 1) {
            StringBuilder formattedSubExpression = new StringBuilder();
            formattedSubExpression.append("(");
            for (int i=0; i<subExpression.length(); i++) {
                formattedSubExpression.append(subExpression.charAt(i));
                if (i != subExpression.length() -1 ) {
                    formattedSubExpression.append("*");
                }
            }
            formattedSubExpression.append(")");
            formattedSubExpressions.add(formattedSubExpression.toString());
        } else {
            formattedSubExpressions.add(subExpression);
        }
    }

    for (String subExpression : formattedSubExpressions) {
        finalString.append(subExpression);
        finalString.append("+");
    }

    if (finalString.charAt(finalString.length() - 1) == '+') {
        finalString.deleteCharAt(finalString.length() - 1);
    }

    return finalString.toString();
}

它提供了以下示例输入/输出:

AB+CD: (A*B)+(C*D)
AB+CD+EF: (A*B)+(C*D)+(E*F)
AB+CD+EFGH: (A*B)+(C*D)+(E*F*G*H)
于 2013-11-06T15:20:03.803 回答
0

我的这个答案基于这样一个想法,即您想要做的是在括号之间对重复的字母字符进行分组,并在它们之间放置一个星号,而不管组之间执行的操作(加法、减法、除法等)如何。

private static final Pattern p = Pattern.compile("[a-zA-Z]{2,}");

public String parse(String s){
    if(s == null || "".equals(s)) {
        return s;
    }
    char[] chars = s.toCharArray();
    StringBuilder sb = new StringBuilder(100);
    Matcher m = p.matcher(s);
    int i = 0;
    while(i<chars.length && m.find()){
        int startIdx = m.start();
        int endIdx = m.end();
        // Need to get the leading part of the string before this matching region
        while(i < startIdx){
            sb.append(chars[i]);
            i++;
        }
        sb.append('('); // Start getting the match region
        while(i < endIdx){
            sb.append(chars[i]);
            if(i < endIdx - 1){
                sb.append('*');
            }
            i++;
        }

        sb.append(')'); // end the match region
    }
    // If there is a region beyond the last match, append it
    if(i < chars.length -1){
        for(; i < chars.length; i++){
            sb.append(chars[i]);
        }
    }
    return sb.toString();

}
于 2013-11-06T15:57:21.270 回答