Created
January 20, 2015 12:14
-
-
Save 1206yaya/07b54a8080e32608f6b7 to your computer and use it in GitHub Desktop.
分数オブジェクトRational メソッド> + - * / 約分 ができる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PSSE.code | |
//class Rational(n: Int, d: Int) { | |
// require(d != 0) // falseならば、IllegalArgumentExceptionを投げる | |
// override def toString = n + "/" + d | |
//} | |
// thatはaddメソッド呼びだしのレシーバーではないので、that.nとかthat.dとすることはできない※ | |
// thatの分子や分母にアクセスするには、それらをフィールドにする必要がある | |
//class Rational(n: Int, d: Int) { // This won't compile | |
// require(d != 0) | |
// override def toString = n +"/"+ d | |
// def add(that: Rational): Rational = | |
// new Rational(n * that.d + that.n * d, d * that.d) | |
//} | |
// < lessThanメソッド の追加 > | |
//class Rational(n: Int, d: Int) { | |
// require(d != 0) | |
// val numer: Int = n | |
// val denom: Int = d | |
// | |
// override def toString = numer +"/"+ denom | |
// | |
// // 他の分数との足し算を行う | |
// def add(that: Rational): Rational = | |
// new Rational( | |
// numer * that.denom + that.numer * denom, | |
// denom * that.denom | |
// ) | |
// // 他の分数よりも小さいかどうか | |
// def lessThan(that: Rational) = | |
// this.numer * that.denom < that.numer * this.denom | |
// | |
// // 大きい方の分数を返す | |
// def max(that: Rational) = | |
// if (this.lessThan(that)) that else this | |
//} | |
// < 補助コンストラクターを追加したRationalクラス | |
// 補助コンストラクターは、先頭をdef this(..)とする。 > | |
//class Rational(n: Int, d: Int) { | |
// | |
// require(d != 0) | |
// | |
// val numer: Int = n | |
// val denom: Int = d | |
// | |
// def this(n: Int) = this(n, 1) // auxiliary constructor | |
// | |
// override def toString = numer +"/"+ denom | |
// | |
// def add(that: Rational): Rational = | |
// new Rational( | |
// numer * that.denom + that.numer * denom, | |
// denom * that.denom | |
// ) | |
//} | |
// < gcdメソッドの追加 > | |
//class Rational(n: Int, d: Int) { | |
// | |
// require(d != 0) | |
// private val g = gcd(n.abs, d.abs) | |
// | |
// val numer: Int = n / g | |
// val denom: Int = d / g | |
// | |
// def this(n: Int) = this(n, 1) // auxiliary constructor | |
// | |
// override def toString = numer +"/"+ denom | |
// | |
// def add(that: Rational): Rational = | |
// new Rational( | |
// numer * that.denom + that.numer * denom, | |
// denom * that.denom | |
// ) | |
// // 約分をする | |
// private def gcd(a: Int, b: Int): Int = | |
// if (b == 0) a else gcd(b, a % b) | |
//} | |
// <add を + に変更。* を追加。> | |
//class Rational(n: Int, d: Int) { | |
// | |
// require(d != 0) | |
// private val g = gcd(n.abs, d.abs) | |
// | |
// val numer: Int = n / g | |
// val denom: Int = d / g | |
// | |
// def this(n: Int) = this(n, 1) // auxiliary constructor | |
// | |
// override def toString = numer +"/"+ denom | |
// | |
// def + (that: Rational): Rational = | |
// new Rational( | |
// numer * that.denom + that.numer * denom, | |
// denom * that.denom | |
// ) | |
// def * (that: Rational): Rational = | |
// new Rational(numer * that.numer, denom * that.denom) | |
// // 約分をする | |
// private def gcd(a: Int, b: Int): Int = | |
// if (b == 0) a else gcd(b, a % b) | |
//} | |
// < 多重定義(overload) > | |
class Rational(n: Int, d: Int) { | |
require(d != 0) | |
private val g = gcd(n.abs, d.abs) | |
val numer = n / g | |
val denom = d / g | |
def this(n: Int) = this(n, 1) | |
def + (that: Rational): Rational = | |
new Rational( | |
numer * that.denom + that.numer * denom, | |
denom * that.denom | |
) | |
def + (i: Int): Rational = | |
new Rational(numer + i * denom, denom) | |
def - (that: Rational): Rational = | |
new Rational( | |
numer * that.denom - that.numer * denom, | |
denom * that.denom | |
) | |
def - (i: Int): Rational = | |
new Rational(numer - i * denom, denom) | |
def * (that: Rational): Rational = | |
new Rational(numer * that.numer, denom * that.denom) | |
def * (i: Int): Rational = | |
new Rational(numer * i, denom) | |
def / (that: Rational): Rational = | |
new Rational(numer * that.denom, denom * that.numer) | |
def / (i: Int): Rational = | |
new Rational(numer, denom * i) | |
override def toString = numer +"/"+ denom | |
private def gcd(a: Int, b: Int): Int = | |
if (b == 0) a else gcd(b, a % b) | |
} | |
object FunctionalObjects_6 extends App{ | |
val oneHalf = new Rational(1, 2) | |
val twoThirds = new Rational(2, 3) | |
println(oneHalf + twoThirds) // 7/6 | |
println(oneHalf + oneHalf * twoThirds) // 5/6 | |
println(oneHalf + (oneHalf * twoThirds)) // 5/6 | |
println("oneHalf.numer = " + oneHalf.numer) // oneHalf.numer = 1 | |
println("oneHalf.denom = " + oneHalf.denom) // oneHalf.denom = 2 | |
// println(oneHalf lessThan twoThirds) | |
// println(oneHalf max twoThirds) | |
println(new Rational(3)) // 3/1 | |
println(new Rational(66, 42)) // 11/7 | |
println(new Rational(2, 1) + 2) // 4/1 | |
println(new Rational(2, 3) * 2) // 4/3 | |
// (Int * Rational)では引数が合わないのでエラーとなる。 | |
// 自動的に整数を分数に変換する暗黙の型変換を定義し解決。 | |
implicit def intToRational(x: Int) = new Rational(x) | |
println(2 * new Rational(2,3)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment