Skip to content

Commit

Permalink
Missing UNDO
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Feregrino authored and Antonio Feregrino committed Dec 14, 2016
1 parent 246709d commit 265bc2a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
47 changes: 32 additions & 15 deletions Calculator/CalculatorBrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal void SetOperand(string variableName)
}

lastVariable = variableName;
_internalProgram.Enqueue(variableName);
_internalProgram.Push(variableName);

}

Expand All @@ -45,13 +45,14 @@ internal void SetOperand(double operand)
_description = $"{_accumulator}";
}
lastVariable = null;
_internalProgram.Enqueue(operand);
_internalProgram.Push(operand);
}

enum Operation
{
Constant,
UnaryOperation,
UnaryOperation,
UnaryOperationPostFix,
BinaryOperation,
Equals,
Clear
Expand Down Expand Up @@ -91,8 +92,8 @@ void PerformPendingOperation()
{ "cos", Operation.UnaryOperation },
{ "sin", Operation.UnaryOperation },
{ "tan", Operation.UnaryOperation },
{ "^2", Operation.UnaryOperation },
{ "^3", Operation.UnaryOperation },
{ "^2", Operation.UnaryOperationPostFix },
{ "^3", Operation.UnaryOperationPostFix },
{ "×", Operation.BinaryOperation },
{ "÷", Operation.BinaryOperation },
{ "", Operation.BinaryOperation },
Expand All @@ -103,7 +104,7 @@ void PerformPendingOperation()
internal void PerformOperation(string symbol)
{
Operation op;
_internalProgram.Enqueue(symbol);
_internalProgram.Push(symbol);
if (operations.TryGetValue(symbol, out op))
{
AddOperationToDescription(symbol, op);
Expand All @@ -112,7 +113,8 @@ internal void PerformOperation(string symbol)
case Operation.Constant:
_accumulator = constants[symbol];
break;
case Operation.UnaryOperation:
case Operation.UnaryOperation:
case Operation.UnaryOperationPostFix:
PerformPendingOperation();
_accumulator = unaries[symbol](_accumulator);
break;
Expand Down Expand Up @@ -160,17 +162,22 @@ private void AddOperationToDescription(string symbol, Operation operation)
else
_description = $"{symbol}";
break;
case Operation.UnaryOperation:
case Operation.UnaryOperation:
case Operation.UnaryOperationPostFix:
if (lastVariable != null)
{
_description += " " + lastVariable;
lastVariable = null;
}
_description = $" {symbol} ({_description ?? _accumulator.ToString()})";
if(operation== Operation.UnaryOperation)
_description = $" {symbol}({_description ?? _accumulator.ToString()})";
else
_description = $"({_description ?? _accumulator.ToString()}){symbol}";

break;
case Operation.BinaryOperation:
if (IsPartialResult) break;
_description = $"({_description ?? _accumulator.ToString()}) {symbol} ";
_description = $"{_description ?? _accumulator.ToString()} {symbol} ";
break;
case Operation.Equals:
if (_previousOperation.HasValue)
Expand All @@ -194,16 +201,26 @@ private void AddOperationToDescription(string symbol, Operation operation)

}


internal void UndoLastOperation()
{
if (_internalProgram.Count != 0)
_internalProgram.Pop();//throw new NotImplementedException();
else
System.Diagnostics.Debug.WriteLine("No hay más operaciones");
}

public void ReRunProgram()
{

var newProgram = Program;
Program = newProgram;
}

private Queue<Object> _internalProgram = new Queue<Object>();
public Object Program { get { return new Queue<object>(_internalProgram); } set
private Stack<Object> _internalProgram = new Stack<Object>();
public Object Program { get { return new Stack<object>(_internalProgram); } set
{
Clear();
var program = value as Queue<Object>;
var program = value as Stack<Object>;
if (program != null)
{
foreach (var item in program)
Expand All @@ -216,7 +233,7 @@ public void ReRunProgram()
continue;
}

if(symbol != null)
if (symbol != null)
{
SetOperand(symbol);
continue;
Expand Down
2 changes: 1 addition & 1 deletion Calculator/CalculatorBrainDictionaries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public partial class CalculatorBrain
{ "", (a,b) => a - b },
{ "+", (a,b) => a + b },
};
}
}
}
12 changes: 10 additions & 2 deletions Calculator/ViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ partial void Undo(){
if (userIsInTheMiddleOfTyping)
{
var l = display.Text.Length;
if (l > 0)
if (l == 1)
{
brain.UndoLastOperation();
display.Text = "0";
userIsInTheMiddleOfTyping = false;
}
else if (l > 1)
{
display.Text = display.Text.Substring(0, l - 1);
}
brain.SetOperand(DisplayValue);
}
else
{
brain.UndoLastOperation();
brain.Program = brain.Program;
brain.ReRunProgram();
}
SetResult();
}

partial void RestoreMemory(UIKit.UIButton sender)
Expand Down

0 comments on commit 265bc2a

Please sign in to comment.