от ammornil » 15 Апр 2022, 23:34
За страните на правоъгълния триъгълник в C# предлагам нещо такова.
Главен метод (основен клас)
- Код: Избери целия код
using System;
namespace PravoygTriygylnik
{
internal class Program
{
static void Main(string[] args)
{
int turn = 0;
while (turn < 5)
{
Console.WriteLine("\n\n Repetition {0} of 5", turn + 1);
PravoygTriyg pt = new PravoygTriyg();
pt.ProcessTriangleData();
turn++;
}
Console.WriteLine("Thank you! Good bye!");
Console.ReadKey(true);
}
}
}
Работен метод (клас за основния обект)
- Код: Избери целия код
using System;
using System.Collections.Generic;
using System.Text;
namespace PravoygTriygylnik
{
internal class PravoygTriyg
{
public double SideA { get; set; }
public double SideB { get; set; }
public char MissingSide { get; set; }
public PravoygTriyg()
{
}
public PravoygTriyg(double a, double b, char ch)
{
this.SideA = a;
this.SideB = b;
this.MissingSide = ch;
}
public void ProcessTriangleData()
{
double a, b;
char ch;
Console.Write("Enter length of one side of a rectangular triangle:\t\t");
try
{
a = double.Parse(Console.ReadLine());
}
catch
{
this.ErrorMsg1();
return;
}
Console.Write("Enter length of another side of a rectangular triangle:\t\t");
try
{
b = double.Parse(Console.ReadLine());
}
catch
{
this.ErrorMsg1();
return;
}
if (a < 0 || b < 0)
{
this.ErrorMsg1();
return;
}
Console.Write("Enter which side's size is missing (type 'h' for hypothenuse or 'k' for cathetus):\t");
ch = char.Parse(Console.ReadLine());
if (ch != 'h' && ch != 'k')
{
this.ErrorMsg2();
return;
}
this.SideA = a;
this.SideB = b;
this.MissingSide = ch;
this.CalcMissingSide();
}
public void CalcMissingSide()
{
if (this.SideA == this.SideB && this.MissingSide != 'h')
{
Console.WriteLine("Rectangular triangle cannot have cathetus equal to the hypotenuse.");
return;
}
if (this.MissingSide == 'h')
{
Console.WriteLine("Rectangular triangle with catheti {0, 8 : 0.0000} and {1, 8 : 0.0000} has a hipothenuse of {2, 8 : 0.0000}",
this.SideA, this.SideB, Math.Sqrt(Math.Pow(this.SideA,2)+Math.Pow(this.SideB,2)));
}
else
{
Console.WriteLine("Rectangular triangle with hypothenuse {0, 8 : 0.0000} and cathetus {1, 8 : 0.0000} has other cathetus of {2, 8 : 0.0000}",
(this.SideA>this.SideB) ? this.SideA : this.SideB,
(this.SideA > this.SideB) ? this.SideB : this.SideA,
Math.Sqrt(Math.Abs(Math.Pow(this.SideA, 2) - Math.Pow(this.SideB, 2))));
}
}
public void ErrorMsg1()
{
Console.WriteLine("The data entered was in wrong format. Please enter a positive numbers only. If your input is of decimal type use . as decimal sign");
}
public void ErrorMsg2()
{
Console.WriteLine("The data entered was in wrong format. The axceptable characters are 'h' for hypothenuse or 'k' for cathetus.");
}
}
}
[tex]\color{lightseagreen}\text{''Който никога не е правил грешка, никога не е опитвал нещо ново.''} \\
\hspace{21em}\text{(Алберт Айнщайн)}[/tex]