Logic
This commit is contained in:
parent
e0adb55d43
commit
6fbbe8c611
@ -35,8 +35,8 @@
|
||||
<Button Grid.Row="3" Grid.Column="3" Name="SubstractButton" Click="OperationButton_OnClick">-</Button>
|
||||
<Button Grid.Row="4" Grid.Column="3" Name="AddButton" Click="OperationButton_OnClick">+</Button>
|
||||
|
||||
<Button Grid.Row="0" Grid.Column="3" Name="EqualButton" Click="OperationButton_OnClick">=</Button>
|
||||
<Button Grid.Row="0" Grid.Column="2" Name="ClearButton" Click="ClearButton_OnClick">CLR</Button>
|
||||
<Button Grid.Row="0" Grid.Column="3" Name="EqualButton" Click="OperationButton_OnClick">=</Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
@ -10,45 +10,42 @@ public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
_currentOperation = _initialOperation;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private double _rezult;
|
||||
private StringBuilder currentNumberB = new();
|
||||
private double _prevNumber;
|
||||
private StringBuilder _currentNumberB = new();
|
||||
|
||||
private double CompleteCurrentNumber()
|
||||
private double ParseCurrentNumber() => double.Parse(_currentNumberB.ToString(), CultureInfo.InvariantCulture);
|
||||
|
||||
readonly Func<double, double, double> _initialOperation = (_, firstN) => firstN;
|
||||
private Func<double, double, double> _currentOperation;
|
||||
|
||||
private void NewOperation(Func<double, double, double> operation, string operationText)
|
||||
{
|
||||
var n = double.Parse(currentNumberB.ToString(), CultureInfo.InvariantCulture);
|
||||
currentNumberB.Clear();
|
||||
return n;
|
||||
// operation replacement
|
||||
if (_currentNumberB.Length == 0)
|
||||
Input.Redo();
|
||||
else
|
||||
{
|
||||
_prevNumber = _currentOperation( _prevNumber, ParseCurrentNumber());
|
||||
Output.Text = _prevNumber.ToString(CultureInfo.InvariantCulture);
|
||||
_currentNumberB.Clear();
|
||||
}
|
||||
|
||||
private void UpdateOutput()
|
||||
{
|
||||
Output.Text = "= " + _rezult;
|
||||
}
|
||||
|
||||
private Func<double, double, double> prevOperation = (_, firstN) => firstN;
|
||||
|
||||
private void TryUpdateRezult(Func<double, double, double> operation, string text)
|
||||
{
|
||||
if (currentNumberB.Length > 0)
|
||||
{
|
||||
_rezult = prevOperation(_rezult, CompleteCurrentNumber());
|
||||
prevOperation = operation;
|
||||
Input.Text += " " + text + " ";
|
||||
}
|
||||
|
||||
UpdateOutput();
|
||||
Input.Text = _prevNumber.ToString(CultureInfo.InvariantCulture) + " " + operationText + " ";
|
||||
_currentOperation = operation;
|
||||
}
|
||||
|
||||
private void NumberButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is not Button button)
|
||||
throw new Exception();
|
||||
string text = button.Content?.ToString()!;
|
||||
|
||||
string text = button.Content!.ToString()!;
|
||||
Input.Text += text;
|
||||
currentNumberB.Append(text);
|
||||
_currentNumberB.Append(text);
|
||||
Output.Text = "= " + _currentOperation(_prevNumber, ParseCurrentNumber()).ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private void OperationButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
@ -59,22 +56,22 @@ public partial class MainWindow : Window
|
||||
switch (text)
|
||||
{
|
||||
case "+":
|
||||
TryUpdateRezult((a, b) => a + b, text);
|
||||
NewOperation((a, b) => a + b, text);
|
||||
break;
|
||||
case "-":
|
||||
TryUpdateRezult((a, b) => a - b, text);
|
||||
NewOperation((a, b) => a - b, text);
|
||||
break;
|
||||
case "*":
|
||||
TryUpdateRezult((a, b) => a * b, text);
|
||||
NewOperation((a, b) => a * b, text);
|
||||
break;
|
||||
case "/":
|
||||
TryUpdateRezult((a, b) => a / b, text);
|
||||
NewOperation((a, b) => a / b, text);
|
||||
break;
|
||||
case "^":
|
||||
TryUpdateRezult(Math.Pow, text);
|
||||
NewOperation(Math.Pow, text);
|
||||
break;
|
||||
case "=":
|
||||
TryUpdateRezult((a, _) => a, "");
|
||||
NewOperation((_, newNumber) => newNumber, "");
|
||||
break;
|
||||
default:
|
||||
throw new Exception("incorrect button text: " + text);
|
||||
@ -83,8 +80,10 @@ public partial class MainWindow : Window
|
||||
|
||||
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
_rezult = 0;
|
||||
UpdateOutput();
|
||||
_prevNumber = 0;
|
||||
_currentOperation = _initialOperation;
|
||||
_currentNumberB.Clear();
|
||||
Input.Text = "";
|
||||
Output.Text = "= 0";
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<LangVersion>12</LangVersion>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<LangVersion>11</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user