Implement an expression class. It is to be implemented using the member functions in the pictures below
Question:
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 the process of implementation
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;
}
Managerial Accounting
ISBN: 978-0697789938
13th Edition
Authors: Ray H. Garrison, Eric W. Noreen, Peter C. Brewer