当前位置:网站首页>Add parentheses to Boolean expressions for short circuit operators
Add parentheses to Boolean expressions for short circuit operators
2022-04-21 07:16:00 【So you're lucky】
Due to the need to do the visualization process of expression , At the beginning , The left to right parsing method is used , as follows , It can be seen that there are disadvantages , namely A || B && C It should be calculated first B && C And again A||, But from left to right, it becomes first A || B Again && C, Lead to incorrect results . Can be tested :true|| false && false; From left to right is false, But in fact, the result is true; Therefore, it needs to be analyzed , It can be interpreted as A || (B && C) Only in this way can the front end correctly parse ;

Java Version code
/**
* @author LaiYongBin
* @date Founded in 2022/4/18 19:29
* @apiNote DO SOMETHING
*/
public class Test {
public static void main(String[] args) {
String s = "A||B&&C&&(A&&D||T&&S)||F&&D";
System.out.println(build(s));
}
public static String build(String param) {
param = param.replace(" ", "");
param = param.replace("&&", " && ");
param = param.replace("||", " || ");
List<String> stringList = allParentheses(param);
// take && Add brackets , The parts in brackets do not add
String toString = putParentheses(param);
for (String now : stringList) {
toString = toString.replace(now, build(now));
}
return toString;
}
public static String putParentheses(String param) {
int left = 0;
// Whether there are left parentheses added by yourself
boolean haveKuo = false;
char[] toCharArray = param.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < toCharArray.length; i++) {
char nowChar = toCharArray[i];
sb.append(nowChar);
if (nowChar == '(') {
left++;
continue;
}
if (nowChar == ')') {
left--;
}
if (left != 0 || nowChar != ' ') {
continue;
}
if (!haveKuo && (i + 4 < toCharArray.length) && toCharArray[i + 4] == '&') {
sb.append('(');
haveKuo = true;
} else if (haveKuo && (i + 1 >= toCharArray.length || toCharArray[i + 1] == '|')) {
sb.append(')');
haveKuo = false;
}
}
if (haveKuo) {
sb.append(')');
}
return sb.toString();
}
/**
* Get everything in parentheses
*/
public static List<String> allParentheses(String eval) {
// true || (false && false)
// Number of left parentheses
int left = 0;
// Has it been matched to
boolean b = false;
StringBuilder s = new StringBuilder();
List<String> resultList = new ArrayList<>();
for (char c : eval.toCharArray()) {
if (c == '(') {
left++;
b = true;
} else if (c == ')') {
left--;
}
if (b) {
s.append(c);
}
if (b && (left == 0)) {
resultList.add(s.substring(1, s.length() - 1));
s = new StringBuilder();
b = false;
}
}
return resultList;
}
}
JS Code
// Method entrance
function build(param) {
param = param.replaceAll(" ", "");
param = param.replaceAll("&&", " && ");
param = param.replaceAll("||", " || ");
const stringList = allParentheses(param);
// take && Add brackets , The parts in brackets do not add
let res = putParentheses(param);
for (let i = 0; i < stringList.length; i++) {
const now = stringList[i];
// recursive
res = res.replaceAll(now, build(now));
}
return res;
}
function putParentheses(param) {
// Number of parentheses matched
let leftBrackets = 0;
// Whether there are left parentheses added by yourself
let haveKuo = false;
let result = "";
for (let i = 0; i < param.length; i++) {
const nowChar = param[i];
result += nowChar;
if (nowChar === '(') {
leftBrackets++;
continue;
}
if (nowChar === ')') {
leftBrackets--;
}
if (leftBrackets !== 0 || nowChar !== ' ') {
continue;
}
if (!haveKuo && (i + 4 < param.length) && param[i + 4] === '&') {
result += "(";
haveKuo = true;
} else if (haveKuo && (i + 1 >= param.length || param[i + 1] === '|')) {
result += ")";
haveKuo = false;
}
}
if (haveKuo) {
result += ")";
}
return result;
}
/** Get the value of all outer parentheses */
function allParentheses(param) {
// true || (false && false)
// Number of left parentheses
let leftBrackets = 0;
// Has it been matched to
let isMatched = false;
let s = "";
const resultList = [];
for (let i = 0; i < param.length; i++) {
const c = param[i];
if (c === '(') {
leftBrackets++;
isMatched = true;
} else if (c === ')') {
leftBrackets--;
}
if (isMatched) {
s += c;
}
if (isMatched && (leftBrackets === 0)) {
resultList.push(s.substring(1, s.length - 1));
s = "";
isMatched = false;
}
}
return resultList;
}
版权声明
本文为[So you're lucky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210615360017.html
边栏推荐
- P1586 四方定理题解
- How does excel form become latex format?
- Canvas drawing Luffy
- applicationContext. How to solve the problem of XML becoming gray document
- win10的jupyter notebook使用pyspark2.3.2解决的报错问题
- yolov5的onnx模型去除transpose层
- [SSM integration] 4 Logic code writing and testing
- 数据的导入
- uniapp中动态底部导航栏tabbar实现,权限管理
- 毕业设计,课程环节学生成绩评价系统
猜你喜欢
随机推荐
3. Transactions and views
记录tx2上安装配置gestermer进而使用gst-rtsp-server
systemd如何使用/etc/init.d脚本
How to download and use the journal latex template
[threadx] threadx source Reading plan (II)
There is no prompt for importing JSTL tag library URI
VMware Workstation server service failed to start
Tensorflow案例4:Mnist手写数字识别(线性神经网络)及其局限性
Simultaneous access of computer intranet and extranet - solution
【Labview】记录下做Labview项目的一些坑点
LEFT JOIN关联表中ON,WHERE后面跟条件的区别
Learn SCI paper drawing skills (a)
导jstl标签库uri没有提示
win10的jupyter notebook使用pyspark2.3.2解决的报错问题
开放平台及其技术架构
Program download and data extraction using JLINK command line
uView的Waterfall 瀑布流,实现单列,加载更多
[WPF] notes
反射执行FlinkSql代码时找不到UDF的class报ClassNotFound
错误OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write









