Computer Science assignments

ebote
Project2Skeleton.zip

expression.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the definition of the Expression class, which is an abstract class that contains one // abstract (pure virtual) function named evaluate, which must be implemented by all its subclasses. class Expression { public: virtual double evaluate() = 0; };

literal.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the definition of the Literal class, whose representation consists of the value // of the literal. Because both of its functions are one line functions, they are implemented as inline // functions. The constructor saves the value of the literal. Because this class is a subclass of Operand // which in turn is a subclass of Expression, it must implement the function evaluate, which returns the // value that corresponds to the literal. class Literal: public Operand { public: Literal(double value) { this->value = value; } double evaluate() { return value; } private: double value; };

minus.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the class definition of the Minus class, which is a subclass of SubExpression, // which in turn is a subclass of the Expression. Because both of its functions are one line functions, // they are implemented as inline functions. Its constructor initializes the left and right subexpressions // it inherits from SubExpression by calling the constructor of the SubExpression class. Because // it is an indirect subclass of Expression it must implement the evaluate function, which it does // by returning the difference of the values of the two subexpressions. class Minus: public SubExpression { public: Minus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() - right->evaluate(); } };

operand.cpp

operand.cpp

// CMSC 330 Advanced Programming Languages
// Project 2 Skeleton
// UMGC CITE
// Spring 2023

// This file contains the body of the function parseName contained in the Operand class. That function
// parses the next token. When the next non-whitespace character is a digit, it assumes a literal is next. 
// When the next character is a left parenthesis, a recursive call is made to parse the subexpression.
// Otherwise the next token is assumed to a variable. No checks are made to ensure correct syntax.

#include   < cctype >
#include   < iostream >
#include   < sstream >
#include   < list >
#include   < string >
using   namespace  std ;

#include   "expression.h"
#include   "subexpression.h"
#include   "operand.h"
#include   "variable.h"
#include   "literal.h"
#include   "parse.h"

Expression *   Operand :: parse ( stringstream &  in )   {
     char  paren ;
     int   value ;

    in  >>  ws ;
     if   ( isdigit ( in . peek ()))   {
        in  >>   value ;
         Expression *   literal   =   new   Literal ( value );
         return   literal ;
     }
     if   ( in . peek ()   ==   '(' )   {
        in  >>  paren ;
         return   SubExpression :: parse ( in );
     }
     else
         return   new   Variable ( parseName ( in ));
     return   0 ;
}

operand.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the definition of the Operand class, which is a subclass of Expression. It is an // abstract class because it does not implement the evaluate function. It contains one static (class) // function parse that parses an operand as either a literal or variable. class Operand: public Expression { public: static Expression* parse(stringstream& in); };

parse.cpp

parse.cpp

// CMSC 330 Advanced Programming Languages
// Project 2 Skeleton
// UMGC CITE
// Spring 2023

// This file contains the body of the function parseName. That function consumes all alphanumeric 
// characters until the next whitespace and returns the name that those characters form.

#include   < cctype >
#include   < sstream >
#include   < string >
using   namespace  std ;

#include   "parse.h"

string parseName ( stringstream &  in )   {
     char  alnum ;
    string name  =   "" ;

    in  >>  ws ;
     while   ( isalnum ( in . peek ()))   {
        in  >>  alnum ;
        name  +=  alnum ;
     }
     return  name ;
}

parse.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the function prototype of the parseName function whose body is defined in parse.cpp. string parseName(stringstream& in);

plus.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the class definition of the Plus class, which is a subclass of SubExpression, // which in turn is a subclass of the Expression. Because both of its functions are one line functions, // they are implemented as inline functions. Its constructor initializes the left and right subexpressions // it inherits from SubExpression by calling the constructor of the SubExpression class. Because // it is an indirect subclass of Expression it must implement the evaluate function, which it does // by returning the sum of the values of the two subexpressions. class Plus: public SubExpression { public: Plus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() + right->evaluate(); } };

project2.cpp

project2.cpp

// CMSC 330 Advanced Programming Languages
// Project 2 Skeleton
// UMGC CITE
// Spring 2023

// This file contains the main function for the project 2 skeleton. It reads an input file named input.txt
// that contains one statement that includes an expression following by a sequence of variable assignments.
// It parses each statement and then evaluates it.

#include   < iostream >
#include   < fstream >
#include   < sstream >
#include   < string >
#include   < vector >
using   namespace  std ;

#include   "expression.h"
#include   "subexpression.h"
#include   "symboltable.h"
#include   "parse.h"

SymbolTable  symbolTable ;

void  parseAssignments ( stringstream &  in );

