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