This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cal.cpp
202 lines (185 loc) · 6.54 KB
/
cal.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <string>
#include <cctype>
// Enums for labels.
enum Tokens {INTEGER, PLUS, MINUS, MULTIPLY, DIV,POWER, MODULE, END};
// Python None type.
const char None = '\0';
class Token {
public:
Tokens type;
std::string value;
Token(){};
Token(Tokens type, const std::string &value) : type(type), value(value) {}
// this function behave like magic method in php, python like __str__ , _tostring etc.
friend std::ostream &operator<<(std::ostream &os, const Token &token) {
os << "type: " << token.type << " value: " << token.value;
return os;
}
};
class Interpreter {
private:
Token current_token;
char current_char;
int position = 0;
std::string text;
long double result;
public:
Interpreter(const std::string &text) {
this->text = text;
this->current_char = this->text[this->position];
}
// Evaluate the expression.
Interpreter expr() {
this->current_token = this->tokenizer();
float result = this->terms();
while (this->current_token.type < END) {
if (this->current_token.type == DIV) {
this->compare(DIV);
float num = this->terms();
if (num == 0) Interpreter::error("Number can not divided by zero");
result = result / this->terms();
}
if (this->current_token.type == MULTIPLY) {
this->compare(MULTIPLY);
result = result * this->terms();
}
if (this->current_token.type == PLUS) {
this->compare(PLUS);
result = result + this->terms();
}
if (this->current_token.type == MINUS) {
this->compare(MINUS);
result = result - this->terms();
}
if (this->current_token.type == POWER) {
this->compare(POWER);
double exp = this->terms();
while (exp != 1) {
result *= result;
exp -= 1;
}
}
if (this->current_token.type == MODULE) {
this->compare(MODULE);
result = (int) result % (int) this->terms();
}
}
this->result = result;
return *this;
}
// display the output result.
void print() {
std::cout << this->result << std::endl;
}
private:
// fire an error.
static void error(const std::string& error="Error parsing input", bool quit = false) {
std::cout << error;
if (quit) exit(1);
}
// increment the position to get more characters.
void handlePosition() {
this->position += 1;
if (this->position > (this->text.length() - 1) ) {
// End of input
this->current_char = None;
} else
this->current_char = this->text[this->position];
}
// skip the whitespaces.
void skipWhitespace() {
while (this->current_char != None && this->current_char == ' ') {
this->handlePosition();
}
}
// match the valid operator.
bool opr() {
char operators[4] = {'+', '-', '*', '/'};
for (char opr : operators) {
if (this->current_char == opr)
return true;
}
return false;
}
//get the number and floating point number.
float num() {
std::string result;
while (this->current_char != None && isdigit(this->current_char)) {
result += this->current_char;
this->handlePosition();
// floating point number
if (this->current_char == '.') {
result += this->current_char;
this->handlePosition();
}
}
return std::stof(result);
}
//tokenizer
Token tokenizer() {
while (this->current_char != None) {
this->skipWhitespace(); // skip whitespaces.
if (this->position == 0 && this->opr()) {
this->handlePosition(); //skip
continue;
}
if (this->current_char == '+') {
this->handlePosition();
return Token(PLUS, "+");
}
if (this->current_char == '-') {
this->handlePosition();
return Token(MINUS, "/");
}
if (this->current_char == '*') {
this->handlePosition();
return Token(MULTIPLY, "*");
}
if (this->current_char == '/') {
this->handlePosition();
return Token(DIV, "/");
}
if (this->current_char == '^') {
this->handlePosition();
return Token(POWER, "^");
}
if (this->current_char == '%') {
this->handlePosition();
return Token(MODULE, "%");
}
if (std::isalpha(this->current_char) || (!std::isdigit(this->current_char) && !this->opr())) {
this->handlePosition(); // skip
Interpreter::error("Alphabets or Special characters are not allowed", true);
}
if (isdigit(this->current_char)) {
return Token(INTEGER, std::to_string(this->num()));
}
}
return Token(END, "");
}
//Compare the token.
void compare(Tokens T) {
if (this->current_token.type == T) {
this->current_token = this->tokenizer();
} else {
Interpreter::error("Operator error", true);
}
}
// get the number.
float terms() {
Token token = this->current_token;
this->compare(INTEGER);
return std::stof(token.value);
}
};
int main()
{
std::string input;
while(true) {
std::cout << "cal-> ";
std::getline(std::cin, input);
Interpreter inpr(input);
inpr.expr().print();
}
}