Skip to content

ComprosoftCEO/ExpressionEvaluator

Repository files navigation

Expression Evaluator


About

The expression evaluator was my semester project for the Principles of Software Design class at IUPUI (CSCI-36300). The class consisted of 4 assignments, each one building on the previous assignment. We started in Assignment 1 by implementing a simple Array class, but eventually we built a system that can evaluate arbitrary math expressions. Each assignment built on the previous one by adding more design patterns and features to the system. This is my final code after we learned everything in class.


Design Patterns Used

  • Wrapper Facade - Array class
  • Template Method -Unary and Binary operands
  • Command - Command class hierarchy
  • Abstract Factory - Flyweight_Expr_Factory and Postfix_Expr_Factory
  • Flyweight - Flyweight_Expr_Factory
  • Composite - Expr_Node class hierarchy
  • Visitor - Variable_Getter and Variable_Setter
  • Builder - Postfix_Builder and Tree_Builder
  • Strategy - Math_Expr

This is not an exhaustive list


Compiling

Currently, the program must be compiled with a tool called MPC. The provided makefile automatically builds the makefile with MPC and runs the makefile using the traditional "make" command. However, you will need to install MPC onto the platform of your choice. Directions to install MPC are included in its repository.

I plan to replace MPC with just a traditional makefile in the future


Usage

Running the program will display the following prompt:

Enter Math Expression:

Enter the math expression into the console, and press <Enter>. It will display the result of the expression below. All parts of the expression need to be separated by spaces. This was a requirement of the class project to make the expressions easier to tokenize. So enter: 5 + 4 not 5+4. The following operators are supported:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus: %
  • Parenthesis: () and []

The calculator only supports integers, so you cannot type in fractions like 6.2. However, you can define variables using the letters a-z and A-Z. When evaluating the expression, the program will prompt you for each variable, in alphabetical order, before evaluating the expression. The same variable name will evaluate to the same value twice. For example:

Enter Math Expression: ( one * TwO + 5 ) / one
Variable "one" = 5
Variable "TwO" = 7
8