Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement an expression class. It is to be implemented using the member functions in the pictures below and a token class i developed This is

 Implement an expression class. It is to be implemented using the member functions in the pictures below and a token class i developed
3.2 Expression Class The Exp.type type, which is an enum type and used in the Expression class, is defined as follows: enum E

original This field stores the original or not yet processed expression. Its type is string. tokenized This field stores th


Expression(const string& s) This constructor takes in a string and treats it as an expression. It tokenizes the input string

This is the process of implementation

Please note the similarity between the constructor that takes a string parameter and the set method in Token class. We could

the expression class has a .h and .cpp class


/////////////////////////////////

this is my token.cpp


#include "Token.h"

#include

#include

#include

#include

#include

#include


using namespace std;

enum Token_type{ID, INT, OP, EQ, OpenBrace, CloseBrace, INVALID};

Token::Token()

{

type = INVALID;

token = "";

priority = -1;

}

Token::Token(const Token& original)

{

token =original.token;

type = original.type;

priority = original.priority;

}

Token::Token(string s)

{

token = s;

this->set(s);

}

Token::~Token(){

}

void Token::set(string s)

{

token = s;

bool frontIsLetter = false;

bool buildID = false;

bool buildINT = false;

bool frontIsNumber = false;

bool frontIsZero = false;

int buildCount = 0;

if(isalpha(s.front()))

frontIsLetter = true;

if(isdigit(s.front()))

frontIsNumber = true;

if(s.front()=='0')

frontIsZero = true;

if(s.size()==1)

{

char t = s[0];

if(t=='+'||t=='-')

{

type = OP;

priority = 1;

}

else if(t=='*'||t=='/')

{

type = OP;

priority = 2;

}

else if(t=='=')

{

type = EQ;

priority = -1;

}

else if(t=='(')

{

priority = 0;

type = OpenBrace;

}

else if(t==')')

{

type = CloseBrace;

priority = -1;

}

else if(isdigit(t))

{

type = INT;

priority = -1;

}

else if(isalpha(t))

{

type = ID;

priority = -1;

}

else

{

type == INVALID;

priority = -1;

}

}

else

{

for(int i=0; i

{

char t = s[i];

if(frontIsLetter)

{

if(isalnum(t) and t!=' ')

{

buildID = true;

buildCount++;

}

else

buildID = false;

}

else if(frontIsNumber && !frontIsZero)

{

if(isdigit(t))

buildINT = true;

else

buildINT = false;

}

else

break;

}

if(buildID&&buildCount == s.size())

type = ID;

else if(buildINT)

type= INT;

else

type = INVALID;

}

}

int Token::value() const

{

int ret = -2;

if(type == INT)

ret = stoi(token);

else if(type == ID)

ret = -1;

return ret;

}

void Token::display() const

{

cout<<"type = ";

if(type==0)

cout<<"ID"<

if(type==1)

cout<<"INT"<

if(type==2)

cout<<"OP"<

if(type==3)

cout<<"EQ"<

if(type==4)

cout<<"OpenBrace"<

if(type==5)

cout<<"CloseBrace"<

if(type==6)

cout<<"INVALID"<

cout<<"token = "<< token<< " (value is " <

cout<<"priority = "<< priority<

}

// The accessors

Token::Token_type Token::get_type() const

{

return type;

}

string Token::get_token() const

{

return token;

}

int Token::get_priority() const

{

return priority;

}
 
 
 
 

3.2 Expression Class The Exp.type type, which is an enum type and used in the Expression class, is defined as follows: enum Exp_type {ASSIGNMENT, ARITHMETIC, ILLEGAL}; For the current assignment, we are not required to figure out the type of an expression. We will do that in a later assignment. For the current project, we treat an expression in two ways: 1) as a string and 2) as a sequence of tokens (Token objects stored in a vector ) obtained from the string. Erpression class is used to store an "expression", which has five members and a few member functions. The members are:

Step by Step Solution

3.54 Rating (157 Votes )

There are 3 Steps involved in it

Step: 1

maincpp include include include include include Expre... 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

Managerial Accounting

Authors: Ray H. Garrison, Eric W. Noreen, Peter C. Brewer

13th Edition

978-0073379616, 73379611, 978-0697789938

More Books

Students also viewed these Physics questions

Question

In Exercises, find the limit. lim (4x- x 16x - x

Answered: 1 week ago

Question

What is the purpose of a statement of cash flows?

Answered: 1 week ago