-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fractions
115 lines (95 loc) · 2.73 KB
/
Fractions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Fraction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fraction
{
class Program
{
static void Main(string[] args)
{
Fraction f1 = new Fraction(5, 1);
Fraction f2 = new Fraction(12, 64);
Fraction f3 = f1.Add(f2);
Console.WriteLine(f3);
try
{
Fraction f4 = new Fraction(10, 0);
Console.WriteLine(f4);
}
catch (Exception) { }
}
}
public class Fraction //or internal
{
public Fraction(int numerator, int denominator)//constructor
{
if (denominator == 0) throw new ArgumentException("Denominator could be 0");
Numerator = numerator;
Denominator = denominator;
}
public int Numerator { get; private set; }
public int Denominator { get; private set; }//private so that outside the class we can't change it
//public double Ratio => Numerator / Denominator; or
public double Ratio
{
get
{
return (double)Numerator / Denominator;//interger division is truncated by defaut
}
}
public Fraction Reciprocal
{
get
{
return new Fraction(Denominator, Numerator);
}
}
public Fraction Negative//or => new Fraction (-Numerator, Denomonator);
{
get
{
return new Fraction(-Numerator, Denominator);
}
}
public Fraction Add(Fraction other)
{
return new Fraction(Numerator * other.Numerator, Denominator * other.Denominator);
}
public Fraction Divide(Fraction other) => Add(other.Reciprocal);
public static Fraction operator +(Fraction left, Fraction right)
{
return left.Add(right);
}
public static Fraction operator -(Fraction left, Fraction right)
{
return left.Substract(right);
}
private Fraction Substract(Fraction right)
{
throw new NotImplementedException();
}
public static int LeastCommonDenominator(int a, int b)
{
int num1, num2;
if (a > b)
{
num1 = a; num2 = b;
}
else
{
num1 = b; num2 = b;
}
for (int i = 1; i < num2; i++)
{
if ((num1 * i) % num2 == 0)
{
return num1 * i;
}
}
return num1 * num2;
}
}
}