-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpara_match.cpp
More file actions
59 lines (50 loc) · 983 Bytes
/
Copy pathpara_match.cpp
File metadata and controls
59 lines (50 loc) · 983 Bytes
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
#define MAX 100
int is_opening ( char ch )
{
if ( ch == '(' || ch == '[' || ch == '{' ) return 1 ;
else return 0 ;
}
int is_closing ( char ch )
{
if ( ch == ')' || ch == ']' || ch == '}' ) return 1 ;
else return 0 ;
}
char counter ( char ch )
{
if ( ch == ')' ) return '(' ;
else if ( ch == ']' ) return '[' ;
else if ( ch == '}' ) return '{' ;
}
int isempty ( int top )
{
if ( top == -1 ) return 1 ;
else return 0 ;
}
void push ( char A[] , int *top , char data )
{
A[++(*top)] = data ;
}
char pop ( char A[] , int *top )
{
char data = A[*top] ;
--(*top);
return data ;
}
extern int para_match_test ( char expr[] )
{
char ch ;
int top = -1 , ctrl = -1 , i = 0 ;
char A[MAX] ;
while (( ch = expr[i++] ) != '\0' ){
if ( is_opening (ch) ) {
push ( A , &top , ch ) ;
}
if ( is_closing (ch) ){
if ( ! ( pop ( A , &top ) == counter ( ch ) ) ){
return 0 ;
}
}
}
if ( isempty ( top ) ) return 1 ;
else return 0 ;
}