Question
I am getting the following errors while trying to compile this code. I need it to compute the values in the provided .txt file (located
I am getting the following errors while trying to compile this code. I need it to compute the values in the provided .txt file (located at bottom of page), but it only computes the first line correctly, incorrectly values the second, and then fails completely.
Build error provided:
Build started...1>------ Build started: Project: Skeleton, Configuration: Debug x64 ------1>operand.cppProject 2\Interpreter\Skeleton\code\operand.h(10,14): error C2011: 'Operand': 'class' type redefinitionProject 2\Interpreter\Skeleton\code\operand.h(10,7): message : see declaration of 'Operand'Project 2\Interpreter\Skeleton\code\variable.h(12,33): error C2504: 'Operand': base class undefinedProject 2\Interpreter\Skeleton\code\literal.h(12,31): error C2504: 'Operand': base class undefinedProject 2\Interpreter\Skeleton\code\operand.cpp(32,22): error C2027: use of undefined type 'Operand'Project 2\Interpreter\Skeleton\code\operand.h(10,7): message : see declaration of 'Operand'Project 2\Interpreter\Skeleton\code\operand.cpp(39,29): error C2440: 'initializing': cannot convert from 'Literal *' to 'Expression *'Project 2\Interpreter\Skeleton\code\operand.cpp(39,29): message : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style castProject 2\Interpreter\Skeleton\code\operand.cpp(47,28): error C2440: 'return': cannot convert from 'Variable *' to 'Expression *'Project 2\Interpreter\Skeleton\code\operand.cpp(47,28): message : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style cast1>project2.cpp1>subexpression.cpp1>variable.cppProject 2\Interpreter\Skeleton\code\operand.h(10,34): error C2504: 'Expression': base class undefinedProject 2\Interpreter\Skeleton\code\operand.h(12,22): error C2143: syntax error: missing ';' before '*'Project 2\Interpreter\Skeleton\code\operand.h(12,22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-intProject 2\Interpreter\Skeleton\code\operand.h(12,47): error C2238: unexpected token(s) preceding ';'1>Generating Code...1>Done building project "Skeleton.vcxproj" -- FAILED.
Unfortunately, CourseHero won't let me attach my .zip folder, so I'll post each as I have them in Visual Studio 2022. I will also attach the .txt file, which was to be added to the master folder that has the code folder as well.
P.S. Too many tutors are too selective and are looking for the easy answer, so they won't bother helping me. I had over 150 tutors look at my question before and it's quite sad Course Hero doesn't have any type of standard for tutors. If there is someone out there willing to help me I am eternally grateful--but please don't give me examples of how to fix a problem LIKE mine, please run my code and tell me exactly how to fix it so I can learn.
My code:
(Header Files)
and.h
class And: public SubExpression{public: And(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() && right->evaluate(); }};
conditional.h
class Conditional : public SubExpression {public: Conditional(Expression* left, Expression* right, Expression* condition) : SubExpression(left, right) { this->condition = condition; } double evaluate() { return condition->evaluate() ? left->evaluate() : right->evaluate(); }private: Expression* condition;};
discard.h
class Discard : public SubExpression {public: Discard(Expression* left, Expression* right, Expression* condition) : SubExpression(left, right) { this->condition = condition; } double evaluate() { double temp = condition->evaluate(); return temp > 0 ? left->evaluate() : right->evaluate(); }private: Expression* condition;};
divide.h
class Divide: public SubExpression{public: Divide(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() / right->evaluate(); }};
equality.h
class Equality: public SubExpression{public: Equality(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() == right->evaluate(); }};
expression.h
class Expression {public: virtual double evaluate() = 0;};
greaterThan.h
class GreaterThan: public SubExpression{public: GreaterThan(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() > right->evaluate(); }};
lessThan.h
class LessThan: public SubExpression{public: LessThan(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() evaluate(); }};
literal.h
class Literal: public Operand {public: Literal(double value) { this->value = value; } double evaluate() { return value; }private: double value;};
minus.h
class Minus: public SubExpression {public: Minus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() - right->evaluate(); }};
modulo.h
class Modulo : public SubExpression{public: Modulo(Expression* left, Expression* right) : SubExpression(left, right) { } double evaluate() { return static_cast(left->evaluate()) % static_cast(right->evaluate()); }};
negation.h
class Negation : public SubExpression{public: Negation(Expression* left, Expression* right) : SubExpression(left, right) { } double evaluate() { return (left->evaluate()); }};
operand.h
class Operand: public Expression {public: static Expression* parse(stringstream& in); };
or.h
class Or: public SubExpression{public: Or(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() || right->evaluate(); }};
parse.h
string parseName(stringstream& in);
plus.h
class Plus: public SubExpression {public: Plus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() + right->evaluate(); }};
power.h
class Power: public SubExpression{public: Power(Expression* left, Expression* right) : SubExpression(left, right) { } double evaluate() { return pow(left->evaluate(), right->evaluate()); }};
subexpression.h
class SubExpression: public Expression {public: SubExpression(Expression* left, Expression* right); static Expression* parse(stringstream& in);protected: Expression* left; Expression* right;};
symboltable.h
#include #include using namespace std;class SymbolTable {public: SymbolTable() {} void insert(string variable, double value); double lookUp(string variable) const; void init();private: struct Symbol { Symbol(string variable, double value) { this->variable = variable; this->value = value; } string variable; double value; }; vector elements;};
ternary.h
class Ternary: public SubExpression{public: Ternary(Expression* left, Expression* right, Expression* condition): SubExpression(left, right) { this->condition = condition; } double evaluate() { return condition->evaluate() ? left->evaluate() : right->evaluate(); }private: Expression* condition;};
times.h
class Times: public SubExpression{public: Times(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() * right->evaluate(); }};
variable.h
#include "operand.h"class Variable : public Operand {public: Variable(string name) { this->name = name; } double evaluate();private: string name;};
(Source Files)
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;}
parse.cpp
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;}
project2
#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; fin = ifstream("input.txt"); if (!(fin.is_open())) { cout > paren; cout > comma; parseAssignments(in); double result = expression->evaluate(); cout > ws >> assignop >> value >> delimiter; symbolTable.insert(variable, value); } while (delimiter == ',');}
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"#include "power.h"#include "modulo.h"#include "conditional.h"#include "discard.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; left = Operand::parse(in); in >> operation; if (operation == '~') { in >> paren; return new Negation(left, NULL); } else if (operation == ':') { right = Operand::parse(in); in >> paren; condition = Operand::parse(in); in >> paren; return new Ternary(left, right, condition); } else { 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 '> paren; return new Conditional(left, right, condition); case '#': condition = Operand::parse(in); in >> paren; return new Discard(left, right, condition); } } return 0;}
symboltable.cpp
#include #include using namespace std;#include "symboltable.h"void SymbolTable::init() { elements.clear();}void SymbolTable::insert(string variable, double value) { struct Symbol symbol(variable, value); elements.push_back(symbol);}double SymbolTable::lookUp(string variable) const { for (int i = 0; i
variable.cpp
#include using namespace std;#include "variable.h"#include "symboltable.h"extern SymbolTable symbolTable;double Variable::evaluate() { return symbolTable.lookUp(name);}
(Text File)
input.txt
((a + 4) ~), a = 3;((x * 2.6) + (y - 3)), x = 1.5, y = 6;(( 7 / z_1) + (z_1 ^ 2)), z_1 = 2;((6 % b) d), c = 9, d = 7;(e & 8), e = 5;(f ? 1 2), f = 0;(g # 1 2 3), g = 2;(tt + ss), tt = 2;(aa + 1), aa = 1, aa = 2;
(Assignment Outline)
CMSC 330 Project 2 The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study in the week 5 module reading The skeleton code for this project is attached. It differs slightly from the what is provided in the case study. The code in the case study allows only one expression to be input from the keyboard whereas the code in the attached skeleton accepts input from a file named input . txt. That file contains one statement per line. The statements of that expression language consist of an arithmetic expression followed by a list of assignments. Assignments are separated from the expression and each other by commas. A semicolon terminates the expression. The arithmetic expressions are fully parenthesized infix expressions containing integer literals and variables. The syntax of a single input file line is described grammar below: statement - expression assignments '; expression - operand operator operand ') ' operator - '+' 1 - operand - literal | variable | expression assignments - assignments assignment | assignment assignment - variable '=' literal In the above grammar, terminal symbols are upper case names or character literals shown in blue and nonterminal symbols are lower case names shown in red. EBNF metacharacters are shown in black. Tokens can be separated by any number of spaces. Tokens can be separated by any number of spaces. Variable names begin with an alphabetic character, followed by any number of alphanumeric characters. Variable names are case sensitive. The regular expressions defining the variables and literal tokens are the following: variable [a-zA-2] [a-=A-20-9]* literal [0-9]+ The program reads in the arithmetic expression and encodes the expression as a binary tree. After the expression has been read in, the variable assignments are read in and the variables and their values of the variables are placed into the symbol table. Finally the expression is evaluated recursively. Your first task is to modify the program so that it will parse additional types of expressions defined by the expanded grammar shown below with the additions to the grammar highlighted in yellow: statement - expression assignmentsexpression - expressions " ) expressions - unary expression | binary expression | ternary expression quaternary expression unary expression - expression 's' xpression expression binary_operator expression binary_operator ternary expression - expression '?' expression expression quaternary_expression 1 - expression '#' expression expression expression operand - literal | variable | expression assignments - assignments assignment | assignment assignment - variable '=' literal The semantics of the additional binary arithmetic operators are as follows: * Multiplication / Division Remainder * Exponentialion Although two of the three additional binary operators are customarily relational operators in most languages, that is not true in this language. The semantics of all three of those operators are as follows: Maximum (Evaluates to the maximum of the left and right operand) 6 Average (Evaluates to the average of the left and right operand) The single unary operator - is the negation operator. Unlike the unary minus in most languages, it is a postfix operator rather than a prefix one. The single ternary operator ? is the conditional expression operator. Unlike the conditional expression operator in C+ + and Java, no colon separates the second and third operands. This expression is evaluated as follows. If the expression to the left of the operator ? is not 0, the value of the expression is the value of the first expression after the operator ?. If it is 0, the value of the expression is the value of the second expression after the operator ?. The single quaternary operator # is a variation of the typical conditional expression operator. Like the ternary conditional expression operator, the remaining three operands are delimited only by whitespace. This expression is evaluated as follows. If the expression to the left of the operator # is less than 0, the value of the expression is the value of the first expression after the operator #. If it is equal to 0, the value of the expression is the value of the second expression after the operator #. If it is greater than 0, the value of the expression is the value of the third expression after the operator #.The second task is to modifier the variable token so that underscores are permitted in all but the first character and modify the literal token so that it accepts unsigned floating point literals. Assignments also should be modified to allow assignment to values that are should also floating point rather than just integers. The final task is to make the following modifications: . The symbol table should be initialized before each statement is evaluated, so that variables that are reused do not contain the value from a previous statement Statements containing uninitialized variables should be reported as an error A variable initialized more than once in a statement should be reported as an error (Creating an exception class to accommodate this error and the previous one is the recommended approach) You may assume that all input is syntactically correct. No checks for syntax errors is required. Each new class must be in a separate . h and .epp pair of files. If all the functions in a class are one line functions, they can be implemented inline in .h file and the .epp file can be omitted. The deliverables for this project include the following: 1. A . zip file containing all the source code correctly implementing all required functionality. a. All the .h and .epp files provided in the skeleton should included regardless of whether they required any changes b. All new files must include a header with the student name, date, project and a description of what the file contains c. All modified files must include a header with the same information 2. A Word or PDF file that contains the following: a. A discussion of how you approached the project b. A test plan that contains test cases that include all additional images and test any new functionality. For each test case, the output produced should be included c. A discussion of lessons learned from the project and any improvements that could be madeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started