-
Notifications
You must be signed in to change notification settings - Fork 1
/
parenthesis_mtch.c
49 lines (42 loc) · 982 Bytes
/
parenthesis_mtch.c
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
//
// parenthesis_mtch.cpp
// parenthesis_mtch
//
// Created by Wang Yi on 14/11/16.
// Copyright (c) 2016 Wang Yi. All rights reserved.
//
#include "parenthesis_mtch.h"
#define FOR(START, END) \
for (START=0; (START) < (END); (START)++){
#define END ;}
#define OPEN '('
#define CLOSE ')'
#define UNDEF '?'
int parenthesis_mtch(const char* array, int l)
{
int i;
int head = 0;
int* symbolTab_stack = Malloc(int, l);
char* out = Malloc(char, l);
FOR(i, l)
out[i] = UNDEF;
END
FOR(i, l)
if (array[i] == OPEN)
symbolTab_stack[head++] = i;
else if (array[i] == CLOSE && head !=0) {
out[symbolTab_stack[head-1]] = OPEN;
out[i] = CLOSE;
head--;
}
END
FOR(i, l)
//if (symbolTab_stack[i] != UNDEF)
printf("%c", out[i]);
END
free(symbolTab_stack);
free(out);
symbolTab_stack = NULL;
out = NULL;
return 0;
}