completed rationals + added IntSet base
This commit is contained in:
		
							
								
								
									
										27
									
								
								src/Assignment3/IntSet.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/Assignment3/IntSet.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| abstract class IntSet() { | ||||
|   def add(x: Int): IntSet | ||||
|   def contains(x: Int): Boolean | ||||
| } | ||||
|  | ||||
| class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet() { | ||||
|   def add(x: Int): IntSet = { | ||||
|     if (x < elem) new NonEmpty(elem, left add x, right) | ||||
|     else if (x > elem) new NonEmpty(elem, left, right add x) | ||||
|     else this | ||||
|   } | ||||
|  | ||||
|   def contains(x: Int): Boolean = | ||||
|     if (x < elem) left contains x | ||||
|     else if (x > elem) right contains x | ||||
|     else true | ||||
| } | ||||
|  | ||||
| class Empty extends IntSet() { | ||||
|   def add(x: Int): IntSet = new NonEmpty(x, new Empty(), new Empty) | ||||
|   def contains(x: Int): Boolean = false | ||||
| } | ||||
|  | ||||
| val t1 = new Empty() | ||||
| val t2 = t1 add 3 | ||||
| val t3 = t1 add 4 add 5 add 2 add 6 | ||||
| t3 contains 4 | ||||
| @@ -1,31 +1,41 @@ | ||||
| class Rational(n: Int, d: Int) { | ||||
|   def num = n | ||||
|   def denom = d | ||||
| import scala.annotation.tailrec | ||||
|  | ||||
|   def add(that: Rational): Rational = new Rational( | ||||
| class Rational(n: Int, d: Int) { | ||||
|   require(d != 0) | ||||
|  | ||||
|   @tailrec | ||||
|   private def gcd(x: Int, y: Int): Int = | ||||
|     if (y == 0) x else gcd(y, x % y) | ||||
|  | ||||
|   private val g: Int = gcd(n, d) | ||||
|   def num: Int = n / g | ||||
|   def denom: Int = d / g | ||||
|  | ||||
|  | ||||
|   def +(that: Rational): Rational = new Rational( | ||||
|     this.num * that.denom + this.denom * that.num, | ||||
|     this.denom * that.denom | ||||
|   ) | ||||
|  | ||||
|   def neg: Rational = new Rational(-num, denom) | ||||
|   def unary_- : Rational = new Rational(-num, denom) | ||||
|  | ||||
|   def smallerThan(that: Rational): Boolean = this.num * that.denom < that.num * this.denom | ||||
|   def <(that: Rational): Boolean = this.num * that.denom < that.num * this.denom | ||||
|  | ||||
|   def max(that: Rational): Rational = if (this.smallerThan(that)) that else this | ||||
|   def max(that: Rational): Rational = if (this < that) that else this | ||||
|  | ||||
|   override def toString = num + "/" + denom | ||||
|   override def toString = if (math.abs(denom) == 1) "" + denom else num + "/" + denom | ||||
| } | ||||
|  | ||||
| val r1 = new Rational(1, 3) | ||||
| val r2 = new Rational(2, 3) | ||||
| val r3 = r1.add(r2) | ||||
| val r3 = r1 + r2 | ||||
|  | ||||
| r3 | ||||
|  | ||||
| r2.neg | ||||
| -r2 | ||||
|  | ||||
| r2.smallerThan(r1) | ||||
| r2.smallerThan(r3) | ||||
| r2 < r1 | ||||
| r2 < r3 | ||||
|  | ||||
| r2.max(r1) | ||||
| r2.max(r3) | ||||
		Reference in New Issue
	
	Block a user