-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (39 loc) · 1014 Bytes
/
main.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
#include <iostream>
#include "ExpressionParser.h"
#include "ParseTree.h"
#include "DivideByZeroException.h"
#include "InvalidDerivationException.h"
int main (void)
{
std::string expr_string;
std::cout << "Enter an expression (enter QUIT to exit):" << std::endl;
std::getline (std::cin, expr_string);
ExpressionParser * parser = new ExpressionParser ();
ParseTree * parseTree;
while (expr_string != "QUIT")
{
int result = 0;
try
{
parseTree = parser->parse (expr_string);
result = parseTree->evaluate ();
}
catch (InvalidDerivationException & e)
{
std::cout << e.what () << std::endl;
std::getline (std::cin, expr_string);
continue;
}
catch (DivideByZeroException & e)
{
std::cout << e.what () << std::endl;
std::getline (std::cin, expr_string);
delete parseTree;
continue;
}
std::cout << result << std::endl;
std::getline (std::cin, expr_string);
delete parseTree;
}
delete parser;
}