int main () { // example expression: (true and x) or (y and (not x)) BooleanExp* expression; Context context; VariableExp* x = new VariableExp("X"); VariableExp* y = new VariableExp("Y"); expression = new OrExp(new AndExp(new Constant(true), x), new AndExp(y, new NotExp(x))); context.Assign(x, false); context.Assign(y, true); bool result = expression->Evaluate(context); VariableExp* z = new VariableExp("Z"); NotExp not_z(z); BooleanExp* replacement = expression->Replace("Y", not_z); context.Assign(z, true); result = replacement->Evaluate(context); return 0; }
main () { /* */ BooleanExp* expression; Context context; VariableExp* x = new VariableExp("X"); VariableExp* y = new VariableExp("Y"); expression = new OrExp( new AndExp(new Constant(true), x), new AndExp(y, new NotExp(x)) ); /* */ context.Assign(x, false); context.Assign(y, true); bool result = expression->Evaluate(context); /* */ BooleanExp* replacement; VariableExp* z = new VariableExp("Z"); replacement = new NotExp(z); expression->Replace("Y", *replacement); context.Assign(z, true); result = expression ->Evaluate(context); /* */ }
int main() { BooleanExp* expression,*expression2; Context context; VariableExp* x = new VariableExp("X"); VariableExp* y = new VariableExp("Y"); expression = new OrExp( new AndExp(new Constant(true), x), new AndExp(y, new NotExp(x))); context.Assign(x, false); context.Assign(y, true); bool result = expression->Evaluate(context); cout<<"result: "<<result<<endl; }
int main(int argc, char* argv[]) { //生成表达式 AbstractExpression* expression; //解析器对象 Context context; //两个变量 VariableExp* varX = new VariableExp("keyX"); VariableExp* varY = new VariableExp("keyY"); /*一个复杂表达式 或(与(常量,变量),与(变量,非(变量)))*/ expression = new OrExp( new AndExp(new ConstantExp(true), varX), new AndExp(varY, new NotExp(varX))); //在解析器中建立外部名字和值的关联 context.Assign(varX, false); context.Assign(varY, true); //递归运算表达式求值 bool result = expression->Interpret(context); cout<<"result: "<<result<<endl; printf("Hello World!\n"); return 0; }
int main(int argc, char ** argv) { BooleanExp *expression; Context context; VariableExp* x = new VariableExp("X"); VariableExp* y = new VariableExp("Y"); expression = new OrExp( new AndExp(new Constant(true),x), new AndExp(y,new NotExp(x))); context.Assign(x,false); context.Assign(y,true); bool result = expression->Evaluate(context); char *str; if(result == true) str = "true"; else str = "false"; printf("result is %s\n",str); system("pause"); return 0; }