Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Triangle : IComparable<Triangle>
- {
- public Triangle(double side, double h)
- {
- this.SideA = side;
- this.H = h;
- this.Surface = this.FindSurfaceBySideAndH();
- }
- public Triangle(double sideA, double sideB, double sideC)
- {
- this.SideA = sideA;
- this.SideB = sideB;
- this.SideC = sideC;
- this.Surface = this.FindSurfaceByThreeSides();
- }
- public double SideA { get; protected set; }
- public double SideB { get; protected set; }
- public double SideC { get; protected set; }
- public double H { get; protected set; }
- public double Surface { get; protected set; }
- private double FindSurfaceBySideAndH()
- {
- return (SideA * H) / 2;
- }
- protected double FindSurfaceByThreeSides()
- {
- double p = (SideA + SideB + SideC) / 2;
- return Math.Sqrt(p * (p - SideA) * (p - SideB) * (p - SideC));
- }
- public int CompareTo(Triangle other)
- {
- return other.Surface.CompareTo(Surface);
- }
- public override string ToString()
- {
- if (SideA > 0 && SideB > 0 && SideC > 0)
- {
- return String.Format("a = {0}, b = {1}, c = {2}, S = {3}",
- SideA, SideB, SideC, Surface);
- }
- return String.Format("a = {0}, h = {1}, S = {2}",
- SideA, H, Surface);
- }
- }
- class Pyramid : Triangle
- {
- public Pyramid(double sideA, double sideB, double sideC, double h)
- : base(sideA, sideB, sideC)
- {
- this.FindSurfaceByThreeSides();
- this.H = h;
- this.Volume = this.FindVolume();
- }
- public double Volume { get; private set; }
- private double FindVolume()
- {
- return this.Surface * H / 3;
- }
- public int CompareTo(Pyramid other)
- {
- return other.Volume.CompareTo(this.Volume);
- }
- public override string ToString()
- {
- return String.Format("a = {0}, b = {1}, c = {2}, h = {3}, V = {4}",
- SideA, SideB, SideC, H, this.Volume);
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- // List<Triangle> list = new List<Triangle>();
- // list.Add(new Triangle(7, 3));
- // list.Add(new Triangle(3, 4, 5));
- // list.Add(new Triangle(8, 5));
- //
- // list.Sort();
- //
- // Console.WriteLine(String.Join("\n\n", list));
- //
- // Console.WriteLine();
- List<Pyramid> listOfPyramids = new List<Pyramid>();
- listOfPyramids.Add(new Pyramid(2, 3, 5, 9));
- listOfPyramids.Add(new Pyramid(3, 4, 5, 8));
- listOfPyramids.Add(new Pyramid(1, 3, 5, 10));
- listOfPyramids.Sort();
- Console.WriteLine(String.Join("\n\n", listOfPyramids));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment