forked from shinezejian/javaStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckExpression.java
More file actions
40 lines (36 loc) · 1.06 KB
/
Copy pathCheckExpression.java
File metadata and controls
40 lines (36 loc) · 1.06 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
package com.zejian.structures.Stack;
/**
* Created by zejian on 2016/11/27.
* Blog : http://blog.csdn.net/javazejian [原文地址,请尊重原创]
* 表达式检测
*/
public class CheckExpression {
public static String isValid(String expstr)
{
//创建栈
LinkedStack<String> stack = new LinkedStack<>();
int i=0;
while(i<expstr.length())
{
char ch=expstr.charAt(i);
i++;
switch(ch)
{
case '(': stack.push(ch+"");//左括号直接入栈
break;
case ')': if (stack.isEmpty() || !stack.pop().equals("(")) //遇见右括号左括号直接出栈
return "(";
}
}
//最后检测是否为空,为空则检测通过
if(stack.isEmpty())
return "check pass!";
else
return "check exception!";
}
public static void main(String args[])
{
String expstr="((5-3)*8-2)";
System.out.println(expstr+" "+isValid(expstr));
}
}