Repository navigation
Expand file tree
/
Copy pathsimpleC.cpp
More file actions
249 lines (216 loc) · 7.02 KB
/
Copy pathsimpleC.cpp
File metadata and controls
249 lines (216 loc) · 7.02 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* simpleC — Simple Calculator
* A lightweight terminal calculator with support for arithmetic expressions,
* percentages, parentheses, and unary minus.
*
* Licensed under GNU GPL v3.0 or later.
*
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Token structure for lexical analysis
// Each token represents a number, operator, or parenthesis
struct Token {
string type; // "number", "op", "lparen", "rparen"
float value; // numeric value (for "number" tokens)
char op; // operator character (for "op" tokens)
};
// Global variables for the recursive descent parser
vector<Token> tokens; // list of tokens after tokenization
size_t pos = 0; // current position in the token list
bool parseError = false; // error flag for invalid expressions
// Forward declaration: allows functions to call each other recursively
float parseExpression();
/*
* parseFactor — Parses the smallest unit of an expression:
* - A number
* - A parenthesized expression ( ... )
* - A unary minus followed by a factor
*
* This is the base of the recursion.
*/
float parseFactor() {
// Check for unexpected end of input
if (pos >= tokens.size()) {
parseError = true;
cout << "Error: unexpected end of expression" << endl;
return 0;
}
Token current = tokens[pos];
// Case 1: it's a number — return its value and advance
if (current.type == "number") {
pos++;
return current.value;
}
// Case 2: unary minus — negate the following factor
if (current.type == "op" && current.op == '-') {
pos++; // skip the minus sign
return -parseFactor();
}
// Case 3: opening parenthesis — parse inner expression
if (current.type == "lparen") {
pos++; // skip '('
float result = parseExpression();
// Expect a matching closing parenthesis
if (pos < tokens.size() && tokens[pos].type == "rparen") {
pos++; // skip ')'
return result;
}
parseError = true;
cout << "Error: expected ')'" << endl;
return 0;
}
// If none of the above matched, the input is invalid
parseError = true;
cout << "Error: expected a number or '('" << endl;
return 0;
}
/*
* parseTerm — Parses multiplication and division operations.
* Multiplication and division have higher precedence than addition/subtraction.
*
* Grammar: term → factor (('*' | '/') factor)*
*/
float parseTerm() {
float left = parseFactor();
if (parseError) return left; // stop if an error occurred
// Process all consecutive '*' and '/' operators
while (pos < tokens.size() && tokens[pos].type == "op" &&
(tokens[pos].op == '*' || tokens[pos].op == '/')) {
char op = tokens[pos].op;
pos++; // skip the operator
float right = parseFactor();
if (parseError) return left;
if (op == '*') {
left = left * right;
}
else { // division
if (right == 0) {
parseError = true;
cout << "Error: division by zero" << endl;
return left;
}
left = left / right;
}
}
return left;
}
/*
* parseExpression — Parses addition, subtraction, and percentage operations.
* This is the top-level function that calls parseTerm for operands.
*
* Grammar: expression → term (('+' | '-') term)*
*
* Percentage: if a '%' token follows the second operand of '+' or '-',
* it is interpreted as "percent of the current result".
* Example: 100 + 10% → 100 + (100 * 10/100) = 110
*/
float parseExpression() {
float left = parseTerm();
if (parseError) return left;
// Process all consecutive '+' and '-' operators
while (pos < tokens.size() && tokens[pos].type == "op" &&
(tokens[pos].op == '+' || tokens[pos].op == '-')) {
char op = tokens[pos].op;
pos++; // skip the operator
float right = parseTerm();
if (parseError) return left;
// Handle percentage after the second operand
// Example: "50 + 20%" — right=20, left=50, result = 50 + 50*0.2
if (pos < tokens.size() && tokens[pos].type == "op" && tokens[pos].op == '%') {
right = left * (right / 100.0);
pos++; // skip '%'
}
if (op == '+') {
left = left + right;
}
else {
left = left - right;
}
}
return left;
}
/*
* main — Entry point. Runs the REPL loop:
* - Reads user input
* - Tokenizes the input string
* - Parses and evaluates the expression
* - Prints the result or an error message
* - Exits on "exit" or "quit"
*/
int main() {
string input;
while (true) {
cout << ">> ";
getline(cin, input);
// Check for exit commands
if (input == "exit" || input == "quit") {
cout << "Goodbye!" << endl;
break;
}
// Skip empty input
if (input.empty()) {
continue;
}
// --- Tokenization: split the input string into tokens ---
tokens.clear();
string currentNumber;
for (size_t i = 0; i < input.length(); i++) {
char ch = input[i];
// Ignore whitespace
if (ch == ' ') {
continue;
}
// Build a number from consecutive digits and dots
if ((ch >= '0' && ch <= '9') || ch == '.') {
currentNumber += ch;
}
else {
// If we were building a number, save it as a token
if (currentNumber != "") {
Token t;
t.type = "number";
t.value = stof(currentNumber);
tokens.push_back(t);
currentNumber = "";
}
// Classify the symbol
if (ch == '(') {
Token t;
t.type = "lparen";
tokens.push_back(t);
}
else if (ch == ')') {
Token t;
t.type = "rparen";
tokens.push_back(t);
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
Token t;
t.type = "op";
t.op = ch;
tokens.push_back(t);
}
// Unknown characters are silently ignored
}
}
// Add the last number if there is one
if (currentNumber != "") {
Token t;
t.type = "number";
t.value = stof(currentNumber);
tokens.push_back(t);
}
// --- Parsing: evaluate the token list ---
pos = 0;
parseError = false;
float result = parseExpression();
// Print result only if parsing succeeded
if (!parseError) {
cout << result << endl;
}
}
return 0;
}