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