Home > Blockchain >  Go program prints asterisks instead of actual characters
Go program prints asterisks instead of actual characters

Time:05-13

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()
}

CodePudding user response:

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:

  1. if valueCheck == true is too much, since valueCheck 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.

  •  Tags:  
  • go
  • Related