Sign In
Not register? Register Now!
Pages:
10 pages/≈2750 words
Sources:
No Sources
Level:
Other
Subject:
Literature & Language
Type:
Other (Not Listed)
Language:
English (U.S.)
Document:
MS Word
Date:
Total cost:
$ 36
Topic:

Analyzing Lexical Items (Other (Not Listed) Sample)

Instructions:
Content:

Operator Project
devide.h
class Divide : public SubExpression
{
public:
Divide(Expression* left, Expression* right) : SubExpression(left, right)
{
}
int evaluate()
{
return left->evaluate() / right->evaluate();
}
};
minus.h
class Minus : public SubExpression
{
public:
Minus(Expression* left, Expression* right) : SubExpression(left, right)
{
}
int evaluate()
{
return left->evaluate() - right->evaluate();
}
};
times.h
class Times : public SubExpression
{
public:
Times(Expression* left, Expression* right) : SubExpression(left, right)
{
}
int evaluate()
{
return left->evaluate() * right->evaluate();
}
};
expression.h
class Expression
{
public:
//declare a virtual function evaluate()
virtual int evaluate() = 0;
};
literal.h
class Literal : public Operand
{
public:
//define the construtor
Literal(int value)
{
this->value = value;
}
//define the function evaluate()
//returns the value
int evaluate()
{
return value;
}
private:
int value;
};
module3.cpp
#include
#include
#include
#include
#include
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "symboltable.h"
#include "parse.h"
//create an object of SymbolTable
SymbolTable symbolTable;
//prototype of the function
void parseAssignments(stringstream& in);
//define main function
int main()
{
// declare the variables
Expression* expression;
char paren, comma;
string line;
// create an input file stream
ifstream fin("input.txt");
// check, if the file is not opened
//then display a error message
if (!fin.is_open())
perror("error while opening file");
//use a loop, to read the content from the file
while (getline(fin, line))
{
symbolTable.init();
if (!fin)
break;
stringstream in(line, ios_base::in);
in >> paren;
cout << line << " ";
expression = SubExpression::parse(in);
in >> comma;
//call the function
parseAssignments(in);
//Display the result
int result = expression->evaluate();
cout << "Value = " << result << endl;
}
system("pause");
return 0;
}
//definition of the function parseAssignments()
void parseAssignments(stringstream& in)
{
char assignop, delimiter;
string variable;
int value;
symbolTable.init();
do
{
variable = parseName(in);
in >> ws >> assignop >> value >> delimiter;
symbolTable.insert(variable, value);
} while (delimiter == ',');
}
operand.cpp
#include
#include
#include
#include
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "operand.h"
#include "variable.h"
#include "literal.h"
#include "parse.h"
#include
//definition of the function parse()
Expression* Operand::parse(stringstream& in)
{
char paren;
double 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
class Operand : public Expression
{
public:
static Expression* parse(stringstream& in);
};
parse.cpp
#include
#include
#include
#include
using namespace std;
#include "parse.h"
//definition of the function parseName()
string parseName(stringstream& in)
{
char alnum;
string name = "";
in >> ws;
while (isalnum(in.peek()))
{
in >> alnum;
name += alnum;
}
return name;
}
parse.h
#include
#include
string parseName(stringstream& in);
plus.h
class Plus : public SubExpression
{
public:
Plus(Expression* left,Expression* right) : SubExpression(left, right)
{
}
int evaluate()
{
return left->evaluate() +right->evaluate();
}
};
subexpression.cpp
#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
//define the constructo
SubExpression::SubExpression(Expression* left, Expression* right)
{
this->left = left;
this->right = right;
}
Expression* SubExpression::parse(stringstream& in)
{
Expression* left;
Expression* right;
char operation, paren;
//read the Operand
left = Operand::parse(in);
//read the operation
in >> operation;
//read the Operand
right = Operand::parse(in);
//read the paren
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);
}
system("pause");
return 0;
}
subexpression.h

...
Get the Whole Paper!
Not exactly what you need?
Do you need a custom essay? Order right now:

Other Topics:

  • Grant Proposal for "Save the Jackalope"
    Description: Save the Jackalope Foundation is seeking a grand to support its initiative of saving and protecting the Jackalope. The main objective of this program is to protect the animal, which is on extinct due to human activities. Therefore, the initiative aims to protect and rescue the rare and near-extinct ...
    2 pages/≈550 words| No Sources | Other | Literature & Language | Other (Not Listed) |
  • The Study of Archaeological Analogues for Lifetime Prediction Essay
    Description: This review elaborates on many years of research of the archeological analogues to forecast carbon steel's long-term behavior due to disposal and nuclear waste effects....
    10 pages/≈2750 words| 16 Sources | Other | Literature & Language | Other (Not Listed) |
  • Interview manager Literature & Language Research Paper
    Description: Answer: Gladly, generally tasked with the day to day running of the gym. Ensure that all employees are prompt and responsible. Equally, manage the gym's financial transactions and ensure all bills are sorted on time. I act as the inter-link between the executive and the employees...
    2 pages/≈550 words| No Sources | Other | Literature & Language | Other (Not Listed) |
Need a Custom Essay Written?
First time 15% Discount!