-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
268 lines (212 loc) · 7.58 KB
/
Copy pathmain.cpp
File metadata and controls
268 lines (212 loc) · 7.58 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
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
/*
#include <iostream>
#include <fstream>
#include <istream>
#include <stack> //added
#include <string> //added
#include <iomanip> //added
using namespace std;
/* Pair
[ and ]
( and )
{ and }
' and '
" and "
/* and
void printFile(istream& dictfile) {
int index = 1;
while (!dictfile.eof()) {
string currentLine;
getline(dictfile, currentLine);
cout << right << setw(2) << index++;
cout << left << " " << currentLine << endl;
}
dictfile.clear();
dictfile.seekg(0, dictfile.beg);
}
void printMatching(char closing) {
string opening = "Q";
if (closing == ')') opening = '(';
else if (closing == ']') opening = '[';
else if (closing == '}') opening = '{';
cout << "pair matching " << opening << " and " << closing << endl;
}
bool matchingBraces(char closing, char opening) { //reference so we don't create a stack copy -- inefficient
char neededOpening = 'Q';
if (closing == ')') neededOpening = '(';
else if (closing == ']') neededOpening = '[';
else if (closing == '}') neededOpening = '{';
else if (closing == '>') neededOpening = '<'; // FOR BLOCK COMMENTS
if (opening == neededOpening)
return true;
else
return false;
}
int findLastQuoteIndex(string currentLine, int CurrentQuoteIndex, char quoteType) { //pass in ' or "
int lastQuoteIndex = -1;
for (int i = CurrentQuoteIndex + 1; i < currentLine.size(); i++) {
if (currentLine[i] == quoteType)
return i;
//lastQuoteIndex = i;
}
return lastQuoteIndex;
}
bool balanceFile(istream& dictfile) {
stack<char> stk; //INVARIANT:
stack<int> lineNumbers; //POP AND PUSH STACKS AT THE SAME TIME
string currentLine;
int numCurrentLine = 0;
bool match = true;
//HANDLES MULTI-LINE COMMENTS -- NEEDED OUTSIDE WHILE LOOP B/C TRACKS MULTIPLE LINES IN THE FILE
bool inBlockComment = false;
bool inQt = false;
while (getline(dictfile, currentLine)) {
numCurrentLine++;
char popFromStack;
// Handling single-line and multi-line comments
bool continueLine = true;
//Handling Quotation marks
bool checkedSingleQt = false;
bool checkedDoubleQt = false;
int lastDoubleQtIndx = -1;
int lastSingleQtIndx = -1;
for (int i = 0; i < currentLine.size(); i++) {
//Check for in-line comments and block comments
// Make sure that you can check one character ahead in the line
if (i < currentLine.size() - 1) {
if (currentLine[i] == '/') {
if (currentLine[i + 1] == '*' && !inQt) {
stk.push('<'); // Send /* as <
lineNumbers.push(numCurrentLine);
inBlockComment = true;
continue;
}
else if (currentLine[i + 1] == '/') {
continueLine = false; // Break out of reading this line if //
continue;
}
}
else if (currentLine[i] == '*' && currentLine[i + 1] == '/' && !inQt) {
if (stk.empty()) {
popFromStack = 0;
}
else {
popFromStack = stk.top();
stk.pop();
lineNumbers.pop();
}
if (!matchingBraces('>', popFromStack)) {
cout << "unmatched " << "" << " on line " << numCurrentLine;
}
else {
cout << "pair matching /* and " << endl;
inBlockComment = false;
}
}
}
if (!continueLine)
break;
//HANDLING QUOTATION MARKS and Looking For Block Comment
if (i < lastDoubleQtIndx || i < lastSingleQtIndx) {
inQt = true;
continue;
}
else
inQt = false;
if (currentLine[i] == '\'' && !checkedSingleQt) {
checkedSingleQt = true;
stk.push('\'');
lineNumbers.push(numCurrentLine);
lastSingleQtIndx = findLastQuoteIndex(currentLine, i, currentLine[i]);
if (lastSingleQtIndx < 1)
match = false;
else {
stk.pop();
lineNumbers.pop();
cout << "pair matching \' and \'" << endl;
}
}
if (currentLine[i] == '\"' && !checkedDoubleQt) {
checkedDoubleQt = true;
stk.push('\"');
lineNumbers.push(numCurrentLine);
lastDoubleQtIndx = findLastQuoteIndex(currentLine, i, currentLine[i]);
if (lastDoubleQtIndx < 1)
match = false;
else {
stk.pop();
lineNumbers.pop();
cout << "pair matching \" and \"" << endl;
}
}
//END OF: HANDLING QUOTATION MARKS
//HANDLING BRACES
switch (currentLine[i]) {
case '(':
case '{':
case '[':
stk.push(currentLine[i]);
lineNumbers.push(numCurrentLine);
continue;
}
switch (currentLine[i]) {
case ')':
case '}':
case ']':
if (stk.empty()) {
match = false;
cout << "unmatched " << currentLine[i] << " on line " << numCurrentLine;
break;
}
if (!stk.empty()) {
popFromStack = stk.top();
stk.pop();
lineNumbers.pop();
}
if (!matchingBraces(currentLine[i], popFromStack)) {
match = false;
cout << "unmatched " << currentLine[i] << " on line " << numCurrentLine;
break;
}
printMatching(currentLine[i]);
}
//END OF: HANDLING BRACES
}
if (!match)
break;
}
if (!stk.empty()) {
match = false;
string mismatch = string(1, stk.top());
int mismatchedLine = lineNumbers.top();
switch (stk.top()) { // IDK how else to make this print properly.
case '<':
mismatch = "/*";
break;
case'\'':
mismatch = "\'";
break;
case '\"':
mismatch = '\"';
break;
}
cout << "unmatched " << mismatch << " on line " << mismatchedLine << endl;
}
return match;
}
int main()
{
ifstream infile;
string filename;
cout << "Please enter filename for C++ code: ";
cin >> filename;
infile.open(filename.c_str());
if (!infile) {
cout << "File not found!" << endl;
return (1);
}
printFile(infile);
if (balanceFile(infile))
cout << "balance ok" << endl;
}
*/