zoukankan      html  css  js  c++  java
  • scala编程第15章

    package myscala15
    import myscala.Element.elem
    import myscala.Element

    sealed abstract class Expr
    case class Var(name: String) extends Expr
    case class Number(num: Double) extends Expr
    case class UnOp(operator: String, arg: Expr) extends Expr
    case class BinOp(operator: String,
      left: Expr, right: Expr) extends Expr

    class ExprFormatter {
      //包含了递增优先级的组中的操作符
      private val opGroups =
        Array(
            Set("|", "||"),
            Set("&", "&&"),
            Set("^"),
            Set("==", "!="),
            Set("<", "<=", ">", ">="),
            Set("+", "-"),
            Set("*", "%")
            )
      //操作符优先级映射
      private val precedence = {
        val assocs =
          for {
            i <- 0 until opGroups.length
            op <- opGroups(i)
          } yield op -> i
          Map() ++ assocs    
      }
      private val unaryPrecedence = opGroups.length
      private val fractionPrecedence = -1
     
      private def format(e: Expr, enclPrec: Int): Element =
        e match {
        case Var(name) =>
          elem(name)
        case Number(num) =>
          def stripDot(s: String) =
            if(s endsWith ".0") s.substring(0, s.length - 2)
            else s
            elem(stripDot(num.toString))
        case UnOp(op, arg) =>
          elem(op) beside format(arg, unaryPrecedence)
        case BinOp("/", left, right) =>
          val top = format(left, fractionPrecedence)
          val bot = format(right, fractionPrecedence)
          val line = elem('-', top.width max bot.width, 1)
          val frac = top above line above bot
          if (enclPrec != fractionPrecedence) frac
          else elem(" ") beside frac beside elem(" ")
        case BinOp(op, left, right) =>
          val opPrec = precedence(op)
          val l = format(left, opPrec)
          val r = format(right, opPrec + 1)
          val oper = l beside elem(" "+ op +" ") beside r
          if (enclPrec <= opPrec)oper
          else elem("(") beside oper beside elem(")")
      }
      def format(e: Expr): Element = format(e, 0)
    }

    object Express extends App {
      val f = new ExprFormatter
      val e1 = BinOp("*", BinOp("/", Number(1), Number(2)),
           BinOp("+", Var("x"), Number(1)))
      val e2 = BinOp("+", BinOp("/", Var("x"), Number(2)),
           BinOp("/", Number(1.5), Var("x")))
      val e3 = BinOp("/", e1, e2)
      def show(e: Expr) = println(f.format(e)+ " ")
    //  for (val e <- Array(e1, e2, e3)) show(e) 显示出错可做如下等价修改

     Array(e1, e2, e3).foreach(e => show(e))
    }

  • 相关阅读:
    ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
    ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
    A*算法的实现
    codevs1011 数的计算 2001年NOIP全国联赛普及组
    一场ACM一场梦——我的一年
    HDU 4422 The Little Girl who Picks Mushrooms ( 模拟)
    HDU4277 USACO ORZ(dfs+set)
    HDU4272LianLianKan(dfs)
    HDU4268 Alice and Bob(贪心+multiset)
    hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/4079276.html
Copyright © 2011-2022 走看看