Using the stack data structure golang demand calculation expression (addition, subtraction)

For example: 3 + 2 * 6-2

Define two stacks, a value for the stack, a stack for the operator;

stack.go

Stack Package 

Import ( 
    " errors " 
    " FMT " 
) 

type Stack struct { 
    MAXTOP int      // maximum stack can store the number of 
    Top     int      // stack 
    Arr [ 20 is ] int  // analog stack 
} 

FUNC (S * Stack) the Push (Val int ) (ERR error) {
     // first determine whether the stack is full 
    iF s.Top s.MaxTop- == 1 { 
        fmt.Println ( " stack is full " )
         return errors.New ( "Full stack " ) 
    } 
    s.Top ++ 
    s.Arr [s.Top] = Val
     return 
} 

FUNC (S * Stack) Pop () (Val int , ERR error) {
     IF s.Top == - . 1 { 
        FMT .Println ( " stack empty " )
         return - . 1 , errors.New ( " stack empty " ) 
    } 
    Val = s.Arr [s.Top] 
    s.Arr [s.Top] = 0 
    s.Top -
     return Val, nil 

}

FUNC (S * Stack) the Show () {
     IF s.Top == - . 1 { 
        fmt.Println ( " the stack is empty " )
         return 
    } 
    tmp: = S
     for I: = tmp.Top; I> = 0 ; I- - { 
        fmt.Printf ( " Arr [D%] V =% \ n- " , I, tmp.Arr [I]) 
    } 
    return 
} 

// whether a character is numeric or arithmetic 
func (s * Stack) IsOper ( Val int ) BOOL {
     IF Val == 42 is || Val == 43 isVal == || 45 || Val == 47 {
         return  to true 
    } the else {
         return  to false 
    } 
} 

// calculation method 
FUNC (S * Stack) Cal (num1 int , num2 int , Oper int ) int { 
    RES: = 0 
    Switch Oper {
     // multiplication 
    Case  42 is : 
        RES = * num2 num1
     // adder 
    Case  43 is : 
        RES = num2 +num1
     // subtraction 
    Case  45 : 
        RES = num2 - num1
     // divide 
    Case  47 : 
        RES = num2 / num1
     default : 
        fmt.Println ( " operator error " ) 
    } 
    return RES 
} 

// define the priority 
func (s * Stack) the Priority (Oper int ) int { 
    RES: = 0 
    IF Oper == 42 is || Oper == 47 { 
        RES =1
    } else if oper == 43 || oper == 45 {
        res = 0
    }
    return res
}

main.go

package main 

import ( 
    " fmt " 
    " go_code / data_structure / stack " 
    " StrConv " 
) 

func main () { 

    str: = " 30 + 2 * 6-600 / 2 " 
    n: = len (str) 
    numStack: = stack. stack { 
        Maxtop: n 
        Top:     - 1 , 
    } 
    fully stack: = & stack.Stack { 
        Maxtop: n 
        Top:     - 1 , 
    } 
    index: = 0
    num1: = 0, 
    num2: = 0 
    operandi: = 0 
    result: = 0 
    keepNum: = "" 
    for { 
        ch: = str [index: index + 1 ]
         // 字符对应的ASCII码值 
        tmp: = int ([] byte ( ch) [ 0 ])
         if operStack.IsOper (tmp) {
             if operStack.Top == - 1 { 
                operStack.Push (tmp) 
            } else {
                 //Determining the priority of the stack 
                IF operStack.Priority (operStack.Arr [operStack.Top])> = 
                    operStack.Priority (tmp) { 
                    num1, _ = numStack.Pop () 
                    num2, _ = numStack.Pop () 
                    Oper, _ = operStack.Pop () 
                    result = operStack.Cal (num1, num2, Oper)
                     // the result of the calculation to re-stack 
                    numStack.Push (result)
                     // current symbol to re-stack 
                    operStack.Push (tmp) 
                } the else {  
                    operStack .Push (tmp)
                } 
            }

        } The else {
             // Analyzing the like 30 such as a digital type 
            keepNum + = CH 

            IF index == N- . 1 { 
                Val, _: = strconv.ParseInt (keepNum, 10 , 64 ) 
                numStack.Push ( int (Val)) 
            } the else {
                 // probe index back to the bit 
                IF operStack.IsOper ( int ([] byte (STR [index + . 1 : index + 2 ]) [ 0 ])) { 
                    Val, _: = strconv.ParseInt (keepNum, 10 , 64 ) 
                    numStack.Push ( int (Val)) 
                    keepNum = "" 
                } 
            } 
            // stack if the number of type int
             // Val, _: = strconv.ParseInt (CH, 10, 64)
             // numStack.Push (int (Val)) 
        }
         IF index == N- . 1 {
             BREAK 
        } 
        // continue scanning 
        index ++ 
    } 

    // if the expression is scanned, and a two-digit symbols are sequentially taken calculates 
    for {
         IF operStack.Top == - . 1 {
             BREAK 
        } 
        num1, _ = numStack.Pop () 
        num2, _ = numStack.Pop () 
        Oper, _ = operStack.Pop () 
        Result = operStack.Cal (num1, num2, Oper)
         / / the re-calculation results stack 
        numStack.Push (result) 
    } 

    RES, _: = numStack.Pop () 
    fmt.Printf ( " calc% v is an expression:% v \ n- " , STR, RES) 
}

operation result:

Guess you like

Origin www.cnblogs.com/xiximayou/p/12023108.html