I have a class called logic which holds a single criteria to evaluate. A different object can have up to 20 of these criteria. I currently have an array containing multiple of these Logic classes. I can loop through the array and call evaluate on all of the Logic classes, then AND all of the results together to get a final result, which has been working. However, I am now needing to not only AND the results, but also OR. Is there a data structure to define something like (LOGIC1 && LOGIC2) || LOGIC3 that could link how these classes relate better than an array? The only criteria is that it has to be easy to ready when defining the structure, so something like this:
LOGIC(HEIGHT, GREATER_THAN, 50),
AND,
LOGIC(HEIGHT, LESS_THAN, 100),
OR,
LOGIC(WEIGHT, LESS_THAN, 500)
Here is a simple version of the code.
enum class OPERATION
{
GREATER_THAN,
LESS_THAN
};
enum class CONDITION
{
HEIGHT,
WEIGHT
};
class Logic
{
CONDITION con;
OPERATION op;
double op2;
public:
bool evaulate(CONDITION con, double op1){
if(con == CONDITION::HEIGHT)
{
if(op == OPERATION::GREATER_THAN)
{
return op1 > op2;
}
else if(op == OPERATION::LESS_THAN)
{
return op1 < op2;
}
}
}
Logic(CONDITION a, OPERATION b, double c){
con = a;
op = b;
op2 = c;
}
};
int main()
{
std::array<Logic, 2> statements = {
Logic(CONDITION::HEIGHT, OPERATION::GREATER_THAN, 7.31),
Logic(CONDITION::WEIGHT, OPERATION::LESS_THAN, 100.0)
//would also want like OR Logic(CONDITION::WEIGHT, OPERATION::LESS_THAN, 100.0)
};
//loop through the statements and evauluate
}
user25135401 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.