templates4/postfix2.scala
changeset 219 44161f2c3226
child 220 3020f8c76baa
equal deleted inserted replaced
218:22705d22c105 219:44161f2c3226
       
     1 // Shunting Yard Algorithm
       
     2 // Edsger Dijkstra
       
     3 
       
     4 
       
     5 type Toks = List[String]
       
     6 
       
     7 def split(s: String) = s.split(" ").toList
       
     8 
       
     9 
       
    10 abstract class Assoc
       
    11 case object RA extends Assoc
       
    12 case object LA extends Assoc
       
    13 
       
    14 def assoc(s: String) : Assoc = s match {
       
    15   case "^" => RA
       
    16   case _ => LA
       
    17 }
       
    18 
       
    19 
       
    20 val precs = Map("+" -> 1,
       
    21   		 "-" -> 1,
       
    22 		 "*" -> 2,
       
    23 		 "/" -> 2,
       
    24                  "^" -> 4)
       
    25 
       
    26 val ops = List("+", "-", "*", "/", "^")
       
    27 
       
    28 def is_op(op: String) : Boolean = ops.contains(op)
       
    29 
       
    30 def prec(op1: String, op2: String) : Boolean = assoc(op1) match {
       
    31   case LA => precs(op1) <= precs(op2)
       
    32   case RA => precs(op1) < precs(op2)
       
    33 }
       
    34 
       
    35 def syard(toks: Toks, st: Toks = Nil, rout: Toks = Nil) : Toks = (toks, st, rout) match {
       
    36   case (Nil, _, _) => rout.reverse ::: st
       
    37   case (num::in, st, rout) if (num.forall(_.isDigit)) => 
       
    38     syard(in, st, num :: rout)
       
    39   case (op1::in, op2::st, rout)  if (is_op(op1) && is_op(op2) && prec(op1, op2)) =>
       
    40     syard(op1::in, st, op2 :: rout) 
       
    41   case (op1::in, st, rout) if (is_op(op1)) => syard(in, op1::st, rout)
       
    42   case ("("::in, st, rout) => syard(in, "("::st, rout)
       
    43   case (")"::in, op2::st, rout) =>
       
    44     if (op2 == "(") syard(in, st, rout) else syard(")"::in, st, op2 :: rout)
       
    45   case (in, st, rout) => {
       
    46     println(s"in: ${in}   st: ${st}   rout: ${rout.reverse}")
       
    47     Nil
       
    48   }  
       
    49 } 
       
    50 
       
    51 def op_comp(s: String, n1: Long, n2: Long) = s match {
       
    52   case "+" => n2 + n1
       
    53   case "-" => n2 - n1
       
    54   case "*" => n2 * n1
       
    55   case "/" => n2 / n1
       
    56   case "^" => Math.pow(n2, n1).toLong
       
    57 } 
       
    58 
       
    59 def compute(toks: Toks, st: List[Long] = Nil) : Long = (toks, st) match {
       
    60   case (Nil, st) => st.head
       
    61   case (op::in, n1::n2::st) if (is_op(op)) => compute(in, op_comp(op, n1, n2)::st)
       
    62   case (num::in, st) => compute(in, num.toInt::st)  
       
    63 }
       
    64 
       
    65 
       
    66 
       
    67 
       
    68 compute(syard(split("3 + 4 * ( 2 - 1 )")))   // 7
       
    69 compute(syard(split("10 + 12 * 33")))       // 406
       
    70 compute(syard(split("( 5 + 7 ) * 2")))      // 24
       
    71 compute(syard(split("5 + 7 / 2")))          // 8
       
    72 compute(syard(split("5 * 7 / 2")))          // 17
       
    73 compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
       
    74 
       
    75 compute(syard(split("4 ^ 3 ^ 2")))      // 262144
       
    76 compute(syard(split("4 ^ ( 3 ^ 2 )")))  // 262144
       
    77 compute(syard(split("( 4 ^ 3 ) ^ 2")))  // 4096
       
    78 
       
    79 
       
    80 syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3"))  // 3 4 8 * 5 1 - 2 3 ^ ^ / +
       
    81 compute(syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3")))
       
    82 
       
    83 compute(syard(split("( 3 + 1 ) ^ 2 ^ 3")))   // 65536
       
    84 
       
    85 
       
    86 def pow(n1: Long, n2: Long) = Math.pow(n1, n2).toLong