-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexec16-9.cpp
61 lines (51 loc) · 1.63 KB
/
exec16-9.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
#include <sstream>
#include "GUI.h"
#include "Window.h"
#include "ch07/calculator_v2/lexer.h"
#include "ch07/calculator_v2/parser.h"
using namespace Graph_lib;
class Calculator_window : public Graph_lib::Window {
public:
Calculator_window(Point xy, int w, int h, const string& title)
:Graph_lib::Window(xy, w, h, title),
ts(iss),
parser(ts),
expr_input(Point(100, 10), w - 200, 20, "Expression:"),
result_output(Point(100, 40), w - 200, 20, "Result:"),
calculate_button(Point(w - 80, 10), 70, 20, "=",
[](Address, Address pw) { reference_to<Calculator_window>(pw).calculate(); }),
quit_button(Point(w - 80, 40), 70, 20, "Quit",
[](Address, Address pw) { reference_to<Calculator_window>(pw).quit(); }) {
attach(expr_input);
attach(result_output);
attach(calculate_button);
attach(quit_button);
}
private:
void calculate() {
iss.clear();
iss.str(expr_input.get_string() + ";");
ts.ignore();
std::ostringstream oss;
try {
oss << parser.statement();
}
catch (std::exception& e) {
oss << "error: " << e.what();
}
result_output.put(oss.str());
}
void quit() { hide(); }
std::istringstream iss;
Token_stream ts;
Parser parser;
In_box expr_input;
Out_box result_output;
Button calculate_button;
Button quit_button;
};
// Simple calculator - GUI version
int main() {
Calculator_window win(Point(100, 100), 400, 70, "Simple calculator");
return gui_main();
}