检测括号是否匹配
(infix: &str)
| 46 | |
| 47 | // 检测括号是否匹配 |
| 48 | fn par_checker3(infix: &str) -> bool { |
| 49 | let mut char_list = Vec::new(); |
| 50 | for c in infix.chars() { |
| 51 | char_list.push(c); |
| 52 | } |
| 53 | |
| 54 | let mut index = 0; |
| 55 | let mut balance = true; |
| 56 | let mut stack = Stack::new(); |
| 57 | while index < char_list.len() && balance { |
| 58 | let c = char_list[index]; |
| 59 | if '(' == c || '[' == c || '{' == c { |
| 60 | stack.push(c); |
| 61 | } |
| 62 | |
| 63 | if ')' == c || ']' == c || '}' == c { |
| 64 | if stack.is_empty() { |
| 65 | balance = false; |
| 66 | } else { |
| 67 | let top = stack.pop().unwrap(); |
| 68 | if !par_match(top, c) { |
| 69 | balance = false; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | index += 1; |
| 75 | } |
| 76 | |
| 77 | balance && stack.is_empty() |
| 78 | } |
| 79 | |
| 80 | fn infix_to_postfix(infix: &str) -> Option<String> { |
| 81 | // 括号匹配检验 |