Advertisement
Guest User

Calculator

a guest
Jan 18th, 2012
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace SAS.Core
  6. {
  7.     public interface ICalculator<T> where T : IComparable<T>
  8.     {
  9.         T Sum(T a, T b);
  10.         T Difference(T a, T b);
  11.         T Product(T a, T b);
  12.         T Quotient(T a, T b);
  13.         T Zero { get;}
  14.         T One { get;}
  15.         T Ten { get;}
  16.         T Hundred { get;}
  17.         T Parse(string str);
  18.         T Convert(object obj);      
  19.     }
  20.  
  21.     public static class Calculators
  22.     {
  23.         public static ICalculator<T> GetCalculator<T>() where T : IComparable<T>
  24.         {
  25.             if (typeof(T) == typeof(int)) {
  26.                 return new CalculatorInt() as ICalculator<T>;
  27.             }
  28.             if (typeof(T) == typeof(float)) {
  29.                 return new CalculatorFloat() as ICalculator<T>;
  30.             }
  31.             if (typeof(T) == typeof(double)) {
  32.                 return new CalculatorDouble() as ICalculator<T>;
  33.             }
  34.             if (typeof(T) == typeof(decimal)) {
  35.                 return new CalculatorDecimal() as ICalculator<T>;
  36.             }
  37.             return null;
  38.         }
  39.     }
  40.  
  41.     public struct CalculatorInt : ICalculator<int>
  42.     {
  43.         #region ICalculator<int> Members
  44.  
  45.         public int Sum(int a, int b)
  46.         {
  47.             return a + b;
  48.         }
  49.  
  50.         public int Difference(int a, int b)
  51.         {
  52.             return a - b;
  53.         }
  54.  
  55.         public int Product(int a, int b)
  56.         {
  57.             return a * b;
  58.         }
  59.  
  60.         public int Quotient(int a, int b)
  61.         {
  62.             return a / b;
  63.         }
  64.  
  65.         public int Zero
  66.         {
  67.             get { return 0; }
  68.         }
  69.  
  70.         public int One
  71.         {
  72.             get { return 1; }
  73.         }
  74.  
  75.         public int Ten
  76.         {
  77.             get { return 10; }
  78.         }
  79.  
  80.         public int Hundred
  81.         {
  82.             get { return 100; }
  83.         }
  84.  
  85.         public int Parse(string str)
  86.         {
  87.             return int.Parse(str);
  88.         }
  89.  
  90.         public int Convert(object obj) {
  91.             return System.Convert.ToInt32(obj);
  92.         }
  93.  
  94.         #endregion
  95.     }
  96.  
  97.     public struct CalculatorFloat : ICalculator<float>
  98.     {
  99.         #region ICalculator<float> Members
  100.  
  101.         public float Sum(float a, float b)
  102.         {
  103.             return a + b;
  104.         }
  105.  
  106.         public float Difference(float a, float b)
  107.         {
  108.             return a - b;
  109.         }
  110.  
  111.         public float Product(float a, float b)
  112.         {
  113.             return a * b;
  114.         }
  115.  
  116.         public float Quotient(float a, float b)
  117.         {
  118.             return a / b;
  119.         }
  120.  
  121.         public float Zero
  122.         {
  123.             get { return 0.0f; }
  124.         }
  125.  
  126.         public float One
  127.         {
  128.             get { return 1.0f; }
  129.         }
  130.  
  131.         public float Ten
  132.         {
  133.             get { return 10.0f; }
  134.         }
  135.  
  136.         public float Hundred
  137.         {
  138.             get { return 100.0f; }
  139.         }
  140.  
  141.         public float Parse(string str)
  142.         {
  143.             return float.Parse(str);
  144.         }
  145.  
  146.         public float Convert(object obj) {
  147.             return System.Convert.ToSingle(obj);
  148.         }
  149.         #endregion
  150.     }
  151.  
  152.     public struct CalculatorDecimal : ICalculator<decimal> {
  153.         #region ICalculator<decimal> Members
  154.  
  155.         public decimal Sum(decimal a, decimal b) {
  156.             return a + b;
  157.         }
  158.  
  159.         public decimal Difference(decimal a, decimal b) {
  160.             return a - b;
  161.         }
  162.  
  163.         public decimal Product(decimal a, decimal b) {
  164.             return a * b;
  165.         }
  166.  
  167.         public decimal Quotient(decimal a, decimal b) {
  168.             return a / b;
  169.         }
  170.  
  171.         public decimal Zero {
  172.             get { return 0.0m; }
  173.         }
  174.  
  175.         public decimal One {
  176.             get { return 1.0m; }
  177.         }
  178.  
  179.         public decimal Ten {
  180.             get { return 10.0m; }
  181.         }
  182.  
  183.         public decimal Hundred {
  184.             get { return 100.0m; }
  185.         }
  186.  
  187.         public decimal Parse(string str) {
  188.             return decimal.Parse(str);
  189.         }
  190.  
  191.         public decimal Convert(object obj) {
  192.             return System.Convert.ToDecimal(obj);
  193.         }
  194.  
  195.         #endregion
  196.     }
  197.  
  198.     public struct CalculatorDouble : ICalculator<double>
  199.     {
  200.         #region ICalculator<double> Members
  201.  
  202.         public double Sum(double a, double b)
  203.         {
  204.             return a + b;
  205.         }
  206.  
  207.         public double Difference(double a, double b)
  208.         {
  209.             return a - b;
  210.         }
  211.  
  212.         public double Product(double a, double b)
  213.         {
  214.             return a * b;
  215.         }
  216.  
  217.         public double Quotient(double a, double b)
  218.         {
  219.             return a / b;
  220.         }
  221.  
  222.         public double Zero
  223.         {
  224.             get { return 0; }
  225.         }
  226.  
  227.         public double One
  228.         {
  229.             get { return 1; }
  230.         }
  231.  
  232.         public double Ten
  233.         {
  234.             get { return 10; }
  235.         }
  236.  
  237.         public double Hundred
  238.         {
  239.             get { return 100; }
  240.         }
  241.  
  242.         public double Parse(string str)
  243.         {
  244.             return double.Parse(str);
  245.         }
  246.  
  247.         public double Convert(object obj) {
  248.             return System.Convert.ToDouble(obj);
  249.         }
  250.         #endregion      
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement