BooleanExp* AndExp::Replace (const char* name, BooleanExp& exp) { return new AndExp( _operand1->Replace(name, exp), _operand2->Replace(name, exp) ); }
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); /* */ }