Avele

Size

May 15th, 2021 (edited)
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shapes
  4. {
  5.     struct size
  6.     {
  7.         public int H { get; set; }
  8.         public int W { get; set; }
  9.         public override string ToString()
  10.         {
  11.             return String.Format("{0} {1}", H, W);
  12.         }
  13.  
  14.         public size(int a, int b)
  15.         {
  16.             H = a;
  17.             W = b;
  18.         }
  19.  
  20.         public static size operator +(size sz1, size sz2)
  21.         {
  22.             return new size(sz1.H + sz2.H, sz1.W + sz2.W);
  23.         }
  24.     }
  25.     struct point
  26.     {
  27.         int x, y;
  28.         public override string ToString()
  29.         {
  30.             return String.Format("{0} {1}", x, y);
  31.         }
  32.  
  33.         public point(int a, int b)
  34.         {
  35.             x = a;
  36.             y = b;
  37.         }
  38.         public point(size sz)
  39.         {
  40.             x = sz.H;
  41.             y = sz.W;
  42.         }
  43.         public static point operator +(point pt, size sz)
  44.         {
  45.             return new point(pt.x + sz.H, pt.y + sz.W);
  46.         }
  47.  
  48.         public static explicit operator size(point pt)
  49.         {
  50.             return new size(pt.x, pt.y);
  51.         }
  52.     }
  53.     class SizeList
  54.     {
  55.         size[] s;
  56.         public SizeList(size[] a)
  57.         {
  58.             s = a;
  59.         }
  60.         public static SizeList operator +(SizeList sl1, SizeList sl2)
  61.         {
  62.             size[] output = new size[sl1.s.Length];
  63.             for (int i = 0; i < sl1.s.Length; i++)
  64.                 output[i] = sl1.s[i] + sl2.s[i];
  65.             return new SizeList(output);
  66.         }
  67.         public System.Collections.IEnumerator GetEnumerator()
  68.         {
  69.             for (int i = 0; i < s.Length; i++)
  70.             {
  71.                 yield return s[i];
  72.             }
  73.         }
  74.     }
  75.     class Program
  76.     {
  77.         static void Main(string[] args)
  78.         {
  79.             size s1 = new size(5, 9);
  80.             size s2 = new size(2, 1);
  81.             Console.WriteLine(s1 + s2);
  82.             point p1 = new point(3, 4);
  83.             point p2 = new point(s2 + s1);
  84.             Console.WriteLine(p2 + s2);
  85.             SizeList szl1 = new SizeList(new size[]{ s1, s2 });
  86.             SizeList szl2 = new SizeList(new size[] {(size)p1, (size)p2});
  87.             foreach (size s in szl1)
  88.                 Console.Write(s + ";");
  89.             Console.WriteLine();
  90.             foreach (size s in szl2)
  91.                 Console.Write(s + ";");
  92.             Console.WriteLine();
  93.             foreach (size s in szl1 + szl2)
  94.                 Console.Write(s + ";");
  95.         }
  96.     }
  97. }
Add Comment
Please, Sign In to add comment