using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace CalculatorApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// =========================
// Обработка кнопок
// =========================
private void Digit_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
InputBox.Text += button.Content.ToString();
}
private void Operator_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
InputBox.Text += button.Content.ToString();
}
private void Dot_Click(object sender, RoutedEventArgs e)
{
InputBox.Text += ".";
}
private void Clear_Click(object sender, RoutedEventArgs e)
{
InputBox.Text = "";
ResultBox.Text = "";
}
// =========================
// Пересчёт результата
// =========================
private void InputBox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
string input = InputBox.Text;
if (string.IsNullOrWhiteSpace(input))
{
ResultBox.Text = "";
return;
}
// Вызов твоего класса
var result = Calculator.Calculate(input);
ResultBox.Text = result.ToString();
}
catch
{
ResultBox.Text = "Ошибка";
}
}
// =========================
// Фильтрация ввода
// =========================
private void InputBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// Разрешённые символы
string allowedPattern = @"^[0-9+\-*/^.]$";
// Проверяем вводимый символ
if (!Regex.IsMatch(e.Text, allowedPattern))
{
// ОТКАЗ В ВВОДЕ
e.Handled = true;
return;
}
// ДОПОЛНИТЕЛЬНАЯ ЛОГИКА (пример):
// Запрет двух точек подряд
if (e.Text == "." && InputBox.Text.EndsWith("."))
{
e.Handled = true;
return;
}
// Запрет двух операторов подряд
if ("+-*/^".Contains(e.Text) && InputBox.Text.Length > 0)
{
char last = InputBox.Text[^1];
if ("+-*/^".Contains(last))
{
e.Handled = true;
return;
}
}
// Если дошли сюда — ВВОД РАЗРЕШЁН
e.Handled = false;
}
}
}