Добавлены операци:

SqrtY,
Factorial,
CubeRoot,
Square,
Cube,
PowY,
Log,
Ln,
Exp,
Inv,
Pi,
Sinh,
Sin,
Cosh,
Cos,
Tanh,
Tan
This commit is contained in:
Мария 2024-04-04 22:49:32 +03:00
parent dcfd7c6e70
commit d3836d6cfc
14 changed files with 1072 additions and 798 deletions

View file

@ -1,4 +1,6 @@
using calculator.Model;
using calculator.Commands;
using calculator.Common;
using calculator.Model;
using calculator.Services;
using calculator.View;
@ -10,6 +12,7 @@ public class CaculatorController
private readonly IInputService _inputService;
private readonly ICaculatorView _view;
private readonly CaculatorData _data;
private ICommand? _currentCommand;
public CaculatorController(ICaculator caculator, IInputService inputService, ICaculatorView view,
CaculatorData data)
@ -21,30 +24,22 @@ public class CaculatorController
_view.OperandPressed += OnOperandPressed;
_view.OperatorPressed += OnOperatorPressed;
_view.ClearPressed += OnFullClearPressed;
_view.ClearPressed += OnClearPressed;
_view.CalculatePressed += OnCalculatePressed;
_view.SingleOperatorPressed += OnSingleOperationPressed;
OnFullClearPressed();
OnClearPressed(true);
}
private void OnSingleOperationPressed(string obj)
private void OnSingleOperationPressed(Operation operation)
{
_data.Operation = null;
if (_data.Operands.Count == 2 && _data.Caculated == false)
{
OnCalculatePressed();
}
_data.Caculated = true;
_data.SingleOperation = obj;
var operand = _data.Operands.Pop();
_data.Operands.Push(_caculator.SingleOperation(operand, obj));
_view.UpdateView(_data.Operands.Peek());
OnOperatorPressed(operation);
_currentCommand = new CalculateCommand(_caculator, _data);
OnCalculatePressed();
}
private void OnFullClearPressed()
private void OnClearPressed(bool full)
{
Clear();
Clear(full: full);
}
private void OnCalculatePressed()
@ -52,83 +47,70 @@ public class CaculatorController
if (_data.Operation is null)
return;
if (_data.SingleOperation is not null)
{
var singleOperand = _data.Operands.Pop();
_data.Operands.Push(_caculator.SingleOperation(singleOperand, _data.SingleOperation));
_view.UpdateView(_data.Operands.Peek());
if (_data.Value is null)
return;
}
if (_data.Operands.Count < 2)
if (_data is not { Caculated: true, Input: null } || _currentCommand == null)
{
var (lastOperand, lastOperation) = (_data.LastOperand, _data.Operation);
if (lastOperand is not null && lastOperation is not null)
{
Calculate(_data.Operands.Pop(), lastOperand, lastOperation);
}
return;
_currentCommand = new CalculateCommand(_caculator, _data);
}
var operand = _data.Operands.Pop();
var operand2 = _data.Operands.Pop();
Calculate(operand2, operand, _data.Operation);
}
private void Calculate(string operand1, string operand2, string operation)
{
_currentCommand?.Execute();
_data.Caculated = true;
_data.LastOperand = operand2;
_data.Operands.Push(_caculator.Calculate(operand1, operand2, operation));
_view.UpdateView(_data.Operands.Peek());
UpdateView(_data.Value.Value);
}
private void Clear(string value = "0", bool full = true)
{
if (full)
{
_data.Operands.Clear();
_data.Value = null;
_data.Operation = null;
_data.SingleOperation = null;
}
else
{
_data.Operands.TryPop(out _);
}
_data.Caculated = false;
_data.LastOperand = null;
_data.Operands.Push(value);
_view.UpdateView(_data.Operands.Peek());
_data.Input = null;
_view.UpdateView(value);
}
private void OnOperatorPressed(string operation)
private void OnOperatorPressed(Operation operation)
{
_data.SingleOperation = null;
if (_data.LastOperand is not null && _data.Caculated == false)
if (_data.Input == null && _data.Value == null)
return;
if (_data is { Caculated: false, Input: not null, Value: not null })
{
OnCalculatePressed();
}
_data.Operation = operation;
if (_data.Input != null)
{
_data.Value = double.Parse(_data.Input);
}
_data.Input = null;
}
private void OnOperandPressed(char obj)
{
if (_data is { Operands.Count: 1, Operation: not null })
if (_data.Caculated)
{
_data.Operands.Push("0");
}
else if (_data is { Caculated: true })
{
Clear();
Clear(full: false);
}
var operand = _data.Operands.Pop();
_data.Input = _inputService.TryInput(_data.Input + obj);
UpdateView(_data.Input);
}
private void UpdateView(double input)
{
_view.UpdateView(input.ToString("0.#####"));
}
_data.Operands.Push(_inputService.TryInput(operand + obj));
_view.UpdateView(_data.Operands.Peek());
private void UpdateView(string input)
{
_view.UpdateView(input);
}
}