Skip to content

Commit

Permalink
#18 記法ミスの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
frog3141 committed Jun 13, 2022
1 parent 872f997 commit 0b92710
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
101 changes: 50 additions & 51 deletions lib/model/fruction.dart
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;
}
}
13 changes: 6 additions & 7 deletions test/model/fraction_test.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import 'dart:math';
import 'package:flutter_test/flutter_test.dart';
import 'package:calculator2022/model/fruction.dart';

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

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

0 comments on commit 0b92710

Please sign in to comment.