-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
227 lines (205 loc) · 6.96 KB
/
main.cpp
File metadata and controls
227 lines (205 loc) · 6.96 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
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
/*
Ramamurthy Sundar
main.cpp
Driver for the program. This programs will showcase
the usefullness of stacks as a data structure.
There are 3 operations this driver will perform. First, it will reverse a
string using a stack and check to see whether the reverse is the same as
the original string. Second, it will use stacks to convert a base 10
number into a number between base 2 and base 16 inclusive. Third, it will
check to see whether the paranthesis in a text file are balanced.
All of the terminal communication is streamed to a text file.
*/
#include "class.h"
using namespace std;
//helper functions
//1. reverse a string with a stack
template <typename T>
string reverse_string(StudentStack<T> &stack, string inputstring) {
//initial loop to create stack of inputstring
int sz = inputstring.size();
for (int i = 0; i < sz; i++) {
stack.push(inputstring[i]);
}
//creating the string and popping from the stack
for (int i = 0; i < sz; i++) {
inputstring[i] = stack.front();
stack.pop();
}
return inputstring;
}
//2. convert an input base ten number into a number of another base between 2 and 16
//outputs a string as the in order to account for hexidemical
template <typename T>
string multibaseOutput(StudentStack<T> &stack, int decimal, const int base) {
int remainder;
while (decimal != 0) {
remainder = decimal % base;
decimal /= base;
stack.push(remainder);
}
//pop from the stack and push to the string, which will be converted to an int
//creating the string and popping from the stack
string solutionstring; char ASCII; int solution;
int sz = stack.size();
for (int i = 0; i < sz; i++) {
//Case 1 - Base is 16
if (base == 16) {
//stack.front() > 9 and < 16
if (stack.front() == 10) {
ASCII = 'A';
solutionstring.push_back(ASCII);
stack.pop();
}
else if (stack.front() == 11) {
ASCII = 'B';
solutionstring.push_back(ASCII);
stack.pop();
}
else if (stack.front() == 12) {
ASCII = 'C';
solutionstring.push_back(ASCII);
stack.pop();
}
else if (stack.front() == 13) {
ASCII = 'D';
solutionstring.push_back(ASCII);
stack.pop();
}
else if (stack.front() == 14) {
ASCII = 'E';
solutionstring.push_back(ASCII);
stack.pop();
}
else if (stack.front() == 15) {
ASCII = 'F';
solutionstring.push_back(ASCII);
stack.pop();
}
//stack.front() < 9
else {
ASCII = '0' + stack.front(); //convert int to ASCII charachter
solutionstring.push_back(ASCII);
stack.pop();
}
}
//Case 2 - Base is not 16
else {
ASCII = '0' + stack.front(); //convert int to ASCII charachter
solutionstring.push_back(ASCII);
stack.pop();
}
}
return solutionstring;
}
//3. tell whether the paranthesis of a text file are balanced
template <typename T>
bool are_paranthesis_balanced(StudentStack<T> &stack, string inputstring) {
for (unsigned int i = 0; i < inputstring.size(); i++) {
if (inputstring[i] == '(' || inputstring[i] == '{' || inputstring[i] == '[') {
stack.push(inputstring[i]);
}
if (inputstring[i] == ')' || inputstring[i] == '}' || inputstring[i] == ']')
{
//right paranthesis without a left pair
if (stack.isEmpty()) {
return 0;
}
/* Pop the top element from stack, if it is not a pair
bracket of character then there is a mismatch.
This will happen for expressions like {(}) */
else if (!matching_pair(stack.front(), inputstring[i])) {
return 0;
}
// pop from the stack if the pair is complete
stack.pop();
}
}
//case 1: The stack was balanced
if (stack.isEmpty()) return 1;
//case 2: The stack is not ballanced;
else return 0;
}
//4. check whether a pair of paranthesis are matching
bool matching_pair(const char char1, const char char2) {
if (char1 == '(' && char2 == ')')
return 1;
else if (char1 == '{' && char2 == '}')
return 1;
else if (char1 == '[' && char2 == ']')
return 1;
else
return 0;
}
//main
int main() {
//create file stream
ofstream outfile;
outfile.open("OutputFile.txt");
//1. charachter reversal
StudentStack<char> ForwardStack;
string inputString; string reverseString;
cout << "Input a string in the form STRING1#STRING2 or quit to terminate: ";
cin >> inputString;
outfile << "Input a string in the form STRING1#STRING2 or quit to terminate: " << inputString;
while (inputString != "quit") {
reverseString = reverse_string(ForwardStack, inputString);
//check to see whether the two stacks are the same and outputs to terminal
if (inputString == reverseString) {
cout << inputString << " matches the pattern" << endl << endl;
outfile << "\n";
outfile << inputString << " matches the pattern" << "\n" << "\n";
}
else {
cout << inputString << " does not match the pattern" << endl << endl;
outfile << "\n";
outfile << inputString << " does not match the pattern" << "\n" << "\n";
}
//next round
cout << "Input a string in the form STRING1#STRING2 or quit to terminate: ";
cin >> inputString;
outfile << "Input a string in the form STRING1#STRING2 or quit to terminate: " << inputString;
}
//2. base converter
cout << endl;
outfile << "\n" << "\n";
StudentStack<int> NewBaseStack;
int decimal; int base;
cout << "Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: ";
cin >> decimal >> base;
outfile << "Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: " << decimal << " " << base;
while (decimal != 0 && base != 0) {
string solution = multibaseOutput(NewBaseStack, decimal, base);
cout << "\t" << decimal << " base " << base << " is " << solution << endl;
outfile << "\n";
outfile << "\t" << decimal << " base " << base << " is " << solution << "\n";
cout << "Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: ";
cin >> decimal >> base;
outfile << "Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: " << decimal << " " << base;
}
//3. Check whether paranthesis are balanced in a text file
cout << endl;
outfile << "\n" << "\n";
ifstream infile; string filename;
StudentStack<char> ParanthesisStack;
cout << "Enter the file name : ";
cin >> filename;
outfile << "Enter the file name : " << filename;
infile.open(filename);
string equation;
while (!infile.eof()) {
getline(infile, equation);
if (!are_paranthesis_balanced(ParanthesisStack, equation)) {
cout << "The symbols in " << filename << " are not balanced." << endl;
outfile << "\n";
outfile << "The symbols in " << filename << " are not balanced." << "\n";
}
}
if (are_paranthesis_balanced(ParanthesisStack, equation)) {
cout << "The symbols in " << filename << " are balanced." << endl;
outfile << "\n";
outfile << "The symbols in " << filename << " are balanced." << "\n";
}
outfile.close();
return 0;
}