-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfiz.cpp
More file actions
60 lines (49 loc) · 1.74 KB
/
infiz.cpp
File metadata and controls
60 lines (49 loc) · 1.74 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
// infiz.cpp
#include "../libinfiz/Evaluator.hpp"
#include <array>
#include <format>
#include <iostream>
constexpr int max_line = 255;
auto main() -> int
{
std::array<char, max_line> input{};
std::cin.getline(input.data(), max_line - 1, '\n');
while (std::cin.good()) {
auto result = evaluate(input.data());
if (result) {
const auto answer = *result;
std::cout << "answer: ";
if (answer.getDenominator() == 1) {
std::cout << std::format("{}\n", answer.getNumerator());
} else {
std::cout << std::format(
"{}/{} ({})\n", answer.getNumerator(), answer.getDenominator(), answer.asFloat<double>());
}
} else {
// Format the error message with the expression and position indicator
const auto &error = result.error();
// Get error type message
std::string errorTypeMsg;
switch (error.type) {
case EvaluationError::ErrorType::INVALID_NUMBER:
errorTypeMsg = "Invalid number - expected only digits";
break;
case EvaluationError::ErrorType::EMPTY_TOKEN:
errorTypeMsg = "Empty token encountered";
break;
case EvaluationError::ErrorType::STACK_ERROR:
errorTypeMsg = "Stack error - expression might be malformed";
break;
case EvaluationError::ErrorType::INVALID_EXPRESSION:
default:
errorTypeMsg = "Invalid expression";
break;
}
// Print a visually appealing error with position indicator
std::cout << "\033[1;31mError:\033[0m " << errorTypeMsg << "\n\n";
std::cout << " " << input.data() << "\n";
std::cout << " " << std::string(error.position, ' ') << "\033[1;31m^\033[0m\n\n";
}
std::cin.getline(input.data(), max_line - 1, '\n');
}
}