-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisCurlyBracesMatched.cpp
200 lines (171 loc) · 4.64 KB
/
isCurlyBracesMatched.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
#include "Linked Stack.h"
#include <iostream> // I/O definitions
#include <vector>
#include <fstream>
#include <string>
using namespace std; // make std:: accessible
int countSpaces(string &inputStr);
void print(std::vector<string> &input);
string getNumberOfLine(string &inputStr);
vector<string> getCurlyBrace()
{
vector<string> braces; // store curly braces lines
ifstream fin("a.cpp");
int lineCount = 0;
string line;
string str;
int spaces = 0;
while (getline(fin, line)) // read until end of file
{
lineCount++;
str = "";
int bOpen = line.find("{", 0); // scan open curly braces
int bClose = line.find("}", 0); // scan closed curly braces
if (bOpen > -1) // Found open braces
{
spaces = countSpaces(line); // get spaces from line
for (int i = 0; i < spaces; i++)
{
str = str + " ";
}
str = str + "{" + to_string(lineCount); // add line number to braces vector
braces.push_back(str);
str = "";
spaces = 0;
}
if (bClose > -1)
{
spaces = countSpaces(line);
for (int i = 0; i < spaces; i++)
{
str = str + " ";
}
str = str + "}" + to_string(lineCount);
braces.push_back(str);
str = "";
spaces = 0;
}
}
return braces; // return vector of braces
}
bool isCurlyBracesMatched(const vector<string> &braces)
{
string wrongCurlyBraces;
bool returnValue = true; //To check the return value
string str;
int bOpen;
int bClose;
LinkedStack S; // stack for opening braces
typedef vector<string>::const_iterator Iter; // iterator type
// iterate through vector
for (Iter p = braces.begin(); p != braces.end(); ++p)
{
bOpen = p->find("{", 0);
if (bOpen > -1) // is curly braces opened ?
{
S.push(*p);
}
else
{
if (S.empty())
{
wrongCurlyBraces = *p;
cout << "Attempts to close an unopened parenthesis in line " << getNumberOfLine(wrongCurlyBraces) << endl;
returnValue = false;
}
else
{
bClose = p->find("}", 0);
string tempStr = S.top(); // store the string at the top of the stack
int len = countSpaces(tempStr); // get spaces length from the top of the stack
if ((bClose > -1 && len == bClose))
{
S.pop(); //pop if opened braces are closed
}
else if (bClose > -1 && bClose > len)
{
wrongCurlyBraces = *p;
cout << "Attempts to close an unopened parenthesis in line " << getNumberOfLine(wrongCurlyBraces) << endl;
returnValue = false; //sets false the return if spaces do not match
}
else if (bClose > -1 && bClose < len)
{
wrongCurlyBraces = S.top();
cout << "Attempts to open an unclosed parentheses in line " << getNumberOfLine(wrongCurlyBraces) << endl;
S.pop(); //error found and used pop for continue the search
p--; //to check again current p
returnValue = false; //sets false the return if spaces do not match
}
}
}
}
while (!S.empty()) //if the stack is not empty
{
wrongCurlyBraces = S.top();
cout << "Attempts to open an unclosed parentheses in line " << getNumberOfLine(wrongCurlyBraces) << endl;
S.pop();
returnValue = false; //sets false the return if the stack is not empty
}
return returnValue;
}
string getNumberOfLine(string &inputStr) // Gets number of the braces line for add braces vector
{
int len;
int bOpen = -1;
int bClose = -1;
string line = "";
len = inputStr.length();
bOpen = inputStr.find("{", 0);
bClose = inputStr.find("}", 0);
if (bOpen > -1)
{
for (int i = (bOpen + 1); i < len; i++)
{
line = line + inputStr[i];
}
}
else if (bClose > -1)
{
for (int i = (bClose + 1); i < len; i++)
{
line = line + inputStr[i];
}
}
return line;
}
int countSpaces(string &inputStr) // counts the spaces of the entered string up to curly braces
{
int num = 0;
char space = ' ';
const char TAB = '\t';
for (int i = 0; i < inputStr.length(); i++)
{
if (inputStr[i] == '{' || inputStr[i] == '}')
{
return num;
}
if (inputStr[i] == space)
{
num++;
}
else if (inputStr[i] == TAB)
{
num += 4;
}
}
return 0;
}
int main()
{
if (isCurlyBracesMatched(getCurlyBrace()))
{
cout << endl
<< "MATCHED" << endl;
}
else
{
cout << endl
<< "NOT MATCHED" << endl;
}
return 0;
}