1 回答

TA貢獻1788條經驗 獲得超4個贊
func input() {
var stack Stack
fmt.Print("Please input the equation without spaces: \n")
input := "ABC/-AK/L-*"
for _, character := range input {
valueCheck := isOperator(string(character))
if valueCheck {
operand1 := stack[len(stack)-1]
stack.pop()
operand2 := stack[len(stack)-1]
stack.pop()
temp := string(character) + string(operand2) + string(operand1)
stack.push(temp)
} else {
stack.push(string(character))
}
}
這將為您提供您所期望的。
一些旁注:
if valueCheck == true太多了,因為valueCheck是布爾類型
var temp string
temp = string(character) + string(operand2) + string(operand1)
也有點冗長
temp := string(character) + string(operand2) + string(operand1)
足夠的。
并且最好熟悉一下dlv debugger,這樣下次你迷茫的時候會節省一些時間和精力。
- 1 回答
- 0 關注
- 118 瀏覽
添加回答
舉報