Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity c# addition class

//Operator Overloads
using System;

public readonly struct Fraction
{
    private readonly int num;
    private readonly int den;

    public Fraction(int numerator, int denominator)
    {
        if (denominator == 0)
        {
            throw new ArgumentException("Denominator cannot be zero.", nameof(denominator));
        }
        num = numerator;
        den = denominator;
    }

    public static Fraction operator +(Fraction a) => a;
    public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den);

    public static Fraction operator +(Fraction a, Fraction b)
        => new Fraction(a.num * b.den + b.num * a.den, a.den * b.den);

    public static Fraction operator -(Fraction a, Fraction b)
        => a + (-b);

    public static Fraction operator *(Fraction a, Fraction b)
        => new Fraction(a.num * b.num, a.den * b.den);

    public static Fraction operator /(Fraction a, Fraction b)
    {
        if (b.num == 0)
        {
            throw new DivideByZeroException();
        }
        return new Fraction(a.num * b.den, a.den * b.num);
    }

    public override string ToString() => $"{num} / {den}";
}

public static class OperatorOverloading
{
    public static void Main()
    {
        var a = new Fraction(5, 4);
        var b = new Fraction(1, 2);
        Console.WriteLine(-a);   // output: -5 / 4
        Console.WriteLine(a + b);  // output: 14 / 8
        Console.WriteLine(a - b);  // output: 6 / 8
        Console.WriteLine(a * b);  // output: 5 / 8
        Console.WriteLine(a / b);  // output: 10 / 4
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to do if comands in c# 
Csharp :: Warum wächst Weizen besonders gut in den Steppen? 
Csharp :: Request.ServerVariables["HTTP_X_FORWARDED_FOR"] get only one ipaddress 
Csharp :: c# wirite to csv 
Csharp :: unity how to remove a tag 
Csharp :: c# datagridview color header 
Csharp :: C# inline question mark on object 
Csharp :: unity how to rotate something to point to something else 
Csharp :: hello in c# 
Csharp :: unity how to reorder a list 
Csharp :: trnasform ubnity 
Csharp :: c# serialize to xml 
Csharp :: uuid generator asp.net 
Csharp :: change picturebox image c# 
Csharp :: fair division 
Csharp :: how to do fizzbuzz in c# 
Csharp :: making a list of chars in c# 
Csharp :: get enum by index c# 
Csharp :: get logged in user name c# 
Csharp :: unity destroy all children 
Csharp :: c# enum check in string value 
Csharp :: c# repeat x times 
Csharp :: c# convert object to string 
Csharp :: c# datetimepicker set weeks after today 
Csharp :: c# winforms textbox to int 
Csharp :: words counter c# 
Csharp :: varibles c# 
Csharp :: decimal to string c# 
Csharp :: how to make error sound c# 
Csharp :: C# push list 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =