侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

and or 生成访问树

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 104 阅读 / 8,138 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
运行结果
"C:\Program Files\Java\jdk1.8.0_202\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=17832:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\liu\eclipse-workspace\testWeb\build\classes;C:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.ejb.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.jms.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\gson-2.2.2.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\jettison-1.3.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\cos-multipart.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.servlet.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\commons-io-2.4.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.resource.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\jpbc-api-1.2.1.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\fastjson-1.1.41.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\java-mail-1.4.4.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\jpbc-plaf-1.2.1.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.annotation.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.persistence.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.servlet.jsp.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.transaction.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\json-simple-1.1.1.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\commons-net-3.3-ftp.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\commons-fileupload-1.3.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\javax.servlet.jsp.jstl.jar;C:\Users\liu\eclipse-workspace\testWeb\WebContent\WEB-INF\lib\mysql-connector-java-5.1.23-bin.jar" attribute.Attribute
开始:a=12
加括号后: (a=12)
--------
( a=12 ) 
----------
参数的长度:1
result: a:12 1of1
---------------------------------------------
开始:((a=12 and 12=12) or c=12) and d=12
加括号后: (((a=12 and 12=12) or c=12) and d=12)
--------
( ( ( a=12 and 12=12 ) or c=12 ) and d=12 ) 
----------
参数的长度:3
参数的长度:3
参数的长度:3
result: a:12 12:12 2of2 c:12 1of2 d:12 2of2
---------------------------------------------
开始:(a=12 and 12=12 or c=12) and (a=12 and 12=12 or c=12)
加括号后: ((a=12 and 12=12 or c=12) and (a=12 and 12=12 or c=12))
--------
( ( a=12 and 12=12 or c=12 ) and ( a=12 and 12=12 or c=12 ) ) 
----------
参数的长度:5
参数的长度:5
参数的长度:3
result: a:12 12:12 2of2 c:12 1of2 a:12 12:12 2of2 c:12 1of2 2of2
---------------------------------------------
开始:(((a=12 and 12=12 or c=12) and (a=12 and 12=12 or c=12)))
加括号后: ((((a=12 and 12=12 or c=12) and (a=12 and 12=12 or c=12))))
--------
( ( ( ( a=12 and 12=12 or c=12 ) and ( a=12 and 12=12 or c=12 ) ) ) ) 
----------
参数的长度:5
参数的长度:5
参数的长度:3
参数的长度:1
参数的长度:1
result: a:12 12:12 2of2 c:12 1of2 a:12 12:12 2of2 c:12 1of2 2of2 1of1 1of1
---------------------------------------------
result: 

Process finished with exit code 0




代码

public static String conductToTree(String val) {
    //todo:and or 的大小写
    //todo: = 或 : 两边去空格
    //todo:空格转义
val = val.trim();
if (val.equals(""))
    return "";
System.out.println("开始:" + val);
String res = "";
LinkedList<String> items = new LinkedList<>();

//取出两边冗余的括号, ()() 时会出错

// while (val.charAt(0) == '(' && val.charAt(val.length()-1) == ')') {
// val = val.substring(1, val.length() - 1);
// }

val = "(" + val + ")";
System.out.println("加括号后: " + val);
//根据空格把字符串分隔开, and or  两边必须有空格.
String[] ss = val.split(" ");
for (String s : ss) {
    s = s.trim();
    if (!s.equals("")) {
        //根据()分割   ,  考虑单个就有左右括号的情况.
        if (s.charAt(0) == '(') {
            while (s.charAt(0) == '(') {
                items.add("(");
                s = (s.substring(1, s.length()));
            }
            if (s.charAt(s.length() - 1) == ')') {
                int l = items.size();
                while (s.charAt(s.length() - 1) == ')') {
                    items.add(")");
                    s = (s.substring(0, s.length() - 1));
                }
                items.add(l, s);
            } else
                items.add(s);
        } else if (s.charAt(s.length() - 1) == ')') {
            int l = items.size();
            while (s.charAt(s.length() - 1) == ')') {
                items.add(")");
                s = (s.substring(0, s.length() - 1));
            }
            items.add(l, s);
        } else
            items.add(s);
    }
}
System.out.println("--------");
for (String s : items)
    System.out.print(s + " ");
System.out.println("\n----------");

//匹配括号进行递归
Stack<Character> kuo = new Stack<>();
ArrayList<String> para = new ArrayList();
for (int i = 0; i < items.size(); i++) {
    switch (items.get(i)) {
        case "(":
            kuo.push('(');
            break;
        case ")":
            if (kuo.pop() == '(') {
                para.clear();
                items.remove(i--);
                while (!items.get(i).equals("(")) {
                    para.add(items.get(i));
                    items.remove(i--);
                }
                System.out.println("参数的长度:" + para.size());
                if (para.size() == 1) {
                    items.set(i, para.get(0).replace("=",":")+" 1of1");
                } else
                    items.set(i, conductTree(para));
            } else
                return "括号错误";
            break;
    }
}

// while (!items.isEmpty()) {
// System.out.println(items.pop());
// }

return items.get(0);

}

//每一个字块 生成 最终的.
private static String conductTree(ArrayList<String> para) {
Collections.reverse(para);
if (!para.get(0).equals("and") || !para.get(0).equals("or"))
para.add(0, "and");
StringBuilder andB = new StringBuilder();
StringBuilder orB = new StringBuilder();
int andC = 0, orC = 0;
for (int i = 0; i < para.size(); i++) {
if (para.get(i).equals("and")) {
i++;
andB.append(para.get(i).replace('=', ':')).append(" ");
andC++;
} else if (para.get(i).equals("or")) {
i++;
orB.append(para.get(i).replace('=', ':')).append(" ");
orC++;
}
}
if (andC == 0) {
orB.append(1).append("of").append(orC);
return orB.toString();
} else if (orC == 0) {
andB.append(andC).append("of").append(andC);
return andB.toString();
} else {
if (andC != 1)
andB.append(andC).append("of").append(andC).append(" ");
andB.append(orB).append(1).append("of").append(orC + 1);
return andB.toString();
}
}

public static void main(String[] args) {
System.out.println("result: " + conductToTree("a=12"));
System.out.println("---------------------------------------------");
System.out.println("result: " + conductToTree("((a=12 and 12=12) or c=12) and d=12"));
System.out.println("---------------------------------------------");
System.out.println("result: " + conductToTree("(a=12 and 12=12 or c=12) and (a=12 and 12=12 or c=12)"));
System.out.println("---------------------------------------------");
System.out.println("result: " + conductToTree("(((a=12 and 12=12 or c=12) and (a=12 and 12=12 or c=12)))"));
System.out.println("---------------------------------------------");
System.out.println("result: " + conductToTree(""));
}

0

评论区