@amoszhou
2014-05-19T10:36:18.000000Z
字数 2593
阅读 1292
scala课后习题
都是从左到右,貌似 3->4+5 会编译不过去。
**(像Fortran那样)或者^(像Pascal那样)作为乘方操作符呢?Scala中的操作符就是方法,其优先级是根据首字母来判断的,一般乘方的操作符是优于乘法操作的,如果使用*作为乘方的话,那么其优先级则与相同,而如果使用^的话,则优先级低于*操作。
import scala.math.absclass Fraction(n: Int, d: Int) {private val num: Int = if (d == 0) 1 else n * sign(d) / gcd(n, d);private val den: Int = if (d == 0) 0 else d * sign(d) / gcd(n, d);override def toString = num + "/" + dendef sign(a: Int) = if (a > 0) 1 else if (a < 0) -1 else 0def gcd(a: Int, b: Int): Int = if (b == 0) abs(a) else gcd(b, a % b)def +(other:Fraction):Fraction={newFrac((this.num * other.den) + (other.num * this.den),this.den * other.den)}def -(other:Fraction):Fraction={newFrac((this.num * other.den) - (other.num * this.den),this.den * other.den)}def *(other:Fraction):Fraction={newFrac(this.num * other.num,this.den * other.den)}def /(other:Fraction):Fraction={newFrac(this.num * other.den,this.den * other.num)}private def newFrac(a:Int,b:Int):Fraction={val x:Int = if (b == 0) 1 else a * sign(b) / gcd(a, b);val y:Int = if (b == 0) 0 else b * sign(b) / gcd(a, b);new Fraction(x,y)}}object Test extends App{val f = new Fraction(15,-6)val p = new Fraction(20,60)println(f)println(p)println(f + p)println(f - p)println(f * p)println(f / p)}
<table><tr><td>Java</td></tr><td>Scala</td></tr><tr><td>Goling...
class Table{private var s:String = ""def |(str:String):Table={s +="<td>" + str + "</td>"this}def ||(str:String):Table={s += "</tr><tr><td>" + str + "</td>"this}override def toString():String={"<table><tr>" + this.s + "</tr></table>"}}object Table{def apply():Table={new Table()}def main(args: Array[String]) {println(Table() | "Java" | "Scala" || "Gosling" | "Odersky" || "JVM" | "JVM,.NET")}}
class RichFile(val path:String){}object RichFile{def apply(path:String):RichFile={new RichFile(path)}def unapply(richFile:RichFile) = {if(richFile.path == null){None} else {val reg = "([/\\w+]+)/(\\w+)\\.(\\w+)".rval reg(r1,r2,r3) = richFile.pathSome((r1,r2,r3))}}def main(args: Array[String]) {val richFile = RichFile("/home/cay/readme.txt")val RichFile(r1,r2,r3) = richFileprintln(r1)println(r2)println(r3)}}
class RichFile(val path:String){}object RichFile{def apply(path:String):RichFile={new RichFile(path)}def unapplySeq(richFile:RichFile):Option[Seq[String]]={if(richFile.path == null){None} else {Some(richFile.path.split("/"))}}def main(args: Array[String]) {val richFile = RichFile("/home/cay/readme.txt")val RichFile(r @ _*) = richFileprintln(r)}}