-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.go
72 lines (58 loc) · 1.17 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Memory struct {
data []int64
p int
}
func NewMemory () Memory {
return Memory{
data: make([]int64, 10),
p: 0,
}
}
func (memory *Memory) Get () int64 {
return memory.data[memory.p]
}
func (memory *Memory) Add () {
memory.data[memory.p] ++
}
func (memory *Memory) Minus () {
memory.data[memory.p] --
}
// 自动扩充右边界
func (memory *Memory) ToRight () {
memory.p ++
if memory.p >= len(memory.data) {
memory.data = append(memory.data, int64(0))
}
}
// 左边触底,到达最右端
func (memory *Memory) ToLeft () {
if memory.p == 0 {
memory.p = len(memory.data) - 1
} else {
memory.p --
}
}
func (memory *Memory) PrintChar () {
fmt.Print(string(rune(memory.data[memory.p])))
}
func (memory *Memory) ReadNum () {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
fmt.Print(input)
if err != nil {
panic("[Error] 蛤???你的输入Too young,too simple...")
}
inputNum, err := strconv.Atoi(strings.Trim(input, "\n"))
if err != nil {
panic("[Error] 蛤???你的输入Sometimes naïve")
}
memory.data[memory.p] = int64(inputNum)
}