From 0b927108b52e51216ffc4da29b0e220f736511db Mon Sep 17 00:00:00 2001 From: frog3141 Date: Mon, 13 Jun 2022 23:13:53 +0900 Subject: [PATCH] =?UTF-8?q?#18=20=E8=A8=98=E6=B3=95=E3=83=9F=E3=82=B9?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/model/fruction.dart | 101 +++++++++++++++++----------------- test/model/fraction_test.dart | 13 ++--- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/lib/model/fruction.dart b/lib/model/fruction.dart index d6df552..df9a2cc 100644 --- a/lib/model/fruction.dart +++ b/lib/model/fruction.dart @@ -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; } } diff --git a/test/model/fraction_test.dart b/test/model/fraction_test.dart index e648758..e32f3d1 100644 --- a/test/model/fraction_test.dart +++ b/test/model/fraction_test.dart @@ -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); });