calculator/calculatorEshkeree/MainWindow.axaml.cs

174 lines
4.6 KiB
C#
Raw Normal View History

2025-02-03 13:57:20 +00:00
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
namespace calculatorEshkeree;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_display = this.FindControl<TextBox>("Display");
}
private TextBox _display;
private string _currentInput = "";
private string _firstOperand = "";
private string _operation = "";
private int _equalPressCount = 0;
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void OnNumberButtonClick(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
if (button.Content.ToString() == "7")
return;
if (_currentInput.Length >= 3)
return;
_currentInput += button.Content.ToString();
UpdateDisplay(_currentInput);
}
}
private void OnOperationButtonClick(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
string operation = button.Content.ToString();
if (!string.IsNullOrEmpty(_currentInput))
{
double value = double.Parse(_currentInput);
if (operation == "tg")
{
_currentInput = "";
_firstOperand = "";
_operation = "";
UpdateDisplay("баг");
return;
}
if (operation == "sin" || operation == "cos" || operation == "ctg")
{
double result = operation switch
{
"sin" => Math.Cos(value * Math.PI / 180),
"cos" => Math.Sin(value * Math.PI / 180),
"ctg" => 1 / Math.Tan(value * Math.PI / 180),
_ => 0
};
UpdateDisplay(result.ToString());
_currentInput = result.ToString();
}
else
{
if (operation == "+" && _operation != "+")
{
_operation = "+";
return;
}
_firstOperand = _currentInput;
_operation = operation;
_currentInput = "";
}
}
}
}
private void OnEqualButtonClick(object sender, RoutedEventArgs e)
{
_equalPressCount++;
if (_equalPressCount < 3)
{
return;
}
_equalPressCount = 0;
if (!string.IsNullOrEmpty(_firstOperand) && !string.IsNullOrEmpty(_currentInput))
{
double first = double.Parse(_firstOperand);
double second = double.Parse(_currentInput);
if (first == 0 && second == 0)
{
UpdateDisplay("zer00");
return;
}
double result = _operation switch
{
"+" => first + second,
"-" => first - second,
"*" => first * second,
"/" => first / second,
_ => 0,
};
if (result.ToString().Length > 5)
{
result = Math.Round(result, 2);
UpdateDisplay($"{result}...");
}
else
{
UpdateDisplay(result.ToString());
}
_currentInput = result.ToString();
_firstOperand = "";
_operation = "";
}
}
private void OnClearButtonClick(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(_currentInput))
{
if (_currentInput.Length > 1)
{
int halfLength = _currentInput.Length / 2;
_currentInput = _currentInput.Substring(0, halfLength);
}
else
{
_currentInput = "";
}
}
else if (!string.IsNullOrEmpty(_firstOperand))
{
if (_firstOperand.Length > 1)
{
int halfLength = _firstOperand.Length / 2;
_firstOperand = _firstOperand.Substring(0, halfLength);
}
else
{
_firstOperand = "";
}
}
else
{
_operation = "";
}
UpdateDisplay(string.IsNullOrEmpty(_currentInput) ? "0" : _currentInput);
}
private void UpdateDisplay(string text)
{
_display.Text = text;
}
}