MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / par_checker3

Function par_checker3

publication/code/chapter04/infix_to_postfix.rs:48–78  ·  view source on GitHub ↗

检测括号是否匹配

(infix: &str)

Source from the content-addressed store, hash-verified

46
47// 检测括号是否匹配
48fn 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
80fn infix_to_postfix(infix: &str) -> Option<String> {
81 // 括号匹配检验

Callers 1

infix_to_postfixFunction · 0.70

Calls 5

par_matchFunction · 0.70
pushMethod · 0.45
lenMethod · 0.45
is_emptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected