-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpHonors.cpp
304 lines (258 loc) · 10.5 KB
/
pHonors.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
pHonors.cpp
Course: CS 2433, C/C++ Programming
Semester: Summer 2020
Assignment: Program 8 with honors contract (infix calculator)
Instructor: Richard Churchill
Due Date/Time: 30 July 2020, 11:59 PM
Submitted:
Written by: Max DeSantis
Build Instructions:
g++ -o pHonors pHonors.cpp
Compiled and working with MinGW 6.3.0 (on Windows) and GCC 9.1.1 (on csx3)
Usage:
Use <program_name> --help to display usage information.
Use <program_name> -rpn to begin a Reverse Polish Notation calculator.
Operators +, -, *, /, **, sin, cos, asin, acos, swap, copy, ~ (negation),
' (inversion), peek, dump, and = are supported.
Define constants with ::<letter><letters and numbers>
Define variables with :$<letter><letters and numbers>
Example input: 1.0 2.0 + =
Example output: 3.0000000
*/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "honorsCalc.h"
using namespace std;
int main(int argc, char* argv[]) {
/* Initializatiion */
calc calculator(argv[0]);
bool continuing = true;
/* Track what operation the user input on command line*/
enum Operation {none, rpn, infix};
Operation specifiedOp = none;
string keyWords[4] = {"-rpn", "-infix", "-i", "-o"};
/* Modular I/O streams. Cin and Cout by default. */
istream* inStream = &cin;
ostream* outStream = &cout;
/* Ensure only one file of each type is opened*/
bool inputDefined = false;
bool outputDefined = false;
try {
/* Check for sufficient number of arguments */
if(argc < 2) {
throw 1;
}
else {
/* Parse through command-line arguments looking for inputs/outputs/operation type */
for(int i = 1; i < argc; ++i) {
/* Check for which main operation should be performed. */
if(strcmp(argv[i], "--help") == 0) {
throw 3;
}
else if(strcmp(argv[i], "-rpn") == 0) {
if(specifiedOp == none) {
specifiedOp = rpn;
}
else {
throw 10;
}
}
else if(strcmp(argv[i], "-infix") == 0) {
if(specifiedOp == none) {
specifiedOp = infix;
}
else {
throw 10;
}
}
/* Check for a user-specified input file. */
else if(strcmp(argv[i], "-i") == 0) {
if(argc > i && !inputDefined) {
/* Check for reserved keywords following input specifier */
for(string temp : keyWords) {
if(strcmp(argv[i + 1], temp.c_str()) == 0) {
throw 5;
}
}
/* Will only execute if allowable name is present */
ifstream* newInStream = new ifstream(argv[i + 1]);
if(newInStream->is_open()) {
inStream = newInStream;
}
else {
throw 8; //Cannot find input file.
}
++i; //Skip past he file name in arguments list.
}
else {
throw 1; //Not enough arguments
}
}
/* Check for a user-specified output file. */
else if(strcmp(argv[i], "-o") == 0) {
if(argc > i) {
/* Check for reserved keywords following input specifier */
for(string temp : keyWords) {
if(strcmp(argv[i + 1], temp.c_str()) == 0) {
throw 6;
}
}
/* Will only execute if allowable name is present */
ofstream* newOutStream = new ofstream(argv[i + 1]);
if(newOutStream->is_open()) {
outStream = newOutStream;
}
else {
throw 9; //Cannot open output file.
}
++i; //Skip past the file name in arguments list.
}
else {
throw 1; //Not enough arguments
}
}
else {
throw 2;
}
}
calculator.setup(inStream, outStream);
if(specifiedOp == rpn) {
while(continuing) {
if(calculator.rpn()) { //Calculator reached execution end.
/* Gather and print execution results */
double result = calculator.expResultRpn();
*outStream << "Result = " << fixed << setprecision(5) << result << "\n";
/* Gather and print stack outcome*/
int size = calculator.stackSize();
*outStream << size << " values remain on the stack.\n";
continuing = false;
}
else { //Calculator error
char input;
string temp;
/* Gather and print error */
int errorCode = calculator.evalError();
cout << calculator.errorTextRpn(errorCode) << "\n";
/* "flush" input buffer if cin is being used as input */
if(inStream = &cin) {
cin.clear();
getline(cin, temp);
}
cout << "Calculation failed. Try again? [Y / N]: ";
/* Gather user response and act accordingly */
getline(cin, temp);
/* Check for valid input */
if(temp.size() > 0) {
input = tolower(temp.at(0));
if(input == 'y') {
calculator.resetCalc();
}
else if(input == 'n') {
continuing = false;
}
else {
throw 4;
}
}
else {
throw 4;
}
}
}
}
else if(specifiedOp == infix) {
while(continuing) {
if(calculator.infixInit()) {
/* Gather and print execution results */
double result = calculator.expResultInfix();
*outStream << "Result = " << fixed << setprecision(5) << result << endl;
continuing = false;
}
else {
char input;
string temp;
/* Gather and print error */
int errorCode = calculator.evalError();
cerr << calculator.errorTextInfix(errorCode) << "\n";
/* "flush" input buffer if cin is being used as input */
if(inStream = &cin) {
cin.clear();
getline(cin, temp);
}
cerr << "Calculation failed. Try again? [Y / N]: ";
/* Gather user response and act accordingly */
getline(cin, temp);
/* Check for valid input */
if(temp.size() > 0) {
input = tolower(temp.at(0));
if(input == 'y') {
calculator.resetCalc();
}
else if(input == 'n') {
continuing = false;
}
else {
throw 4;
}
}
else {
throw 4;
}
}
}
}
else { //No operation was specified.
throw 7;
}
}
/* Calculator exited cleanly.
Cleanup streams and terminate. */
return 0;
}
catch (int e) {
string returnText = "***ERROR: ";
switch (e) {
case 1:
returnText += "Insufficient number of command-line arguments.";
break;
case 2:
returnText += "Unrecognized command-line arguments.";
break;
case 3:
returnText = ""; //User input --help
break;
case 4:
returnText += "Unrecognized response - terminating.";
break;
case 5:
returnText += "Invalid input file name.";
break;
case 6:
returnText += "Invalid output file name.";
break;
case 7:
returnText += "Invalid operation specified.";
break;
case 8:
returnText += "Unable to locate specified input file.";
break;
case 9:
returnText += "Unable to open specified output file.";
break;
case 10:
returnText += "Too many operations described.";
break;
default:
returnText += "Unrecognized error code.";
break;
}
calculator.printUsage();
cerr << "\n" << returnText << endl;
/* Calculator exiting.
Cleanup streams and terminate. */
exit(-1);
}
}