-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
234 lines (191 loc) · 6.42 KB
/
main.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
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
#include <cmath>
#include <iostream>
#include <algorithm>
constexpr char ADD = '+';
constexpr char SUBTRACT = '-';
constexpr char MULTIPLY = 'x';
constexpr char DIVIDE = '/';
constexpr char POWER = '^';
constexpr char MODULO = '%';
void printSupportedOperators();
void printHelp();
void preProcessInput(std::string &input);
void removeAllSpaces(std::string &input);
float calculate(std::string expression);
float solveOperation(float number1, float number2, char op);
bool isOperator(char c);
int getPriority(char op);
float parseStringToFloat(const std::string &str);
bool isFloat(const std::string &str);
int main() {
std::cout << "Welcome to my calculator" << std::endl;
std::cout << "Enter your calculation (h for help, q to quit)" << std::endl;
std::string input;
while (true) {
std::cout << ">" << std::flush;
// get user input
std::getline(std::cin, input);
// check if user wants to quit
if (input == "q") {
break;
}
// check if user wants help
if (input == "h" || input == "help") {
printHelp();
continue;
}
preProcessInput(input);
float result;
try {
result = calculate(input);
} catch (std::invalid_argument &e) {
std::cout << "Invalid input" << std::endl;
continue;
} catch (std::out_of_range &e) {
std::cout << "Invalid input" << std::endl;
continue;
}
std::cout << "Result: " << result << std::endl;
}
std::cout << "Bye see you next time." << std::endl;
return 0;
}
/**
* Calculates the result of the given input.
* @param input The input to calculate: 1+4/6.
* @return The result of the calculation.
*/
float calculate(std::string expression) {
while (!isFloat(expression)) {
std::string priorityNum1;
std::string priorityNum2;
char priorityOp = '\0';
size_t priorityExpressionStart = -1;
size_t priorityExpressionEnd = -1;
std::string currentNum1;
std::string currentNum2;
char currentOp;
bool currentOpFound = false;
size_t currentExpressionStart = 0;
size_t currentExpressionEnd = 0;
bool opPrevious = false;
for (size_t i = 0; i < expression.length(); i++) {
char c = expression[i];
opPrevious = (isOperator(expression[i - 1]) && !opPrevious) || i == 0;
bool charIsOp = isOperator(c) && !opPrevious;
bool endCurrentExpression = !charIsOp && (isOperator(expression[i + 1]) || i + 1 >= expression.length());
// Parse the char
if (charIsOp) {
currentOp = c;
currentOpFound = true;
} else if (!currentOpFound) {
currentNum1 += c;
} else {
currentNum2 += c;
}
currentExpressionEnd++;
// Check to update priority values
if (endCurrentExpression && currentOpFound) {
if (getPriority(currentOp) > getPriority(priorityOp)) {
priorityNum1 = currentNum1;
priorityNum2 = currentNum2;
priorityOp = currentOp;
priorityExpressionStart = currentExpressionStart;
priorityExpressionEnd = currentExpressionEnd;
}
currentNum1 = currentNum2;
currentNum2 = "";
currentOpFound = false;
currentExpressionStart = i - currentNum1.length() + 1;
currentExpressionEnd = i + 1;
}
}
float parcedPriorityNumber1 = parseStringToFloat(priorityNum1);
float parcedPriorityNumber2 = parseStringToFloat(priorityNum2);
float priorityExpressionResult = solveOperation(parcedPriorityNumber1, parcedPriorityNumber2, priorityOp);
expression =
expression.substr(0, priorityExpressionStart) +
std::to_string(priorityExpressionResult) +
expression.substr(priorityExpressionEnd);
}
return parseStringToFloat(expression);
}
float solveOperation(float number1, float number2, char op) {
switch (op) {
case ADD:
return number1 + number2;
case SUBTRACT:
return number1 - number2;
case MULTIPLY:
return number1 * number2;
case DIVIDE:
return number1 / number2;
case POWER:
return std::pow(number1, number2);
case MODULO:
return std::fmod(number1, number2);
default:
throw std::invalid_argument("Invalid operator");
}
}
int getPriority(char op) {
switch (op) {
case ADD:
case SUBTRACT:
return 1;
case MULTIPLY:
case DIVIDE:
case MODULO:
return 2;
case POWER:
return 3;
default:
return 0;
}
}
float parseStringToFloat(const std::string &str) {
try {
size_t pos;
float result = std::stof(str, &pos);
// Check if the entire string was successfully converted
if (pos != str.length()) {
throw std::invalid_argument("Invalid characters in the string");
}
return result;
} catch (const std::invalid_argument &e) {
throw std::invalid_argument("Invalid float value: " + std::string(e.what()));
} catch (const std::out_of_range &e) {
throw std::out_of_range("Float value out of range: " + std::string(e.what()));
}
}
bool isFloat(const std::string &str) {
try {
parseStringToFloat(str);
return true;
} catch (const std::invalid_argument &e) {
return false;
} catch (const std::out_of_range &e) {
return false;
}
}
bool isOperator(char c) {
return c == ADD || c == SUBTRACT || c == MULTIPLY || c == DIVIDE || c == POWER || c == MODULO;
}
void printHelp() {
printSupportedOperators();
}
void printSupportedOperators() {
std::cout << "Supported operations are: ";
std::cout << ADD << ", ";
std::cout << SUBTRACT << ", ";
std::cout << MULTIPLY << ", ";
std::cout << DIVIDE << ", ";
std::cout << POWER << ", ";
std::cout << MODULO << std::endl;
}
void preProcessInput(std::string &input) {
removeAllSpaces(input);
}
void removeAllSpaces(std::string &input) {
input.erase(std::remove_if(input.begin(), input.end(), ::isspace), input.end());
}