CMPT 212 expr.h
David Simpson (simpson@cs.sfu.ca)
Sat, 7 Feb 1998 12:24:54 -0800 (PST)
//
// NAME
// expr.h
//
// DESCRIPTION
// This file contains the type definitions and function prototypes
// exported by the Expression module.
//
#ifndef _expr_h_
#define _expr_h_
//
// NAME
// expr
//
// DESCRIPTION
// This class models an expression.
//
// MEMBER TYPES
// operation - kinds of operations in an expression.
//
// DATA MEMBERS
// oper - operation of the expression.
// number - value if the expression is a constant.
// op1 - pointer to the first operand. If the expression
// is a constant, then op1 is set to NULL.
// op2 - pointer to the second operand. If the expression
// is a constant, then op2 is set to NULL.
//
// FUNCTION MEMBERS
// expr()
// - default constructor.
// expr(const char *)
// - constructs an expression by parsing the given string.
// expr(const double)
// - constructs an expression from the given double.
// expr(const expr &)
// - copy constructor.
// expr(operation, expr &, expr &)
// - constructs an expression from the given operation and
// two subexpressions.
// ~expr()
// - destructor.
// void putOperation(const operation)
// - sets the top-level operation of the expression.
// operation getOperation()
// - returns the top-level operation of the expression.
// void putNumber(const double)
// - sets the number of the expression. The number is used
// only when the expression is a constant.
// double getNumber()
// - returns the number of the expression. The number is
// only meaningful if the expression is a constant.
// void putOperand1(expr *)
// - sets the first operand of the expression.
// expr * getOperand1()
// - returns the first operand of the expression.
// void putOperand2(expr *)
// - sets the second operand of the expression.
// expr * getOperand2()
// - returns the second operand of the expression.
// double evaluate()
// - returns the value of the expression.
// void display()
// - displays the expression in prefix notation.
//
class expr {
public:
enum operation { Constant,
Addition,
Subtraction,
Multiplication,
Division
};
expr();
expr(const char *);
expr(const double);
expr(const expr &);
expr(operation, const expr &, const expr &);
~expr();
void putOperation(const operation);
operation getOperation();
void putNumber(const double);
double getNumber();
void putOperand1(expr *);
expr * getOperand1();
void putOperand2(expr *);
expr * getOperand2();
double evaluate();
void display();
private:
operation oper;
double number;
expr * op1;
expr * op2;
};
#endif