|
using System;
using System.Collections.Generic;
using System.Text;
using MiscUtil;
namespace Calculator {
/// <summary>
/// Since MiscUtil doesn't include operators, I added this class to make it work.
/// This type is used by in generic classes to take the place
/// of variables of unknown type.
/// It also defines operators that can be used instead of method names.
/// </summary>
/// <typeparam name="T">Type that we will be doing arithmetic with</typeparam>
public class MiscUtilNumber<T> {
private T value;
private MiscUtilNumber(T value) {
this.value = value;
}
//IMPORTANT: The implicit operators allows an object of type Number<T> to be
//easily and seamlessly wrap an object of type T.
public static implicit operator MiscUtilNumber<T>(T a) {
return new MiscUtilNumber<T>(a);
}
//IMPORTANT: The implicit operators allows an object of type Number<T> to be
//easily and seamlessly wrap an object of type T.
public static implicit operator T(MiscUtilNumber<T> a) {
return a.value;
}
public static MiscUtilNumber<T> operator +(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
return Operator.Add(a.value, b.value);
}
public static MiscUtilNumber<T> operator -(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
return Operator.Subtract(a.value, b.value);
}
public static bool operator >(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
return Operator.GreaterThan(a.value, b.value);
}
public static bool operator <(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
return Operator.LessThan(a.value, b.value);
}
public static MiscUtilNumber<T> operator *(MiscUtilNumber<T> a, MiscUtilNumber<T> b) {
return Operator.Multiply(a.value, b.value);
}
public static MiscUtilNumber<T> operator /(MiscUtilNumber<T> a, MiscUtilNumber<T> b){
return Operator.Divide(a.value, b.value);
}
public static MiscUtilNumber<T> operator /(MiscUtilNumber<T> a, int b) {
return Operator.DivideInt32(a.value, b); ;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.