pre_testing3/postfix.scala
changeset 401 9471c3b7ea02
parent 400 e48ea8300b2d
child 402 de59aa20a1dc
equal deleted inserted replaced
400:e48ea8300b2d 401:9471c3b7ea02
     1 // Shunting Yard Algorithm
       
     2 // by Edsger Dijkstra
       
     3 // ========================
       
     4 
       
     5 object CW8a {
       
     6 
       
     7 type Toks = List[String]
       
     8 
       
     9 // the operations in the simple version
       
    10 val ops = List("+", "-", "*", "/")
       
    11 
       
    12 // the precedences of the operators
       
    13 val precs = Map("+" -> 1,
       
    14 		"-" -> 1,
       
    15 		"*" -> 2,
       
    16 		"/" -> 2)
       
    17 
       
    18 // helper function for splitting strings into tokens
       
    19 def split(s: String) : Toks = s.split(" ").toList
       
    20 
       
    21 // (6) Implement below the shunting yard algorithm. The most
       
    22 // convenient way to this in Scala is to implement a recursive 
       
    23 // function and to heavily use pattern matching. The function syard 
       
    24 // takes some input tokens as first argument. The second and third 
       
    25 // arguments represent the stack and the output of the shunting yard 
       
    26 // algorithm.
       
    27 //
       
    28 // In the marking, you can assume the function is called only with 
       
    29 // an empty stack and an empty output list. You can also assume the
       
    30 // input os  only properly formatted (infix) arithmetic expressions
       
    31 // (all parentheses will be well-nested, the input only contains 
       
    32 // operators and numbers).
       
    33 
       
    34 // You can implement any additional helper function you need. I found 
       
    35 // it helpful to implement two auxiliary functions for the pattern matching:  
       
    36 // 
       
    37  
       
    38 def is_op(op: String) : Boolean = ops.contains(op)
       
    39 
       
    40 def prec(op1: String, op2: String) : Boolean = precs(op1) <= precs(op2)
       
    41 
       
    42 
       
    43 def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = (toks, st, out) match {
       
    44   case (Nil, _, _) => out.reverse ::: st
       
    45   case (num::in, st, out) if (num.forall(_.isDigit)) => 
       
    46     syard(in, st, num :: out)
       
    47   case (op1::in, op2::st, out)  if (is_op(op1) && is_op(op2) && prec(op1, op2)) =>
       
    48     syard(op1::in, st, op2 :: out) 
       
    49   case (op1::in, st, out) if (is_op(op1)) => syard(in, op1::st, out)
       
    50   case ("("::in, st, out) => syard(in, "("::st, out)
       
    51   case (")"::in, op2::st, out) =>
       
    52     if (op2 == "(") syard(in, st, out) else syard(")"::in, st, op2 :: out)
       
    53   case (in, st, out) => {
       
    54     println(s"in: ${in}   st: ${st}   out: ${out.reverse}")
       
    55     Nil
       
    56   }  
       
    57 } 
       
    58 
       
    59 
       
    60 // test cases
       
    61 //syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
       
    62 //syard(split("10 + 12 * 33"))       // 10 12 33 * +
       
    63 //syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
       
    64 //syard(split("5 + 7 / 2"))          // 5 7 2 / +
       
    65 //syard(split("5 * 7 / 2"))          // 5 7 * 2 /
       
    66 //syard(split("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
       
    67 
       
    68 //syard(split("3 + 4 + 5"))           // 3 4 + 5 +
       
    69 //syard(split("( ( 3 + 4 ) + 5 )"))    // 3 4 + 5 +
       
    70 //syard(split("( 3 + ( 4 + 5 ) )"))    // 3 4 5 + +
       
    71 //syard(split("( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )")) // 3 4 5 + +
       
    72 
       
    73 // (7) Implement a compute function that evaluates an input list
       
    74 // in postfix notation. This function takes a list of tokens
       
    75 // and a stack as argumenta. The function should produce the 
       
    76 // result as an integer using the stack. You can assume 
       
    77 // this function will be only called with proper postfix 
       
    78 // expressions.    
       
    79 
       
    80 def op_comp(s: String, n1: Int, n2: Int) = s match {
       
    81   case "+" => n2 + n1
       
    82   case "-" => n2 - n1
       
    83   case "*" => n2 * n1
       
    84   case "/" => n2 / n1
       
    85 } 
       
    86 
       
    87 def compute(toks: Toks, st: List[Int] = Nil) : Int = (toks, st) match {
       
    88   case (Nil, st) => st.head
       
    89   case (op::in, n1::n2::st) if (is_op(op)) => compute(in, op_comp(op, n1, n2)::st)
       
    90   case (num::in, st) => compute(in, num.toInt::st)  
       
    91 }
       
    92 
       
    93 // test cases
       
    94 // compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
       
    95 // compute(syard(split("10 + 12 * 33")))       // 406
       
    96 // compute(syard(split("( 5 + 7 ) * 2")))      // 24
       
    97 // compute(syard(split("5 + 7 / 2")))          // 8
       
    98 // compute(syard(split("5 * 7 / 2")))          // 17
       
    99 // compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
       
   100 
       
   101 }
       
   102 
       
   103