所以我编写了这个小程序,它给图灵机提供指令,并从中打印出选定的单元格:
package main
import "fmt"
import s "strings"
func main() {
fmt.Println(processturing("> > > + + + . ."));
}
func processturing(arguments string) string{
result := ""
dial := 0
cells := make([]int, 30000)
commands := splitstr(arguments, " ")
for i := 0;i<len(commands);i++ {
switch commands[i] {
case ">":
dial += 1
case "<":
dial -= 1
case "+":
cells[dial] += 1
case "-":
cells[dial] -= 1
case ".":
result += string(cells[dial]) + " "
}
}
return result
}
//splits strings be a delimeter
func splitstr(input, delim string) []string{
return s.Split(input, delim)
}
Run Code Online (Sandbox Code Playgroud)
问题是,当它运行时,控制台不显示任何内容.它什么也没显示.如何fmt.println从我的函数中生成结果字符串?
表达方式
string(cells[dial])
Run Code Online (Sandbox Code Playgroud)
产生整数值的UTF-8表示cells[dial].打印带引号的字符串输出以查看发生了什么:
fmt.Printf("%q\n", processturing("> > > + + + . .")) // prints "\x03 \x03 "
Run Code Online (Sandbox Code Playgroud)
我想你想要整数的十进制表示:
strconv.Itoa(cells[dial])
Run Code Online (Sandbox Code Playgroud)