-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
58 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,96 @@ | ||
class Fruction { | ||
int Numerator, Denominator; | ||
Fruction(this.Numerator, this.Denominator) { | ||
if (Denominator == 0) { | ||
int numerator, denominator; | ||
Fruction(this.numerator, this.denominator) { | ||
if (denominator == 0) { | ||
throw ArgumentError("分母が0になっています"); | ||
} else if (Denominator < 0) { | ||
Denominator *= -1; | ||
Numerator *= -1; | ||
} else if (denominator < 0) { | ||
denominator *= -1; | ||
numerator *= -1; | ||
} | ||
Reduce(); | ||
reduce(); | ||
} | ||
|
||
///約分する | ||
void Reduce() { | ||
int gcd = Gcd(Numerator, Denominator); | ||
Numerator = (Numerator / gcd).toInt(); | ||
Denominator = (Denominator / gcd).toInt(); | ||
void reduce() { | ||
int _gcd = gcd(numerator, denominator); | ||
numerator = numerator ~/ _gcd; | ||
denominator = denominator ~/ _gcd; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return "$Numerator/$Denominator"; | ||
return "$numerator/$denominator"; | ||
} | ||
|
||
///最大公約数を求める | ||
int Gcd(int a, int b) { | ||
return ((a % b) == 0) ? b : Gcd(b, (a % b)); | ||
int gcd(int a, int b) { | ||
return ((a % b) == 0) ? b : gcd(b, (a % b)); | ||
} | ||
|
||
@override | ||
int get hashCode { | ||
var result = 3; | ||
result = 31 * result + Numerator.hashCode; | ||
result = 31 * result + Denominator.hashCode; | ||
result = 31 * result + numerator.hashCode; | ||
result = 31 * result + denominator.hashCode; | ||
return result; | ||
//return Object.hash(Numerator, Denominator); | ||
} | ||
|
||
//以下演算子のオーバーロード | ||
Fruction operator +(dynamic a) { | ||
if (a.runtimeType == Fruction) { | ||
Fruction i = a; | ||
return Fruction(Numerator * i.Denominator + i.Numerator * Denominator, | ||
Denominator * i.Denominator); | ||
} else if (a.runtimeType == int) { | ||
int i = a; | ||
return Fruction(Numerator + i * Denominator, Denominator); | ||
Fruction operator +(other) { | ||
if (other.runtimeType == Fruction) { | ||
Fruction i = other; | ||
return Fruction(numerator * i.denominator + i.numerator * denominator, | ||
denominator * i.denominator); | ||
} else if (other.runtimeType == int) { | ||
int i = other; | ||
return Fruction(numerator + i * denominator, denominator); | ||
} else { | ||
throw ArgumentError("無効な計算です"); | ||
} | ||
} | ||
|
||
Fruction operator -(dynamic a) { | ||
if (a.runtimeType == Fruction) { | ||
Fruction i = a; | ||
return Fruction(Numerator * i.Denominator - i.Numerator * Denominator, | ||
Denominator * i.Denominator); | ||
} else if (a.runtimeType == int) { | ||
int i = a; | ||
return Fruction(Numerator - i * Denominator, Denominator); | ||
Fruction operator -(other) { | ||
if (other.runtimeType == Fruction) { | ||
Fruction i = other; | ||
return Fruction(numerator * i.denominator - i.numerator * denominator, | ||
denominator * i.denominator); | ||
} else if (other.runtimeType == int) { | ||
int i = other; | ||
return Fruction(numerator - i * denominator, denominator); | ||
} else { | ||
throw ArgumentError("無効な計算です"); | ||
} | ||
} | ||
|
||
Fruction operator *(dynamic a) { | ||
if (a.runtimeType == Fruction) { | ||
Fruction i = a; | ||
return Fruction(i.Numerator * Numerator, i.Denominator * Numerator); | ||
} else if (a.runtimeType == int) { | ||
int i = a; | ||
return Fruction(Numerator * i, Denominator); | ||
Fruction operator *(other) { | ||
if (other.runtimeType == Fruction) { | ||
Fruction i = other; | ||
return Fruction(i.numerator * numerator, i.denominator * numerator); | ||
} else if (other.runtimeType == int) { | ||
int i = other; | ||
return Fruction(numerator * i, denominator); | ||
} else { | ||
throw ArgumentError("無効な計算です"); | ||
} | ||
} | ||
|
||
Fruction operator /(dynamic a) { | ||
if (a.runtimeType == Fruction) { | ||
Fruction i = a; | ||
return Fruction( | ||
this.Numerator * i.Denominator, this.Denominator * i.Numerator); | ||
} else if (a.runtimeType == int) { | ||
int i = a; | ||
return Fruction(Numerator, Denominator * i); | ||
Fruction operator /(other) { | ||
if (other.runtimeType == Fruction) { | ||
Fruction i = other; | ||
return Fruction(numerator * i.denominator, denominator * i.numerator); | ||
} else if (other.runtimeType == int) { | ||
int i = other; | ||
return Fruction(numerator, denominator * i); | ||
} else { | ||
throw ArgumentError("無効な計算です"); | ||
} | ||
} | ||
|
||
@override | ||
bool operator ==(dynamic a) { | ||
return a is Fruction && | ||
a.Denominator == Denominator && | ||
a.Numerator == Numerator; | ||
bool operator ==(other) { | ||
return other is Fruction && | ||
other.denominator == denominator && | ||
other.numerator == numerator; | ||
} | ||
} |
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