Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I failed to provide all of the classes on the previous question. I have a C++ program that takes input from input.txt, the program outputs

I failed to provide all of the classes on the previous question. I have a C++ program that takes input from input.txt, the program outputs to console. The program works until the input hits certain lines, mainly lines with double parentheses, percentages, question marks, pound signs, and tildes. Can I get an updated codes to correct this? I am very new to C++ (this is my first project).

project2.cpp*******

#include #include #include #include #include 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("input.txt"); while(true){ symbolTable.init(); 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); cout << "Value = " << expression->evaluate() << endl; } catch(exception) { return EXIT_FAILURE; } } 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 == ','); }

variable.h****

class Variable: public Operand{ public: Variable(string name){ this->name = name; } int evaluate(); private: string name; };

variable.cpp****

#include #include using namespace std; #include "expression.h" #include "operand.h" #include "variable.h" #include "symboltable.h" extern SymbolTable symbolTable; int Variable::evaluate(){ return symbolTable.lookUp(name); }

times.h****

class Times: public SubExpression{ public: Times(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() * right->evaluate(); } };

ternary.h****

class Ternary: public SubExpression{ public: Ternary(Expression* left, Expression* right, Expression* condition): SubExpression(left, right){ this->condition = condition; } int evaluate(){ return condition->evaluate() ? left->evaluate() : right->evaluate(); } private: Expression* condition; };

symboltable.h****

class SymbolTable{ public: SymbolTable() {} void insert(string variable, int value); int lookUp(string variable) const; void init(); private: struct Symbol{ Symbol(string variable, int value){ this->variable = variable; this->value = value; } string variable; int value; }; vector elements; };

symboltable.cpp****

#include #include using namespace std; #include "symboltable.h" void SymbolTable::insert(string variable, int value){ const Symbol& symbol = Symbol(variable, value); elements.push_back(symbol); } int SymbolTable::lookUp(string variable) const{ for (int i = 0; i < elements.size(); i++) if (elements[i].variable == variable) return elements[i].value; return -1; } void SymbolTable::init(){ if(elements.size() > 0){ for(int i = elements.size(); i > 0; i--) { elements.pop_back(); } } }

subexpression.h****

class SubExpression: public Expression{ public: SubExpression(Expression* left, Expression* right); static Expression* parse(stringstream& in); protected: Expression* left; Expression* right; };

subexpression.cpp****

#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 "or.h" #include "and.h" #include "equality.h" #include "greaterThan.h" #include "lessThan.h" #include "ternary.h" #include "negation.h" SubExpression::SubExpression(Expression* left, Expression* right){ this->left = left; this->right = right; } Expression* SubExpression::parse(stringstream& in){ Expression* left; Expression* right; Expression* condition; char operation, paren; /* This is where the program determines which of the three tupes of needs to be built. When we get here we should have burnt a paren char and the next chars in has to be an operand */ left = Operand::parse(in); // At this point the next thing in could be an , or ':' or '!' // Use a switch statement to call the correct operation in >> operation; if(operation == '!') {// Negation Expression in >> paren; return new Negation(left,NULL); //Passing a NULL as the second argument as the negation opeation really only functions on one operand } else if(operation == ':') {//Ternary Expression right = Operand::parse(in); in >> paren;// this char should acutaly be a '?' condition = Operand::parse(in); in >> paren; return new Ternary(left,right,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 Or(left, right); case '&': return new And(left, right); case '=': return new Equality(left, right); case '>': return new GreaterThan(left, right); case '<': return new LessThan(left, right); } } return 0; }

plus.h****

class Plus: public SubExpression{ public: Plus(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() + right->evaluate(); } };

parse.h****

string parseName(stringstream& in);

parse.cpp****

#include #include #include #include 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; }

or.h****

class Or: public SubExpression{ public: Or(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() || right->evaluate(); } };

operand.h****

class Operand: public Expression{ public: static Expression* parse(stringstream& in); };

operand.cpp****

#include #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 "or.h" #include "and.h" #include "equality.h" #include "greaterThan.h" #include "lessThan.h" #include "ternary.h" #include "negation.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; }

negation.h****

class Negation: public SubExpression{ public: Negation(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return !(left->evaluate()); } };

minus.h****

class Minus: public SubExpression{ public: Minus(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() - right->evaluate(); } };

literal.h****

class Literal: public Operand{ public: Literal(int value){ this->value = value; } int evaluate(){ return value; } private: int value; };

lessThan.h****

class LessThan: public SubExpression{ public: LessThan(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() < right->evaluate(); } };

greaterThan.h****

class GreaterThan: public SubExpression{ public: GreaterThan(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() > right->evaluate(); } };

expression.h****

class Expression{ public: virtual int evaluate() = 0; };

equality.h****

class Equality: public SubExpression{ public: Equality(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() == right->evaluate(); } };

divide.h****

class Divide: public SubExpression{ public: Divide(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() / right->evaluate(); } };

and.h****

class And: public SubExpression{ public: And(Expression* left, Expression* right): SubExpression(left, right){ } int evaluate(){ return left->evaluate() && right->evaluate(); } };

input.txt****

(aa + 1), aa = 1, aa = 2; (6 % b) < 5, b = 4; (f ? 1 2), f = 0; ((x * 2.6) + (y - 3)), x = 1.5, y = 6; (( 7 / z_1) + (z_1 ^ 2)), z_1 = 2; (c > d), c = 9, d = 7; (e & 8), e = 5; (g # 1 2 3), g = 2; (tt + ss), tt = 2; ((a + 4) ~), a = 3;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting for Governmental and Nonprofit Entities

Authors: Earl R. Wilson, Jacqueline L Reck, Susan C Kattelus

15th Edition

978-0256168723, 77388720, 256168725, 9780077388720, 978-007337960

More Books

Students also viewed these Programming questions