forked from nragland37/cpp-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (127 loc) · 4.82 KB
/
Copy pathmain.cpp
File metadata and controls
163 lines (127 loc) · 4.82 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
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
//*****************************************************************************************************
//
// This program reads in a file of RPN expressions and instantiates an array-based list stack of
// integers to store and evaluate the expressions.
// RPN (Reverse Polish Notation) is a postfix notation where the operator follows the operands.
// For example, the infix expression (3 + 4) * 5 is written in RPN as 3 4 + 5 *.
//
// Other files required:
// 1. stack.h - header file for the Stack class
// 2. expressions.txt - file containing RPN expressions
//
//*****************************************************************************************************
#include "stack.h"
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
//*****************************************************************************************************
void outputToken(char token[], ofstream &resultsFile);
void processNumber(char token[], Stack<int> &intStack, ofstream &resultsFile);
bool processOperator(char token[], Stack<int> &intStack, ofstream &resultsFile);
void display(Stack<int> &intStack, ofstream &resultsFile, bool &isValid);
void resetFlagStack(Stack<int> &intStack, bool &isValid);
//*****************************************************************************************************
int main() {
Stack<int> intStack;
bool isValid = true;
char token[5];
ifstream in("expressions.txt");
ofstream out("results.txt");
while (in >> token) {
outputToken(token, out);
if (token[0] == ';') {
display(intStack, out, isValid);
resetFlagStack(intStack, isValid);
} else if ((isValid) && (isdigit(token[0]))) {
processNumber(token, intStack, out);
} else if ((isValid) && (ispunct(token[0]))) {
isValid = processOperator(token, intStack, out);
}
}
in.close();
out.close();
return 0;
}
//*****************************************************************************************************
void outputToken(char token[], ofstream &resultsFile) {
resultsFile << endl << "(Token: " << token << ")\t\t";
if (token[0] != ';')
cout << token << " ";
}
//*****************************************************************************************************
void processNumber(char token[], Stack<int> &intStack, ofstream &resultsFile) {
int num;
num = atoi(token);
intStack.push(num);
resultsFile << "Push " << num;
}
//*****************************************************************************************************
bool processOperator(char token[], Stack<int> &intStack, ofstream &resultsFile) {
bool success = false;
int op1,
op2,
result;
if ((intStack.pop(op2)) && (intStack.pop(op1))) {
resultsFile << "Pop " << op2 << "\tPop " << op1;
switch (token[0]) {
case '*':
result = op1 * op2;
break;
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '/':
result = op1 / op2;
break;
case '%':
result = op1 % op2;
break;
}
intStack.push(result);
resultsFile << "\tPush " << result;
success = true;
}
return success;
}
//*****************************************************************************************************
void display(Stack<int> &intStack, ofstream &resultsFile, bool &isValid) {
int result;
if (isValid) {
if (intStack.getNumValues() == 1) {
intStack.pop(result);
resultsFile << "Pop " << result << "\n\t\tValid: result = " << result << "\n" << endl;
cout << "= " << result << endl;
} else if (intStack.getNumValues() > 1) {
resultsFile << "\n\t\tInvalid RPN expression - too many operands\n" << endl;
cerr << "\t\tinvalid\n";
isValid = false;
}
} else {
resultsFile << "\n\t\tInvalid RPN expression - not enough operands\n" << endl;
cerr << "\t\tinvalid\n";
}
}
//*****************************************************************************************************
void resetFlagStack(Stack<int> &intStack, bool &isValid) {
int popVal;
while (intStack.pop(popVal));
isValid = true;
}
//*****************************************************************************************************
/*
2 4 * 5 + = 13
13 5 % 5 + = 8
15 1 + 2 / 1 - = 7
15 + 1 + 2 / 1 - invalid
3 4 + 15 10 - * = 35
3 4 + 6 15 10 - * invalid
2 13 + 14 6 - - 5 * 4 + = 39
35 6 4 2 2 / + * - = 5
3 4 + 1 2 - * 4 2 / 3 - + = -8
3 14 1 2 4 2 3 + % * + - + = 8
*/