int  main ()   {
     const   int  SIZE  =   256 ;
     Expression *  expression ;
     char  paren ,  comma ,  line [ SIZE ];
 
    ifstream fin ;
    fin  =  ifstream ( "input.txt" );
     if   ( ! ( fin . is_open ()))   {
        cout  <<   "File did not open"   <<  endl ;
        system ( "pause" );
         return   1 ;
     }
     while   ( true )   {
        fin . getline ( line ,  SIZE );
         if   ( ! fin )
             break ;
        stringstream in ( line ,  ios_base :: in );  
        in  >>  paren ;
        cout  <<  line  <<   " " ;
         try   {
            expression  =   SubExpression :: parse ( in );
            in  >>  comma ;
            parseAssignments ( in );
             double  result  =  expression -> evaluate ();
            cout  <<   "Value = "   <<  result  <<  endl ;
         }
         catch   ( string message )   {
            cout  <<  message  <<  endl ;
         }
     }
    system ( "pause" );
     return   0 ;
}

void  parseAssignments ( stringstream &  in )   {
     char  assignop ,  delimiter ;
    string variable ;
     int   value ;
     do   {
        variable  =  parseName ( in );
        in  >>  ws  >>  assignop  >>   value   >>  delimiter ;
        symbolTable . insert ( variable ,   value );
     }
     while   ( delimiter  ==   ',' );
}
   

subexpression.cpp

subexpression.cpp

// CMSC 330 Advanced Programming Languages
// Project 2 Skeleton
// UMGC CITE
// Spring 2023

// This file contains the body of the functions contained in The SubExpression class, which includes
// the constructor that initializes the left and right subexpressions and the static function parse
// parses the subexpression. Addition and subtraction are the two operators that are implemented.

#include   < iostream >
#include   < sstream >
using   namespace  std ;

#include   "expression.h"
#include   "subexpression.h"
#include   "operand.h"
#include   "plus.h"
#include   "minus.h"

SubExpression :: SubExpression ( Expression *  left ,   Expression *  right )   {
     this -> left  =  left ;
     this -> right  =  right ;
}

Expression *   SubExpression :: parse ( stringstream &  in )   {
     Expression *  left ;
     Expression *  right ;
     char  operation ,  paren ;
    
    left  =   Operand :: parse ( in );
    in  >>  operation ;
    right  =   Operand :: parse ( in );
    in  >>  paren ;
     switch   ( operation )   {
         case   '+' :
             return   new   Plus ( left ,  right );
         case   '-' :
             return   new   Minus ( left ,  right );
     }
     return   0 ;
}
        

subexpression.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the class definition of the SubExpression class, which is a subclass of Expression. // Because it does not implement the abstract function evalauate, it is an abstract class. SubExpression // objects are represented with the left and right subexpressions. The body of its constructor and the // static (class) function parse are defined in subexpression.cpp. class SubExpression: public Expression { public: SubExpression(Expression* left, Expression* right); static Expression* parse(stringstream& in); protected: Expression* left; Expression* right; };

symboltable.cpp

symboltable.cpp

// CMSC 330 Advanced Programming Languages
// Project 2 Skeleton
// UMGC CITE
// Spring 2023

// This file contains the body of the functions contained in The SymbolTable class. The insert function 
// inserts a new variable symbol and its value into the symbol table and the lookUp function returns
// that value of the supplied variable symbol name.

#include   < string >
#include   < vector >
using   namespace  std ;

#include   "symboltable.h"

void   SymbolTable :: insert ( string variable ,   double   value )   {
     const   Symbol &  symbol  =   Symbol ( variable ,   value );
    elements . push_back ( symbol );
}

double   SymbolTable :: lookUp ( string variable )   const   {
     for   ( int  i  =   0 ;  i  <  elements . size ();  i ++ )
         if   ( elements [ i ]. variable  ==  variable )
              return  elements [ i ]. value ;
     return   - 1 ;
}

symboltable.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the class definition of the SymbolTable class. The symbol table is represented // with a vector (list) of type Symbol which is a pair consisting of a variable and its associated value. // The body of its functions are defined in symboltable.cpp. class SymbolTable { public: SymbolTable() {} void insert(string variable, double value); double lookUp(string variable) const; private: struct Symbol { Symbol(string variable, double value) { this->variable = variable; this->value = value; } string variable; double value; }; vector<Symbol> elements; };

variable.cpp

variable.cpp

// CMSC 330 Advanced Programming Languages
// Project 2 Skeleton
// UMGC CITE
// Spring 2023

// This file contains the body of the function contained in The Variable class. The evaluate function 
// looks up te value of a variable in the symbol table and returns that value.

#include   < string >
#include   < vector >
using   namespace  std ;

#include   "expression.h"
#include   "operand.h"
#include   "variable.h"
#include   "symboltable.h"

extern   SymbolTable  symbolTable ;

double   Variable :: evaluate ()   {
     return  symbolTable . lookUp ( name );
}

variable.h

// CMSC 330 Advanced Programming Languages // Project 2 Skeleton // UMGC CITE // Spring 2023 // This file contains the class definition of the Variable class. The variable is represented by its // name, which the construcor initializes. Because this class is a subclass of Operand which in turn is // a subclass of Expression, it must implement the function evaluate, whose body is defined in variable.cpp. class Variable: public Operand { public: Variable(string name) { this->name = name; } double evaluate(); private: string name; };