Convertir notación infijada a postfix

¿Qué es la notación infija y la notación postfix se puede encontrar si lees cuidadosamente en Wikipedia? También hay un artículo sobre Habré.

En este artículo mostraré un algoritmo simple y claro para convertir entradas infijadas en postfix. Implemento este algoritmo en el lenguaje Kotlin, aunque el algoritmo es adecuado para cualquier lenguaje de programación.

Pues adelante.

Para una mejor comprensión y memorización, utilizaremos abreviaturas:

  1. PILA : una pila es un tipo de datos, que es una lista de elementos organizados de acuerdo con el principio LIFO (el último entró, el primero salió). Estudio más detallado aquí.
  2. COLA - una cola es un tipo de datos, que es una lista de elementos organizados de acuerdo con el principio FIFO (primero en llegar, primero en salir). Estudio más detallado aquí.
  3. PUSH : empujar, al empujar, se agrega un nuevo elemento a la parte superior de la pila, es decir, el elemento actual se convierte en la parte superior de la pila (el último elemento). Puedes estudiar en detalle aquí
  4. POP : descarga un elemento que es la parte superior de la pila. La parte superior es el último elemento en la pila. Más detalles se pueden encontrar aquí .
  5. TOP : la parte superior de la pila, es decir, su último elemento

El algoritmo de conversión de infijo a expresión de postfijo
.

  1. , (QUEUE).
  2. (+, -, *, /) :

    • (STACK) (TOP), (PUSH) (STACK).
    • (TOP), (PUSH) (STACK).
    • , (TOP), POP (QUEUE), (TOP), (PUSH) (STACK).

  3. , (PUSH) (STACK).
  4. , (POP) (QUEUE), . (STACK).
  5. (POP) (QUEUE)

: 5*6+(2-9)
.
STACK = empty
QUEUE = empty

  • 5. .
    STACK = empty
    QUEUE = 5
  • *. .
    STACK = *
    QUEUE = 5
  • 6. .
    STACK = *
    QUEUE = 5, 6
  • +. , . , . .
    STACK = +
    QUEUE = 5, 6, *
  • (. .
    STACK = +, (
    QUEUE = 5, 6, *
  • 2. .
    STACK = +, (
    QUEUE = 5, 6, *, 2
  • . , "", "(". .
    STACK = +, (, —
    QUEUE = 5, 6, *, 2
  • 9 . .
    STACK = +, (, —
    QUEUE = 5, 6, *, 2, 9
  • ) . , . .
    STACK = +
    QUEUE = 5, 6, *, 2, 9, —
  • , .
    STACK = empty
    QUEUE = 5, 6, *, 2, 9, -, +

56*29-+.

Algoritmo para obtener un resultado de una expresión postfix
.

  1. , (PUSH) (STACK)
  2. (*-/+), (POP) (STACK) . (PUSH) (STACK).
  3. , (TOP) (STACK) .

: 56*29-+.

.

STACK = empty

  • 5. .
    STACK = 5
  • 6. .
    STACK = 5, 6
  • * . (5 * 6), , .
    STACK = 30
  • 2 . .
    STACK = 30, 2
  • 9 . .
    STACK = 30, 2, 9
  • . (2 — 9), , .
    STACK = 30, -7
  • + . (-7 + 30), , .
    STACK = 23

:

: 5*6+(2-9) = 23

: 56*29-+ = 23

Por ejemplo, implementamos en el idioma Kotlin
: 5*8*(2+9)+(7*5+8-9*(5*5)+5) = 263

  1. , expressionList: mutableListOf<String>()
    private var stack = mutableListOf<String>()
    private var queue = mutableListOf<String>()
    private var expressionList = mutableListOf<String>()
    
    fun main() {
        parseExpression()
    }
    
    fun parseExpression() {
        "5*8*(2+9)+(7-5+8-9*(5*5)+5)".forEach {
            expressionList.add(it.toString())
        }
        print(" : ")
        expressionList.forEach {
            print(it)
        }
        println()
    }
    


  2. private fun getPostFixEx() {
        //  expressionList
        expressionList.forEach {
            when {
                //      PUSH
                it == "(" -> push(it)
    
                //      POP
                it == ")" -> {
                    if (expressionList.contains("(")) {
                        pop()
                    }
                }
    
                //   ,    
                Regex("[\\d]").containsMatchIn(it) -> addQueue(it)
    
                //   +  -
                Regex("[+-]").containsMatchIn(it) ->
                    /* ,         ,
                    *   PUSH */
                    if (stack.isEmpty() || stack.last() == "(") push(it)
                    /* ,       
                    * ,   POP,  PUSH */
                    else if (stack.last().contains(Regex("[/*]"))) {
                        pop()
                        push(it)
                    }
                    //    PUSH
                    else {
                        addQueue(stack.last())
                        stack[stack.lastIndex] = it
                    }
    
                //   *  /
                Regex("[*/]").containsMatchIn(it) -> {
                    /* ,         ,
                    *   POP */
                    if (stack.isNotEmpty() && (stack.last() == "*" || stack.last() == "/")) {
                        pop()
                    }
                    //   PUSH
                    push(it)
                }
            }
        }
        //     ,       
        if (stack.isNotEmpty()) {
            for (i in stack.lastIndex downTo 0) {
                if (stack[i] != "(") {
                    addQueue(stack[i])
                }
            }
        }
        print(" : ")
        queue.forEach {
                print(it)
        }
        println()
    
    }
    
    private fun pop() {
        //         ,     
        Loop@ for (i in stack.lastIndex downTo 0) {
            if (stack[i] == "(") {
                stack[i] = " "
                break@Loop
            }
            addQueue(stack[i])
            stack[i] = " "
        }
        stack.removeIf { it == " " }
    }
    
    private fun addQueue(item: String) {
        queue.add(item)
    }
    
    private fun push(item: String) {
        stack.add(item)
    }
    


  3. private fun calcPostFix() {
        //   
        val stack = mutableListOf<Int>()
        //    
        for (item in queue) {
            when {
                //    - ,    
                Regex("[\\d]").containsMatchIn(item) -> {
                    stack.add(item.toInt())
                }
                /*    + ,    
                    */
                item == "+" -> {
                    stack[stack.lastIndex - 1] = stack[stack.lastIndex - 1] + stack.last()
                    stack.removeAt(stack.lastIndex)
                }
                /*    * ,    
                    */
                item == "*" -> {
                    stack[stack.lastIndex - 1] = stack[stack.lastIndex - 1] * stack.last()
                    stack.removeAt(stack.lastIndex)
                }
                /*    / ,    
                    */
                item == "/" -> {
                    stack[stack.lastIndex - 1] = stack[stack.lastIndex - 1] / stack.last()
                    stack.removeAt(stack.lastIndex)
                }
                /*    -,    
                     */
                item == "-" -> {
                    stack[stack.lastIndex - 1] = stack[stack.lastIndex - 1] - stack.last()
                    stack.removeAt(stack.lastIndex)
                }
            }
        }
    
        //  -  ,    
        println(": ${stack.first()}")
    }
    



:

: 5*8*(2+9)+(7-5+8-9*(5*5)+5)

: 58*29+*75-8+955**-5++

: 230

Eso es todo, todo es bastante simple y claro.

All Articles