From 1fcbe3c01de7adba9dcb496ef53317e1ec097dd0 Mon Sep 17 00:00:00 2001 From: Antonio Feregrino Date: Sat, 10 Dec 2016 10:50:54 -0600 Subject: [PATCH] Calculator finished --- Calculator/CalculatorBrain.cs | 91 ++++- Calculator/Main.storyboard | 505 +++++++++++++++++--------- Calculator/ViewController.cs | 20 +- Calculator/ViewController.designer.cs | 18 +- 4 files changed, 446 insertions(+), 188 deletions(-) diff --git a/Calculator/CalculatorBrain.cs b/Calculator/CalculatorBrain.cs index f00f022..ce18b2b 100644 --- a/Calculator/CalculatorBrain.cs +++ b/Calculator/CalculatorBrain.cs @@ -1,5 +1,6 @@  using System; +using System.Linq; using System.Collections.Generic; namespace Calculator @@ -7,10 +8,42 @@ namespace Calculator public class CalculatorBrain { double _accumulator; + bool _hasSetDot; + double DecimalPositions = 10; + const double IntegerPositions = 10; + const int MaxMemory = 10; + Queue _operations = new Queue(MaxMemory); - internal void SetOperand(double operand) + + internal void AddOperand(double operand, bool userIsInTheMiddleOfTyping) { - _accumulator = operand; + if (userIsInTheMiddleOfTyping) + { + if (!_hasSetDot) + { + _accumulator = _accumulator * IntegerPositions + operand; + //_integerPositions *= 10; + } + else + { + _accumulator = _accumulator + operand / DecimalPositions; + DecimalPositions *= 10; + } + } + else + { + DecimalPositions = 10; + if (!_hasSetDot) + { + _accumulator = operand; + //_integerPositions *= 10; + } + else + { + _accumulator = operand / DecimalPositions; + DecimalPositions *= 10; + } + } } enum Operation @@ -18,11 +51,13 @@ enum Operation Constant, UnaryOperation, BinaryOperation, - Equals + Equals, + Dot, + Clear } struct PendingOperationInfo - { + { public PendingOperationInfo(Func binaryOperation, double firstOperand) { BinaryOperation = binaryOperation; @@ -49,13 +84,19 @@ void PerformPendingOperation() { { "π", Operation.Constant }, { "e", Operation.Constant }, + { "C", Operation.Clear }, { "√", Operation.UnaryOperation }, { "±", Operation.UnaryOperation }, + { "cos", Operation.UnaryOperation }, + { "sin", Operation.UnaryOperation }, + { "tan", Operation.UnaryOperation }, + { "x!", Operation.UnaryOperation }, { "×", Operation.BinaryOperation }, { "÷", Operation.BinaryOperation }, { "−", Operation.BinaryOperation }, { "+", Operation.BinaryOperation }, - { "=", Operation.Equals } + { "=", Operation.Equals }, + { ".", Operation.Dot } }; Dictionary constants = new Dictionary @@ -68,9 +109,13 @@ void PerformPendingOperation() { { "√", Math.Sqrt }, { "±", (d ) => -1 * d }, + { "cos", Math.Cos }, + { "sin", Math.Sin}, + { "tan", Math.Tan }, + { "x!", (d) => d } }; - Dictionary> binaries = new Dictionary> + Dictionary> binaries = new Dictionary> { { "×", (a,b) => a * b }, { "÷", (a,b) => a / b }, @@ -83,6 +128,7 @@ internal void PerformOperation(string symbol) Operation op; if (operations.TryGetValue(symbol, out op)) { + AddRecentOp(symbol); switch (op) { case Operation.Constant: @@ -92,18 +138,51 @@ internal void PerformOperation(string symbol) _accumulator = unaries[symbol](_accumulator); break; case Operation.BinaryOperation: + + _hasSetDot = false; PerformPendingOperation(); _pendingOperation = new PendingOperationInfo(binaries[symbol], _accumulator); + break; case Operation.Equals: PerformPendingOperation(); break; + case Operation.Dot: + if (!_hasSetDot) + { + _hasSetDot = true; + } + break; + case Operation.Clear: + Clear(); + break; default: break; } } } + void Clear() + { + _hasSetDot = false; + _accumulator = 0; + _pendingOperation = null; + DecimalPositions = 10; + } + + + internal String RecentOperations + { + get { return String.Join(" ", _operations); } + } + + private void AddRecentOp(string op) + { + if(_operations.Count == MaxMemory) + _operations.Dequeue(); + _operations.Enqueue(op); + } + internal Double Result { get { return _accumulator; } diff --git a/Calculator/Main.storyboard b/Calculator/Main.storyboard index 86fb3f8..17ad260 100644 --- a/Calculator/Main.storyboard +++ b/Calculator/Main.storyboard @@ -1,13 +1,16 @@ - + - + + + + - + @@ -15,177 +18,341 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Calculator/ViewController.cs b/Calculator/ViewController.cs index dfb665a..719a4fe 100644 --- a/Calculator/ViewController.cs +++ b/Calculator/ViewController.cs @@ -16,16 +16,9 @@ protected ViewController(IntPtr handle) : base(handle) partial void TouchDigit(UIButton sender) { var digit = sender.CurrentTitle; - if (userIsInTheMiddleOfTyping) - { - var textCurrentlyInDisplay = display.Text; - display.Text = textCurrentlyInDisplay + digit; - } - else - { - display.Text = digit; - } - brain.SetOperand(DisplayValue); + brain.AddOperand(Double.Parse(digit), userIsInTheMiddleOfTyping); + DisplayValue = brain.Result; + recentOperations.Text = brain.RecentOperations; userIsInTheMiddleOfTyping = true; } @@ -37,6 +30,12 @@ Double DisplayValue set { display.Text = value.ToString(); } } + partial void PressDot(UIButton sender) + { + var symbol = sender.CurrentTitle; + brain.PerformOperation(symbol); + DisplayValue = brain.Result; + } partial void PerformOperation(UIButton sender) { @@ -44,6 +43,7 @@ partial void PerformOperation(UIButton sender) var symbol = sender.CurrentTitle; brain.PerformOperation(symbol); DisplayValue = brain.Result; + recentOperations.Text = brain.RecentOperations; } } } diff --git a/Calculator/ViewController.designer.cs b/Calculator/ViewController.designer.cs index 27e6183..a81c0f7 100644 --- a/Calculator/ViewController.designer.cs +++ b/Calculator/ViewController.designer.cs @@ -15,15 +15,22 @@ namespace Calculator partial class ViewController { [Outlet] - [GeneratedCode ("iOS Designer", "1.0")] UIKit.UILabel display { get; set; } - [Action ("PerformOperation:")] + [Outlet] [GeneratedCode ("iOS Designer", "1.0")] + UIKit.UILabel recentOperations { get; set; } + + + [Action ("PerformOperation:")] partial void PerformOperation (UIKit.UIButton sender); + + [Action ("PressDot:")] + partial void PressDot (UIKit.UIButton sender); + + [Action ("TouchDigit:")] - [GeneratedCode ("iOS Designer", "1.0")] partial void TouchDigit (UIKit.UIButton sender); void ReleaseDesignerOutlets () @@ -32,6 +39,11 @@ void ReleaseDesignerOutlets () display.Dispose (); display = null; } + + if (recentOperations != null) { + recentOperations.Dispose (); + recentOperations = null; + } } } } \ No newline at end of file