/
MatchParenthesis.cpp
87 lines (77 loc) · 1.6 KB
/
MatchParenthesis.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
#include <stack>
#define my_swap(x,y) int t;t=x;x=y;y=t;
#include <iostream>
#include <string>
#include <vector>
const bool matchParenthesis(const std::string & sentence)
{
std::stack<char> parenthesisStack;
//std::vector <std::pair<char, char> > parenthesisPair;
std::pair<char, char> common('(', ')');
std::pair<char, char> curly('{', '}');
std::pair<char, char> square('[', ']');
std::vector <std::pair<char, char>> parenthesisPairList{ common, curly, square };
std::vector <int> intVector { 23, 43, 12 };
std::vector <std::string> strVector{ "First", "Second", "Third" };
//parenthesisPair.push_back(common);
//parenthesisPair.push_back(curly);
//parenthesisPair.push_back(square);
for (char i : sentence)
{
std::cout << i;
switch (i)
{
case '{':
case '(':
case '[':
parenthesisStack.push(i);
break;
case '}':
char top;
top = parenthesisStack.top();
if (top == '{')
{
parenthesisStack.pop();
}
else
{
return false;
}
break;
case ']':
top = parenthesisStack.top();
if (top == '[' )
{
parenthesisStack.pop();
}
else
{
return false;
}
break;
case ')':
top = parenthesisStack.top();
if (top == '(')
{
parenthesisStack.pop();
}
else
{
return false;
}
break;
default:
break;
//std::cout << "No parenthesis " << std::endl;
}
}
return (parenthesisStack.empty());
}
int main_matchparenthesis(int argc, char* argv[])
{
std::string sentence;
std::getline(std::cin, sentence);
std::cout << " Sentence is " << sentence << std::endl;
matchParenthesis(sentence);
return 0;
}