Question: I need help with my code; for example, When I run this, it should return 2; however, it is returning 7 (2+5)#(3-1)(5+7)(1+1) /* Expect value

I need help with my code; for example, When I run this, it should return 2; however, it is returning 7 (2+5)#(3-1)(5+7)(1+1) /* Expect value = 2 */

The structure for the class is supposed to follow this: "The single quaternary operator # is a variation of the typical conditional expression operator. Like the ternary conditional expression operator, the remaining three operands are delimited only by whitespace. This expression is evaluated as follows. If the expression to the left of the operator # is less than 0, the value of the expression is the value of the first expression after the operator #. If it is equal to 0, the value of the expression is the value of the second expression after the operator #. If it is greater than 0, the value of the expression is the value of the third expression after the operator #." Please Help me fix this! How I wrote my code

class Quaternary : public SubExpression { public: Quaternary(Expression *first, Expression *second, Expression *third, Expression *condition) : SubExpression(first,second),third(third),condition(condition) { this->condition = condition; } double evaluate() { int condVal = condition->evaluate(); if (condition->evaluate() < 0) { return left->evaluate(); } else if (condVal == 0) { return right->evaluate(); } else { return third->evaluate(); } } private: Expression *third; Expression *condition; };

This is how I wrote it in my subexpression Call Folder.

#include  #include  using namespace std; #include "expression.h" #include "subexpression.h" #include "operand.h" #include "plus.h" #include "minus.h" #include "times.h" #include "divide.h" #include "and.h" #include "greaterThan.h" #include "lessThan.h" #include "ternary.h" #include "xor.h" #include "quaternary.h" #include "unary.h" #include "modulo.h" SubExpression::SubExpression(Expression *left, Expression *right) { this->left = left; this->right = right; } Expression *SubExpression::parse(stringstream &in) { Expression* left; Expression* right; Expression* third; Expression* condition; char operation, paren; /* This is where the program determines which of the three tupes of  needs to be built.*/ left = Operand::parse(in); //IF else statements then switch statement in >> operation; if (operation == '-') {// Unary Expression in >> paren; right = Operand::parse(in); in >> paren; condition = Operand::parse(in); in >> paren; return new Unary(left,right); //Passing a NULL as the second argument as the negation operation really only functions on one operand } else if (operation == '?') {//ternary Expression right = Operand::parse(in); in >> paren; condition = Operand::parse(in); in >> paren; return new Ternary(left, right, condition); } else if (operation == '#') {//Quaternary Expression right = Operand::parse(in); in >> paren; third = Operand::parse(in); in >> paren; condition = Operand::parse(in); in >> paren; return new Quaternary(left, right, third, condition); } else {//Every other Expression //These two operations need to be performed for every case that follows right = Operand::parse(in); in >> paren; switch (operation) { case '+': return new Plus(left, right); case '-': return new Minus(left, right); case '*': return new Times(left, right); case '/': return new Divide(left, right); case '&': return new And(left, right); case '>': return new GreaterThan(left, right); case '<': return new LessThan(left, right); case '^': return new Xor(left, right); case '%': return new Modulo(left, right); } } return 0; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!