HaoAsakura

123

Sep 26th, 2023
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TestFieldIthink
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.WriteLine("[?] Выберите фигуру");
  10.             Console.WriteLine("[1] Призма");
  11.             Console.WriteLine("[2] Конус");
  12.  
  13.             int choice = int.Parse(Console.ReadLine());
  14.            
  15.             double a, b, h, r;
  16.  
  17.             switch (choice)
  18.             {
  19.                 case 1:
  20.                     Console.Write("[*] Введите длину: ");
  21.                     a = double.Parse(Console.ReadLine());
  22.                     Console.Write("[*] Введите ширину: ");
  23.                     b = double.Parse(Console.ReadLine());
  24.                     Console.Write("[*] Введите высоту: ");
  25.                     h = double.Parse(Console.ReadLine());
  26.  
  27.                     Prism prism = new Prism(a, b, h);
  28.  
  29.                     Console.WriteLine($"[*] Объем призмы: {prism.CalculateVolume()} ;" +
  30.                                       $" Площадь поверхности: {prism.CalculateArea()}");
  31.  
  32.                     break;
  33.                 case 2:
  34.                     Console.Write("[*] Введите радиус: ");
  35.                     r = double.Parse(Console.ReadLine());
  36.                     Console.Write("[*] Введите высоту: ");
  37.                     h = double.Parse(Console.ReadLine());
  38.  
  39.                     Cone cone = new Cone(r, h);
  40.  
  41.                     Console.WriteLine($"[*] Объем конуса: {cone.CalculateVolume()} ;" +
  42.                                       $" Площадь поверхности конуса: {cone.CalculateArea()}");
  43.  
  44.                     break;
  45.                 default:
  46.                     Console.WriteLine("Некорректный выбор.");
  47.                     break;
  48.             }
  49.         }
  50.  
  51.         public abstract class Figure
  52.         {
  53.             public abstract double CalculateArea();
  54.             public abstract double CalculateVolume();
  55.  
  56.         }
  57.  
  58.         public class Prism : Figure
  59.         {
  60.             private double a, b, h;
  61.  
  62.             public override double CalculateArea()
  63.             {
  64.                 return Math.Round(2 * (a * b + a * h + b * h),3);
  65.             }
  66.  
  67.             public override double CalculateVolume()
  68.             {
  69.                 return Math.Round(a * b * h, 3);
  70.             }
  71.  
  72.             public Prism(double a , double b, double h)
  73.             {
  74.                 this.a = a;
  75.                 this.b = b;
  76.                 this.h = h;
  77.             }
  78.  
  79.         }
  80.  
  81.         public class Cone : Figure
  82.         {
  83.             private double r, h;
  84.  
  85.             public override double CalculateArea()
  86.             {
  87.                 return Math.Round( Math.PI * r * Math.Sqrt(r * r + h * h) , 3);
  88.             }
  89.  
  90.             public override double CalculateVolume()
  91.             {
  92.                 return Math.Round(( ((double)1/3) * Math.PI * r * r * h),3);
  93.             }
  94.  
  95.             public Cone(double r, double h)
  96.             {
  97.                 this.r = r;
  98.                 this.h = h;
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment