Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. class dvar
  2. {
  3.     public object val { get; set; }
  4.     public static implicit operator dvar(int s) => new dvar() { val = s };
  5.     public static implicit operator dvar(string s) => new dvar() { val = s };
  6.     public static implicit operator dvar(bool s) => new dvar() { val = s };
  7.     public static implicit operator dvar(float s) => new dvar() { val = s };
  8.     public static implicit operator dvar(double s) => new dvar() { val = s };
  9.     public static implicit operator dvar(decimal s) => new dvar() { val = s };
  10.     public static implicit operator dvar(object[] s) => new dvar() { val = s.ToList()};
  11.     public static implicit operator dvar(List<object> s) => new dvar() { val = s };
  12.  
  13.     public dvar()
  14.     {
  15.  
  16.     }
  17.  
  18.     public dvar(object s)
  19.     {
  20.         val = s;
  21.     }
  22.  
  23.     public static string listPrinter(List<object> list)
  24.     {
  25.         string op = "[";
  26.         foreach (var item in list)
  27.         {
  28.             string z = item switch
  29.             {
  30.                 dvar s => s.val switch
  31.                 {
  32.                     string c => $"\"{c}\"",
  33.                     _ => (string)s
  34.                 },
  35.                 int s => s.ToString(),
  36.                 float s => s.ToString(),
  37.                 double s => s.ToString(),
  38.                 decimal s => s.ToString(),
  39.                 bool s => s.ToString(),
  40.                 string s => $"\"{s}\"",
  41.                 IFormattable s => s.ToString(),
  42.                 _ => "object"
  43.  
  44.             };
  45.  
  46.             op += z + ",";
  47.  
  48.         }
  49.  
  50.         op = op.TrimEnd(","[0]);
  51.         op += "]";
  52.         return op;
  53.     }
  54.  
  55.     public static implicit operator string(dvar d) => d.val switch {
  56.         bool s => s.ToString(),
  57.         string s => s,
  58.         int s => s.ToString(),
  59.         decimal s => s.ToString(),
  60.         double s => s.ToString(),
  61.         float s => s.ToString(),
  62.         List<object> s => listPrinter(s),
  63.         _ => "object"
  64.     };
  65.  
  66.     public static double parseString(string str)
  67.     {
  68.         double d;
  69.         if (double.TryParse(str, out d))
  70.         {
  71.             return d;
  72.         }
  73.         return double.NaN;
  74.     }
  75.  
  76.     public static implicit operator int(dvar d) => d.val switch {
  77.         bool s => s ? 1 : 0,
  78.         string s => double.IsNaN(parseString(s)) || parseString(s) > int.MaxValue || parseString(s) < int.MinValue ? 0 : (int)parseString(s),
  79.         int s => s,
  80.         decimal s => s > int.MaxValue || s < int.MinValue ? 0 : (int)s,
  81.         double s => s > int.MaxValue || s < int.MinValue || double.IsNaN(s) || double.IsInfinity(s) ? 0 : (int)s,
  82.         float s => s > int.MaxValue || s < int.MinValue || float.IsNaN(s) || float.IsInfinity(s) ? 0 : (int)s,
  83.         List<object> s => s.Count,
  84.         _ => 0
  85.     };
  86.  
  87.     public static implicit operator bool(dvar d) => d.val switch
  88.     {
  89.         bool s => s,
  90.         string s => s == string.Empty || s == null ? false : true,
  91.         int s => s == 0 ? false : true,
  92.         decimal s => s == 0 ? false : true,
  93.         double s => s == 0 || double.IsNaN(s) ? false : true,
  94.         float s => s == 0 || float.IsNaN(s) ? false : true,
  95.         object s => s == null ? false : true,
  96.         _ => true
  97.     };
  98.  
  99.     public static implicit operator float(dvar d) => d.val switch
  100.     {
  101.         bool s => s ? 1 : 0,
  102.         string s => (float)parseString(s),
  103.         int s => s,
  104.         decimal s => (float)s,
  105.         double s => s > float.MaxValue || s < float.MinValue ? (s > float.MaxValue ? float.PositiveInfinity : float.NegativeInfinity) : (float)s,
  106.         float s => s,
  107.         _ => float.NaN
  108.     };
  109.  
  110.     public static implicit operator double(dvar d) => d.val switch
  111.     {
  112.         bool s => s ? 1 : 0,
  113.         string s => parseString(s),
  114.         int s => s,
  115.         decimal s => (double)s,
  116.         double s => s,
  117.         float s => s,
  118.         _ => double.NaN
  119.     };
  120.  
  121.     public static dvar operator +(dvar d1, dvar d2) {
  122.         if (d1.val != null && d1.val.GetType() == typeof(List<object>)) {
  123.             var list = (List<object>)d1.val;
  124.             object[] arr = new object[list.Count];
  125.             list.CopyTo(arr);
  126.  
  127.             List<object> newList = arr.ToList();
  128.             newList.Add(d2);
  129.  
  130.             return newList;
  131.         } else if (d1.val == null || d2.val == null)
  132.         {
  133.             return (double)((string)d1.val == null ? ((string)d2 == null ? (dvar)0 : d2) : d1);
  134.         }
  135.         else if (d1.val.GetType() == typeof(string) || d2.val.GetType() == typeof(string))
  136.         {
  137.             return ((string)d1) + ((string)d2);
  138.         }
  139.         else if (d1.val.GetType() == typeof(int) && d2.val.GetType() == typeof(int))
  140.         {
  141.             return (int)d1.val + (int)d2.val;
  142.         }
  143.         else
  144.         {
  145.             double a = (double)d1;
  146.             double b = (double)d2;
  147.             return a + b;
  148.         }
  149.     }
  150.  
  151.     public static dvar operator -(dvar d1, dvar d2)
  152.     {
  153.         if (d1.val == null || d2.val == null)
  154.         {
  155.             return (string)d1.val == null ? ((string)d2.val == null ? (dvar)0 : (0 - d2)) : d1;
  156.         }
  157.         else if (d1.val.GetType() == typeof(int) && d2.val.GetType() == typeof(int))
  158.         {
  159.             return (int)d1.val - (int)d2.val;
  160.         }
  161.         else
  162.         {
  163.             return (double)d1 - (double)d2;
  164.         }
  165.     }
  166.  
  167.     public static bool operator ==(dvar d1, dvar d2)
  168.     {
  169.         if (d1.val == null && d2.val == null)
  170.         {
  171.             return true;
  172.         }
  173.         else if (d1.val.GetType() == typeof(int) && d2.val.GetType() == typeof(int))
  174.         {
  175.             return (int)d1.val == (int)d2.val;
  176.         }
  177.         else
  178.         {
  179.             return (double)d1 == (double)d2;
  180.         }
  181.     }
  182.  
  183.     public static bool operator !=(dvar d1, dvar d2)
  184.     {
  185.         return !(d1 == d2);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement