Skip to content

Commit

Permalink
#18 ==演算子、int型との四則演算を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
frog3141 committed Jun 13, 2022
1 parent df2f142 commit 872f997
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
76 changes: 61 additions & 15 deletions lib/model/fruction.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class Fruction {
int Numerator, Denominator;
Fruction(this.Numerator, this.Denominator) {
final numrator = this.Numerator;
if (Denominator == 0) {
throw ArgumentError("分母が0になっています");
} else if (Denominator < 0) {
Denominator *= -1;
Numerator *= -1;
}
Reduce();
}

///約分する
void Reduce() {
int gcd = Gcd(Numerator, Denominator);
Numerator = (Numerator / gcd).toInt();
Expand All @@ -15,6 +22,7 @@ class Fruction {
return "$Numerator/$Denominator";
}

///最大公約数を求める
int Gcd(int a, int b) {
return ((a % b) == 0) ? b : Gcd(b, (a % b));
}
Expand All @@ -25,27 +33,65 @@ class Fruction {
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);
} else {
throw ArgumentError("無効な計算です");
}
}

Fruction operator +(Fruction i) {
return Fruction(
this.Numerator * i.Denominator + i.Numerator * this.Denominator,
this.Denominator * i.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);
} else {
throw ArgumentError("無効な計算です");
}
}

Fruction operator -(Fruction i) {
return Fruction(
this.Numerator * i.Denominator - i.Numerator * this.Denominator,
this.Denominator * i.Denominator);
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);
} else {
throw ArgumentError("無効な計算です");
}
}

Fruction operator *(Fruction i) {
return Fruction(
i.Numerator * this.Numerator, i.Denominator * this.Numerator);
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);
} else {
throw ArgumentError("無効な計算です");
}
}

Fruction operator /(Fruction i) {
return Fruction(
this.Numerator * this.Denominator, this.Denominator * i.Numerator);
@override
bool operator ==(dynamic a) {
return a is Fruction &&
a.Denominator == Denominator &&
a.Numerator == Numerator;
}
}
15 changes: 12 additions & 3 deletions test/model/fraction_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import 'dart:math';
import 'package:flutter_test/flutter_test.dart';
import 'package:calculator2022/model/fruction.dart';

void main() {
test("fruction_test", () {
test("fruction_and_fruction", () {
final A = Fruction(2, 3);
final B = Fruction(4, 3);
final result = A / B;
//result.Reduce();
print(result);
final anser = Fruction(1, 2);
expect(result, anser);
});

test("fructionfruction_and_int", () {
final A = Fruction(2, 3);
final B = 3;
final result = A / B;
Fruction anser = Fruction(2, 9);
expect(result, anser);
});
}

0 comments on commit 872f997

Please sign in to comment.