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))
    }

  • 相关阅读:
    git ——visual studio code 工具之 Git Graph & git clone & git checkout
    docker中添加redis & 在程序中应用
    Configuring Redis for ASP.NET Core Session Store(转载)
    Configuring Redis Session State In ASP.NET Core(转载)
    http请求端口占用异常——处理思路
    在pods中添加有关httpclient的 压力测试 & 监控
    Singleton HttpClient? Beware of this serious behaviour and how to fix it
    HttpClient 之 CLOSE_WAIT
    HttpClient的使用
    HttpClient连接池之CLOSE_WAIT
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/4079276.html
Copyright © 2011-2022 走看看