Перегрузка операторов сравнения - C# (215056)
Формулировка задачи:
Привет, делаю упражнение с дробями, перегрузку арифметических операторов сделал, а со сравнением не получается, подскажите как нужно делать.
Вот код того что уже есть:
пробовал написать так:
выдает ошибку - The operator 'Fraction.Fraction.operator <(Fraction.Fraction, Fraction.Fraction)' requires a matching operator '>' to also be defined
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fraction
{
internal class Fraction
{
public Fraction()
{
x = 0;
y = 1;
}
public Fraction(long X, uint Y)
{
x = X;
y = Y;
}
private long x;
private uint y;
public float getFrac()
{
float x1=x;
return x1/y;
}
public static Fraction operator +(Fraction f1, Fraction f2)
{
Fraction th=new Fraction();
th.x = (f1.x*f2.y) + (f2.x*f1.y);
th.y = f1.y*f2.y;
return th;
}
public static Fraction operator -(Fraction f1, Fraction f2)
{
Fraction th = new Fraction();
th.x = (f1.x * f2.y) - (f2.x * f1.y);
th.y = f1.y * f2.y;
return th;
}
public static Fraction operator *(Fraction f1, Fraction f2)
{
Fraction th = new Fraction();
th.x = f1.x * f2.x;
th.y = f1.y * f2.y;
return th;
}
public static Fraction operator /(Fraction f1, Fraction f2)
{
Fraction th = new Fraction();
th.x = f1.x * f2.y;
th.y = f1.y * (uint)f2.x;
return th;
}
}
class Program
{
static void Main(string[] args)
{
Fraction d1=new Fraction(1,2);
Fraction d2 = new Fraction(1,4);
Fraction d3 = new Fraction(1, 2);
Fraction d = new Fraction();
d = d1 + d2+d3;
Console.WriteLine(d.getFrac());
Console.ReadKey();
}
}
}
public static bool operator < (Fraction f1, Fraction f2)
{
if (f1.getFrac() < f2.getFrac())
return true;
else
return false;
}Решение задачи: «Перегрузка операторов сравнения»
textual
Листинг программы
X = X/X;