Exemplo n.º 1
0
        /// <summary>
        /// Add a decimal to the current string
        /// </summary>
        public void Decimal()
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            if (lastAction == CalculatorAction.Error)
            {
                return;
            }

            if (lastAction == CalculatorAction.Equals || lastAction == CalculatorAction.Operator)
            {
                displayString.Clear();
                displayString.Append("0");
            }

            if (displayString.ToString().Contains("."))
            {
                return;
            }
            else
            {
                displayString.Append(".");
                UpdateDisplayText();
                lastAction = CalculatorAction.Input;
            }
        }
Exemplo n.º 2
0
        public Button(CalculatorAction calculatorAction)
        {
            switch (calculatorAction)
            {
            case CalculatorAction.Equals:
                DisplayText = "=";
                break;

            case CalculatorAction.Add:
                DisplayText = "+";
                break;

            case CalculatorAction.Subtract:
                DisplayText = "-";
                break;

            case CalculatorAction.Multiply:
                DisplayText = "x";
                break;

            case CalculatorAction.Divide:
                DisplayText = "÷";
                break;

            default:
                throw new ArgumentException($"Not a valid CalculatorAction: {calculatorAction.ToString()}");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Append a number string to the current string
        /// </summary>
        /// <param name="numberString"></param>
        public void AppendNumberString(string numberString)
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            if (lastAction == CalculatorAction.Error)
            {
                return;
            }

            if (lastAction != CalculatorAction.Input)
            {
                SetCurrentString(0);
            }

            bool hasDecimal = displayString.ToString().Contains(".");

            int maxLength = hasDecimal ? maximumDisplayCharacters + 1 : maximumDisplayCharacters;

            if (displayString.Length < maxLength)
            {
                if (ParseDisplayStringValue() == 0 && !displayString.ToString().Contains(".") || wipeOnNextInput)
                {
                    displayString.Clear();
                    wipeOnNextInput = false;
                }
                displayString.Append(numberString);
                UpdateDisplayText();
                lastAction = CalculatorAction.Input;
            }
        }
Exemplo n.º 4
0
 // Set error state
 private void Error()
 {
     displayString.Clear();
     displayString.Append("Err");
     UpdateDisplayText();
     lastAction      = CalculatorAction.Error;
     wipeOnNextInput = true;
 }
Exemplo n.º 5
0
        private void Calculate(CalculatorAction action)
        {
            if (!LastResult.HasValue)
            {
                return;
            }

            decimal result = Result;

            if (action == CalculatorAction.Percentage)
            {
                result /= 100m;
            }

            try
            {
                switch (LastAction)
                {
                case CalculatorAction.Add:
                    Result = LastResult.Value + result;
                    break;

                case CalculatorAction.Divide:
                    Result = LastResult.Value / result;
                    break;

                case CalculatorAction.Multiply:
                    Result = LastResult.Value * result;
                    break;

                case CalculatorAction.OneDividedBy:
                    Result = 1m / LastResult.Value;
                    break;

                case CalculatorAction.SquareRoot:
                    Result = (decimal)Math.Sqrt((double)LastResult.Value);
                    break;

                case CalculatorAction.Subtract:
                    Result = LastResult.Value - result;
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
            catch
            {
                HaveError = true;
                ClearAll();
            }

            LastAction = CalculatorAction.None;
            LastResult = null;

            _textResult = "0";
        }
Exemplo n.º 6
0
        /// <summary>
        /// Clear the current display
        /// </summary>
        public void Clear()
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            SetCurrentString(0);
            rightValue = 0f;
            lastAction = CalculatorAction.Input;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Process operator input
        /// </summary>
        private void ProcessOperator()
        {
            if (lastAction == CalculatorAction.Operator || lastAction == CalculatorAction.Error)
            {
                return;
            }
            else if (leftValueExists)
            {
                Calculate();
            }

            leftValue       = ParseDisplayStringValue();
            leftValueExists = true;
            lastAction      = CalculatorAction.Operator;
        }
Exemplo n.º 8
0
        private string GetActionChar(CalculatorAction calculatorAction)
        {
            switch (calculatorAction)
            {
                case CalculatorAction.Add: return "+";
                case CalculatorAction.Divide: return "/";
                case CalculatorAction.Multiply: return "*";
                case CalculatorAction.Negative: return "±";
                case CalculatorAction.OneDividedBy: return "1/x";
                case CalculatorAction.Percentage: return "%";
                case CalculatorAction.SquareRoot: return "√";
                case CalculatorAction.Subtract: return "-";

                default:
                    throw new NotSupportedException();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Square root button function
        /// </summary>
        public void SquareRoot()
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            if (lastAction == CalculatorAction.Error)
            {
                return;
            }

            lastAction = CalculatorAction.Input;
            SetCurrentString(System.Math.Sqrt(ParseDisplayStringValue()));
            wipeOnNextInput = true;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Memory recall button function
        /// </summary>
        public void MemoryRecall()
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            if (lastAction == CalculatorAction.Error)
            {
                return;
            }
            if (memoryExists)
            {
                SetCurrentString(memoryValue);
                lastAction      = CalculatorAction.Input;
                wipeOnNextInput = true;
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Clear the current display and stored values
        /// </summary>
        public void AllClear()
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            leftValue       = 0f;
            leftValueExists = false;
            rightValue      = 0f;

            memoryValue  = 0f;
            memoryExists = false;

            SetCurrentString(0);
            lastAction     = CalculatorAction.None;
            activeOperator = CalculatorOperator.None;
        }
Exemplo n.º 12
0
        private void AddAction(CalculatorAction action)
        {
            if (LastAction != CalculatorAction.None)
            {
                Calculate(CalculatorAction.Perform);
            }

            LastAction = action;
            LastResult = Result;

            Result = 0m;

            switch (action)
            {
            case CalculatorAction.OneDividedBy:
            case CalculatorAction.SquareRoot:
                Calculate(CalculatorAction.Perform);
                break;
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Flip the sign button function
        /// </summary>
        public void FlipSign()
        {
            if (!CheckIfCanPressButton())
            {
                return;
            }

            AudioManager.Instance.PlayClip(buttonClick, transform.position, .3f, 1f);

            if (lastAction == CalculatorAction.Error)
            {
                return;
            }

            double v = ParseDisplayStringValue();

            if (v == 0f)
            {
                // Do nothing
            }
            else if (v < 0f)
            {
                displayString.Remove(0, 1);
                UpdateDisplayText();
                lastAction = CalculatorAction.Input;
            }
            else
            {
                if (displayString.Length > maximumDisplayCharacters - 1)
                {
                    if (!displayString.ToString().Contains("."))
                    {
                        Error();
                        return;
                    }
                }
                displayString.Insert(0, "-");
                UpdateDisplayText();
                lastAction = CalculatorAction.Input;
            }
        }
Exemplo n.º 14
0
        private string GetActionChar(CalculatorAction calculatorAction)
        {
            switch (calculatorAction)
            {
            case CalculatorAction.Add: return("+");

            case CalculatorAction.Divide: return("/");

            case CalculatorAction.Multiply: return("*");

            case CalculatorAction.Negative: return("±");

            case CalculatorAction.OneDividedBy: return("1/x");

            case CalculatorAction.Percentage: return("%");

            case CalculatorAction.SquareRoot: return("√");

            case CalculatorAction.Subtract: return("-");

            default:
                throw new NotSupportedException();
            }
        }
Exemplo n.º 15
0
 public void OperationAction(CalculatorAction action)
 {
     //This function would need to be fleshed out a bit to get postfix calculation working
 }
Exemplo n.º 16
0
 public void Perform(CalculatorAction action)
 {
     _calculator.PerformAction(action);
 }
Exemplo n.º 17
0
 internal Calculator()
 {
     _lastAction = CalculatorAction.None;
     _result     = 0;
 }
Exemplo n.º 18
0
        internal void PerformAction(CalculatorAction action)
        {
            HaveError = false;

            switch (action)
            {
                case CalculatorAction.Add:
                case CalculatorAction.Divide:
                case CalculatorAction.Multiply:
                case CalculatorAction.OneDividedBy:
                case CalculatorAction.SquareRoot:
                case CalculatorAction.Subtract:
                    AddAction(action);
                    break;

                case CalculatorAction.Negative: PerformNegative(); break;

                case CalculatorAction.Percentage:
                case CalculatorAction.Perform:
                    Calculate(action);
                    break;

                case CalculatorAction.Add0: AddCharacter("0"); break;
                case CalculatorAction.Add1: AddCharacter("1"); break;
                case CalculatorAction.Add2: AddCharacter("2"); break;
                case CalculatorAction.Add3: AddCharacter("3"); break;
                case CalculatorAction.Add4: AddCharacter("4"); break;
                case CalculatorAction.Add5: AddCharacter("5"); break;
                case CalculatorAction.Add6: AddCharacter("6"); break;
                case CalculatorAction.Add7: AddCharacter("7"); break;
                case CalculatorAction.Add8: AddCharacter("8"); break;
                case CalculatorAction.Add9: AddCharacter("9"); break;
                case CalculatorAction.AddDecimal: AddCharacter("."); break;
                case CalculatorAction.Backspace: RemoveCharacter(); break;

                case CalculatorAction.MemoryAdd: MemoryAdd(); break;
                case CalculatorAction.MemoryClear: MemoryClear(); break;
                case CalculatorAction.MemoryRecall: MemoryRecall(); break;
                case CalculatorAction.MemorySet: MemorySet(); break;
                case CalculatorAction.MemorySubtract: MemorySubtract(); break;

                case CalculatorAction.Clear: ClearAll(); break;
                case CalculatorAction.ClearEntry: ClearEntry(); break;

                default:
                    throw new NotSupportedException();
            }
        }
Exemplo n.º 19
0
        internal void PerformAction(CalculatorAction action)
        {
            HaveError = false;

            switch (action)
            {
            case CalculatorAction.Add:
            case CalculatorAction.Divide:
            case CalculatorAction.Multiply:
            case CalculatorAction.OneDividedBy:
            case CalculatorAction.SquareRoot:
            case CalculatorAction.Subtract:
                AddAction(action);
                break;

            case CalculatorAction.Negative: PerformNegative(); break;

            case CalculatorAction.Percentage:
            case CalculatorAction.Perform:
                Calculate(action);
                break;

            case CalculatorAction.Add0: AddCharacter("0"); break;

            case CalculatorAction.Add1: AddCharacter("1"); break;

            case CalculatorAction.Add2: AddCharacter("2"); break;

            case CalculatorAction.Add3: AddCharacter("3"); break;

            case CalculatorAction.Add4: AddCharacter("4"); break;

            case CalculatorAction.Add5: AddCharacter("5"); break;

            case CalculatorAction.Add6: AddCharacter("6"); break;

            case CalculatorAction.Add7: AddCharacter("7"); break;

            case CalculatorAction.Add8: AddCharacter("8"); break;

            case CalculatorAction.Add9: AddCharacter("9"); break;

            case CalculatorAction.AddDecimal: AddCharacter("."); break;

            case CalculatorAction.Backspace: RemoveCharacter(); break;

            case CalculatorAction.MemoryAdd: MemoryAdd(); break;

            case CalculatorAction.MemoryClear: MemoryClear(); break;

            case CalculatorAction.MemoryRecall: MemoryRecall(); break;

            case CalculatorAction.MemorySet: MemorySet(); break;

            case CalculatorAction.MemorySubtract: MemorySubtract(); break;

            case CalculatorAction.Clear: ClearAll(); break;

            case CalculatorAction.ClearEntry: ClearEntry(); break;

            default:
                throw new NotSupportedException();
            }
        }
Exemplo n.º 20
0
 public void Perform(CalculatorAction action)
 {
     _calculator.PerformAction(action);
 }
Exemplo n.º 21
0
        private void AddAction(CalculatorAction action)
        {
            if (LastAction != CalculatorAction.None)
                Calculate(CalculatorAction.Perform);

            LastAction = action;
            LastResult = Result;

            Result = 0m;

            switch (action)
            {
                case CalculatorAction.OneDividedBy:
                case CalculatorAction.SquareRoot:
                    Calculate(CalculatorAction.Perform);
                    break;
            }
        }
Exemplo n.º 22
0
        // Calculate the expression
        private void Calculate()
        {
            if (activeOperator == CalculatorOperator.None)
            {
                return;
            }

            switch (lastAction)
            {
            case CalculatorAction.Operator:
                rightValue = leftValue;
                break;

            case CalculatorAction.Input:
                rightValue = ParseDisplayStringValue();
                break;

            case CalculatorAction.Equals:
                // rightValue stays the same
                break;

            case CalculatorAction.None:
                return;
            }

            // Set lastAction early in case Error happens in SetCurrentString()
            // so that Error state can propagate
            lastAction = CalculatorAction.Equals;

            switch (activeOperator)
            {
            case CalculatorOperator.Add:
                leftValue += rightValue;
                SetCurrentString(leftValue);
                break;

            case CalculatorOperator.Subtract:
                leftValue -= rightValue;
                SetCurrentString(leftValue);
                break;

            case CalculatorOperator.Multiply:
                leftValue *= rightValue;
                SetCurrentString(leftValue);
                break;

            case CalculatorOperator.Divide:

                if (rightValue != 0f)
                {
                    leftValue /= rightValue;
                }
                else
                {
                    Error();
                    return;
                }
                SetCurrentString(leftValue);
                break;
            }
        }
Exemplo n.º 23
0
        private void Calculate(CalculatorAction action)
        {
            if (!LastResult.HasValue)
                return;

            decimal result = Result;

            if (action == CalculatorAction.Percentage)
                result /= 100m;

            try
            {
                switch (LastAction)
                {
                    case CalculatorAction.Add:
                        Result = LastResult.Value + result;
                        break;

                    case CalculatorAction.Divide:
                        Result = LastResult.Value / result;
                        break;

                    case CalculatorAction.Multiply:
                        Result = LastResult.Value * result;
                        break;

                    case CalculatorAction.OneDividedBy:
                        Result = 1m / LastResult.Value;
                        break;

                    case CalculatorAction.SquareRoot:
                        Result = (decimal)Math.Sqrt((double)LastResult.Value);
                        break;

                    case CalculatorAction.Subtract:
                        Result = LastResult.Value - result;
                        break;

                    default:
                        throw new NotSupportedException();
                }
            }
            catch
            {
                HaveError = true;
                ClearAll();
            }

            LastAction = CalculatorAction.None;
            LastResult = null;

            _textResult = "0";
        }
Exemplo n.º 24
0
 internal Calculator()
 {
     _lastAction = CalculatorAction.None;
     _result = 0;
 }