-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (53 loc) · 1.54 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include <random> // TODO Remove when done testing
#include "calculate.h"
#include "mixednumber.h"
#include "parseexception.h"
#include "parser.h"
using namespace std;
void printIntroMessage();
void doCalculations();
int main()
{
printIntroMessage();
doCalculations();
return 0;
}
void printIntroMessage() {
cout << "Mixed Number RPN Calculator" << endl
<< "Enter an algebraic expression consisting of" << endl
<< " integers, fractions, decimal numbers, standard" << endl
<< " mathematical operators (+, -, *, /) and parentheses." << endl;
cout << "Your algebraic expression will be repeated in" << endl
<< " Reverse Polish Notation, and the result will be" << endl
<< " displayed." << endl;
cout << endl;
}
void doCalculations() {
string expression;
vector<string> expressionBits;
string result;
while(true) {
cout << "EXPRESSION: ";
getline(cin, expression);
try {
expressionBits = parser::parseToRPN(expression);
result = calculate::getAnswer(expressionBits);
for(unsigned int i = 0; i < expressionBits.size(); i++) {
cout << expressionBits.at(i) << " ";
}
cout << "= ";
cout << result << endl;
} catch (parseexception& e) {
cout << "ERROR: " << e.what() << endl;
} catch (fraction::ERRORS e) {
if(e == fraction::DIVIDE_BY_ZERO) {
cout << "ERROR: Cannot divide by zero" << endl;
}
} catch(...) {
cout << "ERROR: Unknown error." << endl;
}
cout << endl;
}
}