-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
45 lines (36 loc) · 1.31 KB
/
interface.cpp
File metadata and controls
45 lines (36 loc) · 1.31 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
// Basic Input/Output console interface for the math engine
#include <vector>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "engine/engine.h"
static char _kbuffer[512];
int main() {
mathengine::set_trigmode(mathengine::TRIGMODE_DEGREES);
while (true) {
std::cout << "> ";
fgets(_kbuffer, 512, stdin);
std::string s(_kbuffer);
s.erase(s.end() - 1);
std::vector<mathengine::token> lex = mathengine::tokenize_infix(s);
std::vector<mathengine::token> rpn = mathengine::create_rpn(lex);
long double result;
try {
result = mathengine::evaluate_rpn(rpn);
std::cout << std::to_string(result) << std::endl << std::endl;
}
catch (mathengine::exc_divbyzero exc) {
std::cout << "Error: " << exc.what() << std::endl << std::endl;
}
catch (mathengine::exc_nonintegral exc) {
std::cout << "Error: " << exc.what() << std::endl << std::endl;
}
catch (mathengine::exc_nonreal exc) {
std::cout << "Error: " << exc.what() << std::endl << std::endl;
}
catch (std::out_of_range) {
std::cout << "Error: Syntax error" << std::endl << std::endl;
}
}
}