-
Notifications
You must be signed in to change notification settings - Fork 84
/
Check paranthesis expression.cpp
66 lines (55 loc) · 1.64 KB
/
Check paranthesis expression.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
#include <bits/stdc++.h>
using namespace std;
char input[1000];
int Stack[1000], ind;
void push (int x) {
Stack[++ind] = x;
}
bool isEmpty() {
if (ind > 0) return false;
else return true;
}
bool verify (char input[]) {
ind = 0;
int n = strlen(input);
for (int i=0; i<n; ++i) {
if (input[i] == '(') push(1);
if (input[i] == '[') push(2);
if (input[i] == '{') push(3);
if (input[i] == ')') {
// if the stack is empty, or the last parenthesis is different
// than the one we are closic, then the input is wrong
if (isEmpty() || Stack[ind] != 1) return false;
else {
Stack[ind] = 0;
--ind;
}
}
if (input[i] == ']') {
// if the stack is empty, or the last parenthesis is different
// than the one we are closic, then the input is wrong
if (isEmpty() || Stack[ind] != 2) return false;
else {
Stack[ind] = 0;
--ind;
}
}
if (input[i] == '}') {
// if the stack is empty, or the last parenthesis is different
// than the one we are closic, then the input is wrong
if (isEmpty() || Stack[ind] != 3) return false;
else {
Stack[ind] = 0;
--ind;
}
}
}
if (ind == 0) return true; // if the stack is empty, then the input is OK
else return false;
}
int main()
{
cin>>input;
cout<<verify (input);
return 0;
}