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

Function par_checker3

publication/code/chapter04/par_checker3.rs:40–74  ·  view source on GitHub ↗
(par: &str)

Source from the content-addressed store, hash-verified

38}
39
40fn par_checker3(par: &str) -> bool {
41 let mut char_list = Vec::new();
42 for c in par.chars() {
43 char_list.push(c);
44 }
45
46 let mut index = 0;
47 let mut balance = true;
48 let mut stack = Stack::new();
49 while index < char_list.len() && balance {
50 let c = char_list[index];
51
52 // 开符号入栈
53 if '(' == c || '[' == c || '{' == c {
54 stack.push(c);
55 }
56
57 // 闭符号则判断是否平衡
58 if ')' == c || ']' == c || '}' == c {
59 if stack.is_empty() {
60 balance = false;
61 } else {
62 let top = stack.pop().unwrap();
63 if !par_match(top, c) {
64 balance = false;
65 }
66 }
67 }
68
69 // 非括号直接跳过
70 index += 1;
71 }
72
73 balance && stack.is_empty()
74}
75
76fn main() {
77 let sa = "(2+3){func}[abc]";

Callers 1

mainFunction · 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