-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
187 lines (162 loc) · 4.2 KB
/
token.cpp
File metadata and controls
187 lines (162 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <cstdio>
#include <cstring>
#include <cctype>
#include "calculator.h"
// using namespace std;
// Token class member functions (given code)
// Default to initialize to the ERROR TokenType
Token::Token()
{
type = ERROR;
op = '$';
val = -999;
}
// Initialize to a specific TokenType
Token::Token (TokenType t)
{
type = t;
op = '$';
val = -999;
}
// Set to a specific TokenType
void Token::setToType(TokenType t)
{
type = t;
op = '$';
val = -999;
}
// Set to a OPERATOR TokenType with specific operator value
void Token::setToOperator(char c)
{
type = OPERATOR;
op = c;
val = -999;
}
// Set to a VALUE TokenType with a specific numeric value
void Token::setToValue(int v)
{
type = VALUE;
op = '$';
val = v;
}
// return true if the Current Token is of the given TokenType
bool Token::equalsType(TokenType t)
{
if (type == t)
return true;
else
return false;
}
// return true if the Current Token is of the OPERATOR TokenType
// and contains the given operator character
bool Token::equalsOperator(char c)
{
if (type == OPERATOR && op == c)
return true;
else
return false;
}
// Return the Operator for the current Token
// verify the current Token is of the OPERATOR TokenType
char Token::getOperator ()
{
if (type == OPERATOR)
return op;
else
return '$'; // using $ to indicate an error value
}
// Return the Value for the current Token
// verify the current token is of the value TokenType
int Token::getValue()
{
if (type == VALUE)
return val;
else
return -999; // using -999 to indicate an error value
}
// TokenReader class member functions (given code)
// initialize the TokenReader class to read from Standard Input
TokenReader::TokenReader()
{
// set to read from Standard Input
inputline[0] = '\0';
pos = 0;
needline = true;
}
// Force the next getNextToken to read in a line of input
void TokenReader::clearToEoln()
{
needline = true;
}
// Return the next Token from the input line
Token TokenReader::getNextToken()
{
char* endCheck;
//printf ("getToken %d, %d, %s\n", pos, needline, inputline);
// get a new line of input from user
if (needline)
{
endCheck = fgets ( inputline, 300, stdin);
if (endCheck == NULL )
{
printf ("Error in reading");
Token t(EOFILE);
return t;
}
for (int i = 0 ; i < strlen(inputline) ; i++)
if ('\n' == inputline[i])
inputline[i] = ' ';
strcat (inputline , " "); // add a space at end to help deal with digit calculation
needline = false;
pos = 0;
}
// skip over any white space characters in the beginning of the input
while ( pos < strlen(inputline) && isspace(inputline[pos]) )
{
pos++;
}
// check for the end of the current line of input
if (pos >= strlen(inputline))
{ // at end of line
needline = true;
Token t(EOLN);
return t;
}
// Get the next character from the input line
char ch = inputline[pos]; pos++;
// check if 'q' or 'Q' was entered ==> QUIT Token
if ( 'q' == ch || 'Q' == ch )
{
return Token (QUIT);
}
// check if "?" was entered ==> HELP Token
if ( '?' == ch )
{
return Token (HELP);
}
// check for Operator values of: + - * / ( ) ==> OPERATOR Token
if ( ('+' == ch) || ('-' == ch) || ('*' == ch) ||
('/' == ch) || ('%' == ch) || ('(' == ch) || (')' == ch) )
{
Token t;
t.setToOperator( ch );
return t;
}
// check for a number ==> VALUE Token
if (isdigit(ch))
{
int number = int (ch) - int('0'); // subtract ascii value of ch from ascii value of '0'
ch = inputline[pos]; pos++;
while (isdigit(ch))
{
number = number * 10 + int (ch) - int('0');
ch = inputline[pos]; pos++;
}
pos--; // since number calcuation check one character after the last digit
Token t;
t.setToValue( number );
return t;
}
// Input in not valid if code get to this part ==> ERROR Token
return Token (ERROR);
}