Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace SAS.Core
- {
- public interface ICalculator<T> where T : IComparable<T>
- {
- T Sum(T a, T b);
- T Difference(T a, T b);
- T Product(T a, T b);
- T Quotient(T a, T b);
- T Zero { get;}
- T One { get;}
- T Ten { get;}
- T Hundred { get;}
- T Parse(string str);
- T Convert(object obj);
- }
- public static class Calculators
- {
- public static ICalculator<T> GetCalculator<T>() where T : IComparable<T>
- {
- if (typeof(T) == typeof(int)) {
- return new CalculatorInt() as ICalculator<T>;
- }
- if (typeof(T) == typeof(float)) {
- return new CalculatorFloat() as ICalculator<T>;
- }
- if (typeof(T) == typeof(double)) {
- return new CalculatorDouble() as ICalculator<T>;
- }
- if (typeof(T) == typeof(decimal)) {
- return new CalculatorDecimal() as ICalculator<T>;
- }
- return null;
- }
- }
- public struct CalculatorInt : ICalculator<int>
- {
- #region ICalculator<int> Members
- public int Sum(int a, int b)
- {
- return a + b;
- }
- public int Difference(int a, int b)
- {
- return a - b;
- }
- public int Product(int a, int b)
- {
- return a * b;
- }
- public int Quotient(int a, int b)
- {
- return a / b;
- }
- public int Zero
- {
- get { return 0; }
- }
- public int One
- {
- get { return 1; }
- }
- public int Ten
- {
- get { return 10; }
- }
- public int Hundred
- {
- get { return 100; }
- }
- public int Parse(string str)
- {
- return int.Parse(str);
- }
- public int Convert(object obj) {
- return System.Convert.ToInt32(obj);
- }
- #endregion
- }
- public struct CalculatorFloat : ICalculator<float>
- {
- #region ICalculator<float> Members
- public float Sum(float a, float b)
- {
- return a + b;
- }
- public float Difference(float a, float b)
- {
- return a - b;
- }
- public float Product(float a, float b)
- {
- return a * b;
- }
- public float Quotient(float a, float b)
- {
- return a / b;
- }
- public float Zero
- {
- get { return 0.0f; }
- }
- public float One
- {
- get { return 1.0f; }
- }
- public float Ten
- {
- get { return 10.0f; }
- }
- public float Hundred
- {
- get { return 100.0f; }
- }
- public float Parse(string str)
- {
- return float.Parse(str);
- }
- public float Convert(object obj) {
- return System.Convert.ToSingle(obj);
- }
- #endregion
- }
- public struct CalculatorDecimal : ICalculator<decimal> {
- #region ICalculator<decimal> Members
- public decimal Sum(decimal a, decimal b) {
- return a + b;
- }
- public decimal Difference(decimal a, decimal b) {
- return a - b;
- }
- public decimal Product(decimal a, decimal b) {
- return a * b;
- }
- public decimal Quotient(decimal a, decimal b) {
- return a / b;
- }
- public decimal Zero {
- get { return 0.0m; }
- }
- public decimal One {
- get { return 1.0m; }
- }
- public decimal Ten {
- get { return 10.0m; }
- }
- public decimal Hundred {
- get { return 100.0m; }
- }
- public decimal Parse(string str) {
- return decimal.Parse(str);
- }
- public decimal Convert(object obj) {
- return System.Convert.ToDecimal(obj);
- }
- #endregion
- }
- public struct CalculatorDouble : ICalculator<double>
- {
- #region ICalculator<double> Members
- public double Sum(double a, double b)
- {
- return a + b;
- }
- public double Difference(double a, double b)
- {
- return a - b;
- }
- public double Product(double a, double b)
- {
- return a * b;
- }
- public double Quotient(double a, double b)
- {
- return a / b;
- }
- public double Zero
- {
- get { return 0; }
- }
- public double One
- {
- get { return 1; }
- }
- public double Ten
- {
- get { return 10; }
- }
- public double Hundred
- {
- get { return 100; }
- }
- public double Parse(string str)
- {
- return double.Parse(str);
- }
- public double Convert(object obj) {
- return System.Convert.ToDouble(obj);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement