Reputation: 143
I want to make condition statement from the string includes parentheses, AND and OR.
So i found the codes that split string with parenthese from stackoverflow.
let array = [],
c = 0;
'(A OR B) AND C'.split(/([()])/).filter(Boolean).forEach(e =>
e == '(' ? c++ : e == ')' ? c-- : c > 0 ? array.push('(' + e + ')') : array.push(e)
);
let finalQuery = [];
while (array.length > 0) {
const condition = array.pop();
// console.log(condition)
if (condition.includes("OR")) {
let temp = condition.trim().split(" OR ");
temp.map(each => {
finalQuery.push(each);
})
} else if (condition.includes("AND")) {
let temp = condition.trim();
if (temp.includes("AND")) {
if (temp.indexOf("AND") == 0) {
if (finalQuery.length > 0) {
finalQuery = finalQuery.map(x => x + " " + temp.replace("AND", ""))
}
} else if (temp.indexOf("AND") == temp.length - 3) {
if (finalQuery.length > 0) {
finalQuery = finalQuery.map(x => temp.replace("AND", "") + ' ' + x)
}
}
}
}
console.log(finalQuery)
}
I tried the codes above.
But it doesn't work.
I want results like below.
Input: (A OR B) AND (C OR D)
Output: A C OR A D OR B C OR B D
Input: (A OR B) AND (C OR D) AND (E OR F)
Output: A C E OR A C F OR A D E OR A D F OR B C E OR B C F OR B D E OR B D F
Could anyone help me about it?
Upvotes: 0
Views: 405
Reputation: 324
Only for format by A AND/OR B AND/OR C ...
,
let's assume that
AND
and OR
expression = token opertaor token
. - (1)I evaluate all expressions to token as string. and I have some rules for it:
A OR B
-> A,B
(tokens as string)A AND B
-> AB
A,B AND C
can be evaluated to AC,BC
parse and get expression by bracket (
and )
, and evaluated it.
For example, (A AND B) OR C
is evaluated to AB OR C
and do it again to AB,C
. Now we can evaluated all expression by above (1)
here code is. Try it:
function is_exp(s){
return s.indexOf(' OR ') !== -1 || s.indexOf(' AND ') !== -1;
}
// return "exp oper exp" to evaluated exp
function eval(s) {
// OR operation
if (s.indexOf('OR') != -1) {
var i = s.indexOf('OR');
var first = s.slice(0, i - 1);
var second = s.slice(i + 3);
return [first, second].toString(); // merge
}
// AND operation
else if (s.indexOf('AND') != -1) {
var i = s.indexOf('AND');
var first = s.slice(0, i - 1).split(',');
var second = s.slice(i + 4).split(',');
var result = [];
for(var f0 of first) {
for(var s0 of second) {
result.push(f0 + s0);
}
}
return result.toString();
}
// wrong and fail
else {
return "";
}
}
function _calculate(s) {
var l = -1, r = Infinity;
var tokens = [];
var opers = [];
while (l < s.length) {
l = s.indexOf('(', l+1);
if (r+1 < l) {
opers.push(s.slice(r+1, l).replace(/\s/gi, ''));
}
r = s.indexOf(')', l);
if (l == -1 || r == -1) break;
var exp = s.slice(l+1, r);
tokens.push(is_exp(exp) ? eval(exp) : exp); // if it is expression, evaluted it.
}
// evaluate all chained elements
var result = tokens[0];
for(var i=1; i < tokens.length; ++i) {
result = eval(result + ' ' + opers[i-1] + ' ' + tokens[i]);
}
// make string as good to read
var lst = result.split(',');
lst = lst.map((v) => (v.split('').join(' '))); // "ABC" => "A B C"
return lst.join(' or '); // add "OR" between all tokens
}
function calculate() {
var input = document.getElementById('input');
var output = document.getElementById('output');
output.value = _calculate(input.value);
}
document.addEventListener('DOMContentLoaded', calculate);
textarea { width: 100%; display: block; margin: 5px auto; }
<textarea id="input" rows="2">(A OR B) AND (C OR D)</textarea>
<button onclick="calculate()">Run</button>
<textarea id="output" rows="2" disabled readonly></textarea>
BUT, In my code does NOT work with (A AND (B OR C)) OR D
.
If you want to handle a case with above case, I suppose that can be solved with stack or recursion and parse more perfect on brackets.
Upvotes: 2