Skip to content

Commit 5d0708f

Browse files
Added FractionMath in C# (#4040)
1 parent 3088421 commit 5d0708f

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

archive/c/c-sharp/FractionMath.cs

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
using System;
2+
3+
namespace SamplePrograms
4+
{
5+
public class FractionMath
6+
{
7+
private int numerator;
8+
private int denominator;
9+
10+
public FractionMath(int numerator = 0, int denominator = 1)
11+
{
12+
if (denominator == 0)
13+
{
14+
throw new ArgumentException("Denominator cannot be zero.");
15+
}
16+
17+
this.numerator = numerator;
18+
this.denominator = denominator;
19+
}
20+
21+
// GCD method using the Euclidean algorithm in an iterative approach
22+
// Orginal algorithm was found on GeeksforGeeks, modified for clarity:
23+
// https://www.geeksforgeeks.org/program-to-find-gcd-or-hcf-of-two-numbers/#
24+
private int GCD(int x, int y)
25+
{
26+
while (y != 0)
27+
{
28+
int z = x;
29+
x = y;
30+
y = z % y;
31+
}
32+
return x;
33+
}
34+
35+
private void Simplify()
36+
{
37+
int gcd = GCD(numerator, denominator);
38+
numerator /= gcd;
39+
denominator /= gcd;
40+
41+
if (denominator < 0)
42+
{
43+
numerator = -numerator;
44+
denominator = -denominator;
45+
}
46+
}
47+
48+
public override string ToString()
49+
{
50+
Simplify();
51+
return $"{numerator}/{denominator}";
52+
}
53+
54+
public static FractionMath Parse(string fractionString)
55+
{
56+
string[] numbers = fractionString.Split('/');
57+
if (numbers.Length != 2)
58+
{
59+
throw new FormatException("Invalid fraction. A format of 'numerator/denominator' is expected.");
60+
}
61+
62+
int numerator = int.Parse(numbers[0]);
63+
int denominator = int.Parse(numbers[1]);
64+
65+
return new FractionMath(numerator, denominator);
66+
}
67+
68+
public static FractionMath operator +(FractionMath f1, FractionMath f2)
69+
{
70+
int newNumerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
71+
int newDenominator = f1.denominator * f2.denominator;
72+
return new FractionMath(newNumerator, newDenominator);
73+
}
74+
75+
public static FractionMath operator -(FractionMath f1, FractionMath f2)
76+
{
77+
int newNumerator = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
78+
int newDenominator = f1.denominator * f2.denominator;
79+
return new FractionMath(newNumerator, newDenominator);
80+
}
81+
82+
public static FractionMath operator *(FractionMath f1, FractionMath f2)
83+
{
84+
int newNumerator = f1.numerator * f2.numerator;
85+
int newDenominator = f1.denominator * f2.denominator;
86+
return new FractionMath(newNumerator, newDenominator);
87+
}
88+
89+
public static FractionMath operator /(FractionMath f1, FractionMath f2)
90+
{
91+
int newNumerator = f1.numerator * f2.denominator;
92+
int newDenominator = f1.denominator * f2.numerator;
93+
return new FractionMath(newNumerator, newDenominator);
94+
}
95+
96+
public static bool operator ==(FractionMath f1, FractionMath f2)
97+
{
98+
return f1.numerator * f2.denominator == f1.denominator * f2.numerator;
99+
}
100+
101+
public static bool operator !=(FractionMath f1, FractionMath f2)
102+
{
103+
return !(f1 == f2);
104+
}
105+
106+
public static bool operator >(FractionMath f1, FractionMath f2)
107+
{
108+
return f1.numerator * f2.denominator > f1.denominator * f2.numerator;
109+
}
110+
111+
public static bool operator <(FractionMath f1, FractionMath f2)
112+
{
113+
return f1.numerator * f2.denominator < f1.denominator * f2.numerator;
114+
}
115+
116+
public static bool operator >=(FractionMath f1, FractionMath f2)
117+
{
118+
return f1 > f2 || f1 == f2;
119+
}
120+
121+
public static bool operator <=(FractionMath f1, FractionMath f2)
122+
{
123+
return f1 < f2 || f1 == f2;
124+
}
125+
126+
public static void Main(string[] args)
127+
{
128+
if (args.Length != 3)
129+
{
130+
Console.WriteLine("Usage: ./fraction-math operand1 operator operand2");
131+
return;
132+
}
133+
134+
try
135+
{
136+
FractionMath operand1 = Parse(args[0]);
137+
string operation = args[1];
138+
FractionMath operand2 = Parse(args[2]);
139+
140+
FractionMath result;
141+
bool comparisonResult;
142+
143+
switch (operation)
144+
{
145+
case "+":
146+
result = operand1 + operand2;
147+
Console.WriteLine(result);
148+
break;
149+
case "-":
150+
result = operand1 - operand2;
151+
Console.WriteLine(result);
152+
break;
153+
case "*":
154+
result = operand1 * operand2;
155+
Console.WriteLine(result);
156+
break;
157+
case "/":
158+
result = operand1 / operand2;
159+
Console.WriteLine(result);
160+
break;
161+
case "==":
162+
comparisonResult = operand1 == operand2;
163+
Console.WriteLine(comparisonResult ? "1" : "0");
164+
break;
165+
case "!=":
166+
comparisonResult = operand1 != operand2;
167+
Console.WriteLine(comparisonResult ? "1" : "0");
168+
break;
169+
case ">":
170+
comparisonResult = operand1 > operand2;
171+
Console.WriteLine(comparisonResult ? "1" : "0");
172+
break;
173+
case "<":
174+
comparisonResult = operand1 < operand2;
175+
Console.WriteLine(comparisonResult ? "1" : "0");
176+
break;
177+
case ">=":
178+
comparisonResult = operand1 >= operand2;
179+
Console.WriteLine(comparisonResult ? "1" : "0");
180+
break;
181+
case "<=":
182+
comparisonResult = operand1 <= operand2;
183+
Console.WriteLine(comparisonResult ? "1" : "0");
184+
break;
185+
default:
186+
Console.WriteLine($"Error: Invalid operator '{operation}'");
187+
break;
188+
}
189+
}
190+
catch (Exception e)
191+
{
192+
Console.WriteLine($"Error: {e.Message}");
193+
}
194+
}
195+
}
196+
}

0 commit comments

Comments
 (0)