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

Function par_checker3

code/chapter03/par_checker3.rs:38–72  ·  view source on GitHub ↗
(par: &str)

Source from the content-addressed store, hash-verified

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