-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdemo4.cpp
More file actions
71 lines (60 loc) · 1.57 KB
/
Copy pathdemo4.cpp
File metadata and controls
71 lines (60 loc) · 1.57 KB
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
#include<iostream>
#include"stdlib.h"
#include"MyStack.h"
#include"Coordinate.h"
using namespace std;
int strlen(const char *str) {
// 实现strlen,返回字符串长度
if ('\0' == *str)
return 0;
else
return strlen(str + 1) + 1;
}
int main(void) {
MyStack<char> *pStack = new MyStack<char>(30);
MyStack<char> *pNeedStack = new MyStack<char>(30);
char str[] = "[()])";
char currentNeed = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] != currentNeed) {
pStack->push(str[i]);
switch(str[i]) {
case '[':
if (currentNeed != 0) {
pNeedStack->push(currentNeed);
}
currentNeed = ']';
break;
case '(':
if (currentNeed != 0) {
pNeedStack->push(currentNeed);
}
currentNeed = ')';
break;
case '{':
if (currentNeed != 0) {
pNeedStack->push(currentNeed);
}
currentNeed = '}';
break;
default:
cout << "fail" << endl;
return 0;
}
} else {
char elem;
pStack->pop(elem);
pNeedStack->pop(currentNeed);
}
}
if (pStack->stackEmpty()) {
cout << "success" << endl;
} else {
cout << "fail" << endl;
}
delete pStack;
pStack = NULL;
delete pNeedStack;
pNeedStack = NULL;
return 0;
}