MiroslavBr

triangle 14.11.20205

Nov 14th, 2025 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _00.Demo
  9. {
  10.     internal class Triangle
  11.     {
  12.         private double _a;
  13.         private double _b;
  14.         private double _c;
  15.  
  16.         private void ValidateSide(double value, string sideName)
  17.         {
  18.             if (value <= 0)
  19.             {
  20.                 throw new ArgumentException($"The side {sideName} has to be more than zero");
  21.             }
  22.         }
  23.         public double A
  24.         {
  25.             get
  26.             {
  27.                 return this._a;
  28.             }
  29.             private set
  30.             {
  31.                 ValidateSide(value, nameof(A));
  32.                 this._a = value;
  33.             }
  34.         }
  35.         public double B
  36.         {
  37.             get
  38.             {
  39.                 return this._b;
  40.             }
  41.             private set
  42.             {
  43.                 ValidateSide(value, nameof(B));
  44.                 this._b = value;
  45.             }
  46.         }
  47.         public double C
  48.         {
  49.             get
  50.             {
  51.                 return this._c;
  52.             }
  53.             private set
  54.             {
  55.                 ValidateSide(value, nameof(A));
  56.                 this._c = value;
  57.             }
  58.         }
  59.  
  60.         public Triangle(double a, double b, double c)
  61.         {
  62.             if (a + c <= b ||
  63.                 b + c <= a ||
  64.                 a + b <= c)
  65.             {
  66.                 throw new ArgumentException("The triangle does not exist");
  67.             }
  68.  
  69.             this.A = a;
  70.             this.B = b;
  71.             this.C = c;
  72.         }
  73.  
  74.         public double P()
  75.         {
  76.             return this.A + this.B + this.C;
  77.         }
  78.         public double S()
  79.         {
  80.             double p = P() / 2;
  81.             double S = Math.Sqrt(p * (p - this.A) * (p - this.B) * (p - this.C));
  82.             return S;
  83.         }
  84.  
  85.         public override string ToString()
  86.         {
  87.             return $"Triangle with sides:\na: {this.A}\nb: {this.B}\nc: {this.C}";
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93. namespace _00.Demo
  94. {
  95.     internal class Program
  96.     {
  97.         static void Main(string[] args)
  98.         {
  99.             double a = double.Parse(Console.ReadLine());
  100.             double b = double.Parse(Console.ReadLine());
  101.             double c = double.Parse(Console.ReadLine());
  102.             Triangle triangle = new(a, b, c);
  103.             Console.WriteLine(triangle.ToString());
  104.             Console.WriteLine("S= " + triangle.S());
  105.             Console.WriteLine("P= " + triangle.P());
  106.         }
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment