英文:
Go program prints asterisks instead of actual characters
问题
我正在编写一个程序,将后缀表达式转换为前缀形式(例如,将"ABC/-AK/L-*"转换为"*-A/BC-/AKL")。规则很简单:如果是字母或数字(操作数),则将其推入堆栈;如果是运算符,则弹出堆栈中的最后两个字符(假设为op1(最后一个)和op2(倒数第二个)),然后将它们与运算符连接起来(temp = operator + op2 + op1),然后将temp推入堆栈。
问题在于,当使用pop时,操作数变成了星号,我不知道为什么。也许需要使用指针?有人能告诉我我做错了什么吗?非常感谢!
输入: "ABC/-AK/L-*"
期望输出: "*-A/BC-/AKL"
观察到的输出: "[***]"
import (
"fmt"
)
type Stack []string
func (s *Stack) isEmpty() bool {
return len(*s) == 0
}
func (s *Stack) push(value string) {
*s = append(*s, value)
}
func (s *Stack) pop() (string, bool) {
if s.isEmpty() {
return "", false
} else {
elementIndex := len(*s) - 1
element := (*s)[elementIndex]
*s = (*s)[:elementIndex]
return element, true
}
}
func isOperator(character string) bool {
switch character {
case "+", "-", "*", "/":
return true
default:
return false
}
}
func input() {
var stack Stack
fmt.Print("请输入没有空格的方程式:\n")
input := "ABC/-AK/L-*"
for _, character := range input {
valueCheck := isOperator(string(character))
if valueCheck == true {
operand1 := input[len(input)-1]
stack.pop()
operand2 := input[len(input)-1]
stack.pop()
var temp string
temp = string(character) + string(operand2) + string(operand1)
stack.push(temp)
} else {
stack.push(string(character))
}
}
fmt.Print(stack)
}
func main() {
input()
}
英文:
I'm writing a program that converts postfix expression to it's prefix form (so like it should convert this "ABC/-AK/L-*" to this "*-A/BC-/AKL". The rules are simple: if it's a letter or a number (operand), then it is pushed to the stack, if it's an operator, then two lasts characters (let's say op1(the last) and op2(the one after the last one)) of the stack are being popped and then concatenated with the operator (temp = operator + op2 + op1) and this temp is then pushed to the stack.
The issue is that when pop is used operands become asterisks and I don't know why. Perhaps pointers are needed? Could someone please tell me what am I doing wrong? Thank you very much!
input: "ABC/-AK/L-*"
expected output: "*-A/BC-/AKL"
observed output: "[***]"
import (
"fmt"
)
type Stack []string
func (s *Stack) isEmpty() bool {
return len(*s) == 0
}
func (s *Stack) push(value string) {
*s = append(*s, value)
}
func (s *Stack) pop() (string, bool) {
if s.isEmpty() {
return "", false
} else {
elementIndex := len(*s) - 1
element := (*s)[elementIndex]
*s = (*s)[:elementIndex]
return element, true
}
}
func isOperator(character string) bool {
switch character {
case "+", "-", "*", "/":
return true
default:
return false
}
}
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 == true {
operand1 := input[len(input)-1]
stack.pop()
operand2 := input[len(input)-1]
stack.pop()
var temp string
temp = string(character) + string(operand2) + string(operand1)
stack.push(temp)
} else {
stack.push(string(character))
}
}
fmt.Print(stack)
}
func main() {
input()
}
</details>
# 答案1
**得分**: 0
```go
func input() {
var stack Stack
fmt.Print("请输入没有空格的方程式:\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))
}
}
}
这段代码会按照你的期望给出结果。
还有几点注意事项:
1)if valueCheck == true
太冗余了,因为 valueCheck
是布尔类型。
2)var temp string
和 temp = string(character) + string(operand2) + string(operand1)
有点啰嗦,可以简化为 temp := string(character) + string(operand2) + string(operand1)
。
最好熟悉一下 dlv 调试器,这样下次遇到问题时可以节省时间和精力。
英文:
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))
}
}
This will give you exactly what you expect.
A couple of side notes:
if valueCheck == true
is too much, sincevalueCheck
is of boolean type
var temp string
temp = string(character) + string(operand2) + string(operand1)
is a bit verbose too
temp := string(character) + string(operand2) + string(operand1)
is enough.
And best get familiar with dlv debugger, that will save some time and efforts next time you are at loss.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论