Advertisement
YavorGrancharov

Boxes(class)

Jul 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Boxes
  6. {
  7.     class Box
  8.     {
  9.         public Point UpperLeft { get; set; }
  10.  
  11.         public Point UpperRight { get; set; }
  12.  
  13.         public Point BottomLeft { get; set; }
  14.  
  15.         public Point BottomRight { get; set; }
  16.  
  17.         public int Width { get; set; }
  18.  
  19.         public int Height { get; set; }
  20.  
  21.         public static int CalculatePerimeter(int width, int height)
  22.         {
  23.             int perimeter = (2 * width) + (2 * height);
  24.             return perimeter;
  25.         }
  26.  
  27.         public static int CalculateArea(int width, int height)
  28.         {
  29.             int area = width * height;
  30.             return area;
  31.         }
  32.     }
  33.  
  34.     class Point
  35.     {
  36.         public int X { get; set; }
  37.  
  38.         public int Y { get; set; }
  39.  
  40.         public static int CalcDistance(Point poin1, Point poin2)
  41.         {
  42.             int x = (int)Math.Pow(poin1.X - poin2.X, 2);
  43.             int y = (int)Math.Pow(poin1.Y - poin2.Y, 2);
  44.             int distance = (int)Math.Sqrt(x + y);
  45.  
  46.             return distance;
  47.         }
  48.     }
  49.    
  50.     class Program
  51.     {              
  52.         static void Main(string[] args)
  53.         {
  54.             List<Box> boxes = new List<Box>();
  55.  
  56.             string input = Console.ReadLine();
  57.  
  58.             while (input != "end")
  59.             {
  60.                 int[] tokens = input
  61.                     .Split(new [] { ' ', ':', '|' },
  62.                     StringSplitOptions.RemoveEmptyEntries)
  63.                     .Select(int.Parse)
  64.                     .ToArray();
  65.  
  66.                 Point upperLeft = new Point
  67.                 {
  68.                     X = tokens[0],
  69.                     Y = tokens[1]
  70.                 };
  71.  
  72.                 Point upperRight = new Point
  73.                 {
  74.                     X = tokens[2],
  75.                     Y = tokens[3]
  76.                 };
  77.  
  78.                 Point bottomLeft = new Point
  79.                 {
  80.                     X = tokens[4],
  81.                     Y = tokens[5]
  82.                 };
  83.  
  84.                 Point bottomRight = new Point
  85.                 {
  86.                     X = tokens[6],
  87.                     Y = tokens[7]
  88.                 };
  89.  
  90.                 Box newBox = new Box
  91.                 {
  92.                     UpperLeft = upperLeft,
  93.                     UpperRight = upperRight,
  94.                     BottomLeft = bottomLeft,
  95.                     BottomRight = bottomRight
  96.                 };
  97.  
  98.                 boxes.Add(newBox);
  99.  
  100.                 input = Console.ReadLine();
  101.             }
  102.  
  103.             foreach (var box in boxes)
  104.             {
  105.                 int width = Point.CalcDistance(box.UpperLeft, box.UpperRight);
  106.                 int height = Point.CalcDistance(box.UpperLeft, box.BottomLeft);
  107.                 int perimeter = Box.CalculatePerimeter(width, height);
  108.                 int area = Box.CalculateArea(width, height);
  109.  
  110.                 Console.WriteLine("Box: {0}, {1}", width, height);
  111.                 Console.WriteLine("Perimeter: {0}", perimeter);
  112.                 Console.WriteLine("Area: {0}", area);
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement