-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·96 lines (89 loc) · 2.38 KB
/
main.cpp
File metadata and controls
executable file
·96 lines (89 loc) · 2.38 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
93
94
95
96
// main.cpp Andrew Levy Matthew Puentes
#include "main.h"
int main(int argc, char* argv[]) {
using namespace std;
if (argc <= 1) {
cout << "Please give at least one argument" << endl;
exit(0);
}
for (int i = 1; i < argc; i++) {
ifstream input;
input.open(argv[i]);
if (!input) {
perror("unable to open file");
}
string readLine;
while (true) {
getline(input, readLine);
if (readLine != "") {
stringstream line;
line << readLine;
Rational first;
Rational second;
string oper;
bool booleanReturn = false;
cout << readLine << "\t: ";
try {
line >> first;
bool booleanReturnValue = false;
while (!line.eof()) {
line >> second >> oper;
if (booleanReturn) {
throw operation_on_boolean_exception();
}
if (oper == "+") {
first = (first + second);
} else if (oper == "-") {
first = (first - second);
} else if (oper == "*") {
first = (first * second);
} else if (oper == "/") {
if (second == 0) {
throw devide_by_zero_exception();
}
first = (first / second);
} else if (oper == "==") {
booleanReturnValue = (first == second);
booleanReturn = true;
} else if (oper == "!=") {
booleanReturnValue = (first != second);
booleanReturn = true;
} else if (oper == "<") {
booleanReturnValue = (first < second);
booleanReturn = true;
} else if (oper == ">") {
booleanReturnValue = (first > second);
booleanReturn = true;
} else if (oper == "<=") {
booleanReturnValue = (first <= second);
booleanReturn = true;
} else if (oper == ">=") {
booleanReturnValue = (first >= second);
booleanReturn = true;
}
line >> std::ws;
}
if (booleanReturn) {
if (booleanReturnValue)
cout << "True" << endl;
else
cout << "False" << endl;
} else
cout << first << " (double:" << first.toDouble() << ")"
<< endl;
} catch (const invalid_input_exception& e) {
cout << "Error: " << e.what() << endl;
} catch (const devide_by_zero_exception& e) {
cout << "Error: " << e.what() << endl;
} catch (const operation_on_boolean_exception& e) {
cout << "Error: " << e.what() << endl;
}
} else {
break;
}
}
input.close();
}
cout << "program finished" << endl;
return 0;
}