题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
package main import ( "bufio" "fmt" "os" "strings" ) func main() { scan:=bufio.NewScanner(os.Stdin) scan.Scan() cmd:=scan.Text() tem := "" var flag bool var res []string arr := strings.Split(cmd, "") count := 0 for i, v := range arr { // fmt.Println(tem) if v == "\"" { count += 1 // 判断是否进入“”模式赋值 if count%2 == 0 { // 退出“”模式 flag = false res = append(res, tem) tem = "" } else if count%2 == 1 { flag = true } // 进出模式,这个"都不需要扫描 continue } if !flag { // 没有进入模式 if v != " " { tem += v } // 添加到res中的条件是v为空格或者v是最后一个格子 // 当然需要注意tem当中必须有值,这个很重要,比如在下面这个例子中 // l "b:\" /kzv /yar // 当第二个参数的第二个引号被扫描的时候,退出了“”搜索模式,但是随后的空格又会引起一次新的无用的提交 if (v==" "||i==len(arr)-1)&&len(tem)!=0{ res = append(res, tem) tem = "" } } else { // 进入模式 tem += v } } fmt.Println(len(res)) for i := range res { fmt.Println(res[i]) } }