-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
92 lines (85 loc) · 2.18 KB
/
Calculator.cpp
File metadata and controls
92 lines (85 loc) · 2.18 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
#include "./Calculator.h"
#include "./Parser.h"
#include "./Exception.h"
#include "./Command.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace gcalc;
using namespace std;
Calculator::Calculator()
{
}
void Calculator::interactive()
{
Parser parse = Parser();
std::string cmd;
while (!cin.eof())
{
std::cout << "Gcalc> ";
std::getline(std::cin, cmd);
try
{
if (parse.trim(cmd, " ") == "quit")
{
break;
}
parse.command(cmd, graphs, IContextParams());
}
catch (gcalc::Exception *e)
{
std::cout << "Error: " << e->what() << std::endl;
}
catch (gcalc::Exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
}
}
void Calculator::batch(std::string input, std::string output)
{
Parser parse = Parser();
std::ifstream in(input);
std::streambuf *cinbuf = std::cin.rdbuf();
std::cin.rdbuf(in.rdbuf());
std::ofstream out(output);
std::streambuf *coutbuf = std::cout.rdbuf();
std::cout.rdbuf(out.rdbuf());
if (in.good() && out.good())
{
std::string cmd;
while (std::getline(std::cin, cmd))
{
try
{
if (cmd.find("\r") != string::npos && cmd.find("\r") == cmd.size() - 1)
{
cmd = cmd.substr(0, cmd.find("\r"));
}
if (parse.trim(cmd, " ") == "quit")
{
in.close();
out.close();
break;
}
parse.command(cmd, graphs, IContextParams());
}
catch (gcalc::Exception *e)
{
std::cout << "Error: " << e->what() << std::endl;
}
catch (gcalc::Exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
}
}
else
{
std::cin.rdbuf(cinbuf);
std::cout.rdbuf(coutbuf);
throw Exception("Could not read/write to files");
}
std::cin.rdbuf(cinbuf);
std::cout.rdbuf(coutbuf);
}