92 lines
4.0 KiB
C#
92 lines
4.0 KiB
C#
using System;
|
||
using Avalonia;
|
||
using Avalonia.Controls;
|
||
using Avalonia.Markup.Xaml;
|
||
|
||
namespace BuggyCalculator
|
||
{
|
||
public partial class MainWindow : Window
|
||
{
|
||
private TextBox _input1;
|
||
private TextBox _input2;
|
||
private ComboBox _operation;
|
||
private TextBlock _result;
|
||
private Button _calculateButton;
|
||
|
||
public MainWindow()
|
||
{
|
||
InitializeComponent();
|
||
|
||
_input1 = this.FindControl<TextBox>("Input1") ?? throw new InvalidOperationException("Input1 not found");
|
||
_input2 = this.FindControl<TextBox>("Input2") ?? throw new InvalidOperationException("Input2 not found");
|
||
_operation = this.FindControl<ComboBox>("Operation") ?? throw new InvalidOperationException("Operation not found");
|
||
_result = this.FindControl<TextBlock>("Result") ?? throw new InvalidOperationException("Result not found");
|
||
_calculateButton = this.FindControl<Button>("CalculateButton") ?? throw new InvalidOperationException("CalculateButton not found");
|
||
|
||
_calculateButton.Click += Calculate;
|
||
}
|
||
|
||
private void InitializeComponent()
|
||
{
|
||
AvaloniaXamlLoader.Load(this);
|
||
}
|
||
|
||
private void Calculate(object? sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(_input1.Text))
|
||
{
|
||
_result.Text = "Ошибка: Первое поле пустое.";
|
||
return;
|
||
}
|
||
|
||
|
||
if (!double.TryParse(_input1.Text, out double num1))
|
||
{
|
||
throw new FormatException("Ошибка: Некорректный ввод");
|
||
}
|
||
double num2 = string.IsNullOrWhiteSpace(_input2.Text) ? 0 : double.Parse(_input2.Text);
|
||
|
||
if (_operation.SelectedItem is ComboBoxItem selectedItem)
|
||
{
|
||
string operation = selectedItem.Content.ToString();
|
||
|
||
double result = operation switch
|
||
{
|
||
"Сложение" => num1 + num2,
|
||
"Вычитание" => num1 - num2,
|
||
"Умножение" => num1 * num2,
|
||
"Деление" => num2 != 0 ? num1 / num2 : throw new DivideByZeroException(),
|
||
"Возведение в степень" => num2 < 0 ? throw new InvalidOperationException("Возведение в отрицательную степень запрещено") : Math.Pow(num1, num2),
|
||
"Квадратный корень" => num1 >= 0 ? Math.Sqrt(num1) : throw new InvalidOperationException("Квадратный корень из отрицательного числа невозможен"),
|
||
"Логарифм" => num1 > 0 && num2 > 0 && num2 != 1 ? Math.Log(num1, num2) : throw new InvalidOperationException("Основание логарифма должно быть > 0 и != 1"),
|
||
"Синус" => Math.Sin(num1),
|
||
"Косинус" => Math.Cos(num1),
|
||
"Тангенс" => Math.Tan(num1),
|
||
_ => throw new InvalidOperationException("Неизвестная операция")
|
||
};
|
||
|
||
_result.Text = $"Результат: {result}";
|
||
}
|
||
else
|
||
{
|
||
_result.Text = "Ошибка: Не выбрана операция.";
|
||
}
|
||
}
|
||
catch (FormatException)
|
||
{
|
||
_result.Text = "Ошибка: Неверный формат числа.";
|
||
}
|
||
catch (DivideByZeroException)
|
||
{
|
||
_result.Text = "Ошибка: Деление на ноль.";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Environment.FailFast(ex.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|