确定Java字符数组列表中的变量

确定Java字符数组列表中的变量,java,arraylist,boolean-logic,Java,Arraylist,Boolean Logic,嗨,我有一个字符数组列表,我把我的逻辑表达式存储在这里。我的代码计算列表中的变量,我的问题是当我有相同的变量,但有一个not符号和一个括号。例如:xy+zy'变量的数量应该是4,y和y'是不同的 public void simplify(String strexp){ int length = strexp.length(); //get the size and variables used in expression List<Character> use

嗨,我有一个字符数组列表,我把我的逻辑表达式存储在这里。我的代码计算列表中的变量,我的问题是当我有相同的变量,但有一个not符号和一个括号。例如:xy+zy'变量的数量应该是4,y和y'是不同的

public void simplify(String strexp){

 int length = strexp.length();

     //get the size and variables used in expression
    List<Character> usedVariables = new ArrayList<Character>();
    for (int i = 0; i < length; i++) {
        char c = strexp.charAt(i);
        if (Character.isLetter(c) && !usedVariables.contains(c)&&usedVariables.contains('\'')) {
            usedVariables.add(c);
        }
    }
}
public void simplify(字符串strexp){
int length=strexp.length();
//获取表达式中使用的大小和变量
List usedVariables=new ArrayList();
for(int i=0;i< /代码> 如果你的变量可能比一个字符长,你应该考虑一个字符串数组,而不是字符列表。

也许你想重新设计你的代码:
Matcher m = Pattern.compile("[a-z]'?").matcher(strExpr);
while (m.find()) {
    String var = m.group();
    ...
}

这是解决您问题的简单可行的方法

public static void simplify(String strexp){

         int length = strexp.length();

             //get the size and variables used in expression
            java.util.List<String> usedVariables = new ArrayList<String>();
            for (int i = 0; i < length; i++) {
                String c = strexp.charAt(i) + "";       

                  // check for character which are not actual


                if (!usedVariables.contains(c)) {
                    usedVariables.add(c);
                }else{
                    if((i+1) < length){
                        String c2 = strexp.charAt(i+1) + "";

                        if(c2.equals("'")){
                            c = c + c2;
                            if (!usedVariables.contains(c)) {
                                usedVariables.add(c);
                            }
                        }
                    }                   
                }
            }
            System.out.println(""+usedVariables.size());
        }
publicstaticvoidsimplify(字符串strexp){
int length=strexp.length();
//获取表达式中使用的大小和变量
java.util.List usedVariables=new ArrayList();
for(int i=0;i
如果我的表达式有一个括号呢?不幸的是,我并不完全清楚您想做什么。简单的表达式解析/解释可以使用堆栈,按下
并弹出
。我将制作一个真值表,首先我需要确定表达式中使用的变量。真值表的两个输出来自未简化表达式和简化表达式。