是否存在一个通用/定义的设计模式,该模式将有助于对布尔表达式的求值器进行编程。
我正在为此类表达式编写字符串匹配算法,并寻找一种有助于构造算法的设计模式。
样本期望字符串-
"nike AND (tshirt OR jerseys OR jersey OR tshirts OR (t AND shirt)) AND black"
答案 0 :(得分:1)
您的表达式在infix notation中。要对其进行评估,请将其转换为postfix notation。
中缀表达式如下:
row = sheet.createRow(rowNum++);
if (department.getId() == student.getDepartmentId()) {
row.createCell(0).setCellValue(student.getIdNumber());
...
}
后缀表达式如下:
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.6
@angular-devkit/build-angular 0.13.6
@angular-devkit/build-optimizer 0.13.6
@angular-devkit/build-webpack 0.13.6
@angular-devkit/core 7.3.6
@angular-devkit/schematics 7.3.6
@angular/cli 7.3.6
@ngtools/webpack 7.3.6
@schematics/angular 7.3.6
@schematics/update 0.13.6
rxjs 6.3.3
typescript 3.2.4
webpack 4.29.0
您可以使用Shunting Yard Algorithm转换表达式。
在转换表达式后,evaluate使用此方法(伪代码):
<operand><operator><operand>
答案 1 :(得分:0)
我不知道哪种设计模式本身会适合您的问题,但是如果您的编程语言具有正则表达式支持,我们可以轻松编写出这样的模式:
(?=.*\bnike\b)(?=.*\b(?:tshirts?|jerseys?|t\b.*\bshirt|shirt\b.*\bt))(?=.*\bblack\b).*
模式可以解释为:
(?=.*\bnike\b) match "nike" AND
(?=.*\b(?:tshirts?|jerseys?|t\b.*\bshirt|shirt\b.*\bt))
match tshirt(s), jersey(s) or "t" and "shirt" AND
(?=.*\bblack\b) match "black"
.* then consume the entire line