-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3.cpp
More file actions
54 lines (46 loc) · 1.11 KB
/
Assignment3.cpp
File metadata and controls
54 lines (46 loc) · 1.11 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
#include "std_lib_facilities.h"
class Token{
public:
char kind;
double value;
};
Token get_token();
vector<Token> tokens;
int main(){
for(Token t = get_token(); t.kind != 'q'; t = get_token()){
tokens.push_back(t);
}
for(Token token: tokens){
if (token.kind == '#')
cout << "A number token with val= "<< token.value << "\n";
else if(token.value == 0)
cout << "A token of kind " << token.kind << "\n";
else
cout << "We received an invalid token of value " << token.kind << "\n";
}
}
Token get_token(){
char ch;
cin >> ch;
switch(ch){
case 'q':
case ';':
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
return Token{ch};
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
{
cin.putback(ch);
double val;
cin >> val;
return Token{'#', val};
}
default:
return Token{ch, 1};
}
}