-
Notifications
You must be signed in to change notification settings - Fork 0
/
big_calc.cpp
113 lines (107 loc) · 2.21 KB
/
big_calc.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
#include <vector>
#include <string>
#include <cstdlib>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include "parser.h"
#include "base_node.h"
#include "big_calc.h"
class bad_file_exception : public std::runtime_error
{
public:
bad_file_exception(const std::string& msg) : std::runtime_error(msg) { }
};
int interactive_mode()
{
std::string line;
base_node* expr = NULL;
while(!std::getline(std::cin, line).eof() && line != "q")
{
try {
expr = parser::parse(line);
}
catch(parse_exception& e)
{
std::cerr << "parse pony is very sad :-(\nerror: " << e.what() << std::endl;
if(expr != NULL);
delete expr;
expr = NULL;
}
if(expr != NULL)
{
std::cout << "Result: " << expr->eval() << std::endl;
delete expr;
expr = NULL;
}
}
return 0;
}
int file_input_mode(const std::string& filename)
{
std::ifstream input(filename.c_str());
if(input.fail())
throw bad_file_exception("either file doesn't exist, or you don't have enough permission for reading it");
std::string line;
base_node* expr;
int expr_n = 1;
while(!input.eof() && !input.fail())
{
std::cout << "expression " << expr_n << std::endl;
std::getline(input, line);
try {
expr = parser::parse(line);
}
catch(parse_exception& e)
{
std::cerr << "parse pony is very sad :-(\n problem with expression " << expr_n << ": "<< e.what() << std::endl;
if(expr != NULL)
delete expr;
expr = NULL;
}
if(expr != NULL)
{
std::cout << expr->eval() << std::endl;
delete expr;
expr = NULL;
}
expr_n++;
}
if(input.fail())
{
input.close();
throw bad_file_exception("There was a problem while I've been reading the file - it's probably corrupted");
}
return 0;
}
int main(int argc, char** argv)
{
std::string line;
int ret;
if(argc == 1)
{
ret = interactive_mode();
}
else if(argc == 2)
{
std::string filename(argv[1]);
try {
ret = file_input_mode(filename);
}
catch(bad_file_exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
}
else
{
std::cerr << "bad number of arguments, please refer to documentation for the right arguments\n";
return 1;
}
return ret;
/*big_num a("-1");
big_num b("2.1");
std::cout << a + b << std::endl;
return 0;*/
}