Exemplo n.º 1
0
        public void Calculate(double[] argument, double[] result)
        {
            Sorter.Calculate(argument);
            var testResult = argument;

            Assert.AreEqual(result, testResult);
        }
Exemplo n.º 2
0
        internal double Compute(string expression)
        {
            _expression = expression.Replace('.', ',').Replace(" ", string.Empty);
            _position   = 0;
            IOperation operation = AdditionOrSubtraction();

            return(operation.Calculate());
        }
Exemplo n.º 3
0
 public double Calculate()
 {
     switch (_code)
     {
     case "-":
         return(-_operation.Calculate());
     }
     return(0D);
 }
Exemplo n.º 4
0
        public double Calculate()
        {
            switch (_code)
            {
            case "+":
                return(_leftOperation.Calculate() + _rightOperation.Calculate());

            case "-":
                return(_leftOperation.Calculate() - _rightOperation.Calculate());

            case "*":
                return(_leftOperation.Calculate() * _rightOperation.Calculate());

            case "/":
                return(_leftOperation.Calculate() / _rightOperation.Calculate());
            }
            return(0D);
        }
        double IExpression.GetValue()
        {
            var items  = _links.Select(it => it.GetValue()).ToList();
            var result = items.First();

            foreach (var it in items.GetRange(1, items.Count - 1))
            {
                result = _operation.Calculate(result, it);
            }

            return(result);
        }
Exemplo n.º 6
0
 public void Calculate()
 {
     try
     {
         IOperation operation = this.GetterOperation.GetOperation();
         this.Result = operation.Calculate();
         this.ConsoleWrapper.WriteLine(
             string.Format("->: {0} = {1}", operation.GetTextOperation(), this.Result));
     }
     catch (InvalidOperationException ex)
     {
         this.ConsoleWrapper.WriteLine(ex.Message);
     }
     catch (FileNotFoundException ex)
     {
         this.ConsoleWrapper.WriteLine(ex.Message + ex.FileName);
     }
     catch (ArgumentNullException ex)
     {
         this.ConsoleWrapper.WriteLine(ex.Message);
     }
     catch (Exception ex)
     {
         this.ConsoleWrapper.WriteLine(ex.Message);
     }
     finally
     {
         if (IsInputFromConsole)
         {
             CheckIfNewOperation();
         }
         else
         {
             this.ConsoleWrapper.WriteLine(ConsoleMessages.PressEnterCloseApplication);
             this.ConsoleWrapper.ReadLine();
         }
     }
 }
Exemplo n.º 7
0
 public void Operate(IOperation operation)
 {
     FromStack(operation.Calculate(ToStack()));
     _enterBeforeNextDigit = true;
 }
        /// <summary>
        /// Processes an arithmetic equation.
        /// </summary>
        /// <param name="equation">The equation which should to be processed.</param>
        /// <returns>The calculated value.</returns>
        public double OnOperation(string equation)
        {
            //Parse the number and the symbol from the Equation
            double parsedNumber;
            char   symbol;
            string number = equation.Substring(0, equation.Length - 1);

            if (!double.TryParse(number, out parsedNumber))
            {
                parsedNumber = 0.00;
            }
            symbol = equation[equation.Length - 1];

            //Declare a variable for the result
            double?result;

            //If the first parameter is null assign the current value and operation to the internal properties. Return value is the first parameter.
            if (param1.Equals(null))
            {
                param1           = parsedNumber;
                currentOperation = oFactory.GetOperation(symbol);
                return(param1 ?? 0.00);
            }
            else if //if the last two operations are "="
            ((oFactory.GetOperation(symbol).GetType() == currentOperation.GetType()) &&
             currentOperation.GetType() == typeof(Equate))
            {
                param2 = parsedNumber;
                result = currentOperation.Calculate(param1, param2);     //calculate the result

                //Save operation
                operationHistory.Push(new ResultHistory(
                                          currentOperation,
                                          param1,
                                          param2));

                //Nullify all parameters
                param1           = null;
                param2           = null;
                currentOperation = null;
            }
            else     //All other cases:
            {
                //Pass the value to param2
                param2 = parsedNumber;

                //Calculate the result
                result = currentOperation.Calculate(param1, param2);

                //Save the operation
                operationHistory.Push(new ResultHistory(
                                          currentOperation,
                                          param1,
                                          param2
                                          ));

                //Reassign values before ending
                param1           = result;
                lastOperation    = currentOperation;
                currentOperation = oFactory.GetOperation(symbol);
                param2           = null;
            }

            //If it's not null - return result
            //Else return 0.00
            return(result ?? 0.00);
        }
Exemplo n.º 9
0
 public void Operate(IOperation operation)
 {
     FromStack(operation.Calculate(ToStack()));
     _enterBeforeNextDigit = true;
 }
Exemplo n.º 10
0
 public CalculatorResult(List <int> values, IOperation operation)
 {
     Values    = values;
     Operation = operation;
     Result    = operation.Calculate(values);
 }
Exemplo n.º 11
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            ActionBar.Tab tab = ActionBar.NewTab();
            tab.SetText(Resources.GetString(Resource.String.calcTab));
            tab.TabSelected += (sender, args) =>
            {
                // Do something when tab is selected
            };
            ActionBar.AddTab(tab);

            tab = ActionBar.NewTab();
            tab.SetText(Resources.GetString(Resource.String.convertTab));
            tab.TabSelected += (sender, args) =>
            {
                // Do something when tab is selected
            };
            ActionBar.AddTab(tab);

            Button button0 = FindViewById <Button>(Resource.Id.Button0);
            Button button1 = FindViewById <Button>(Resource.Id.Button1);
            Button button2 = FindViewById <Button>(Resource.Id.Button2);
            Button button3 = FindViewById <Button>(Resource.Id.Button3);
            Button button4 = FindViewById <Button>(Resource.Id.Button4);
            Button button5 = FindViewById <Button>(Resource.Id.Button5);
            Button button6 = FindViewById <Button>(Resource.Id.Button6);
            Button button7 = FindViewById <Button>(Resource.Id.Button7);
            Button button8 = FindViewById <Button>(Resource.Id.Button8);
            Button button9 = FindViewById <Button>(Resource.Id.Button9);

            Button buttonAddition       = FindViewById <Button>(Resource.Id.ButtonAdd);
            Button buttonDivision       = FindViewById <Button>(Resource.Id.ButtonDiv);
            Button buttonFraction       = FindViewById <Button>(Resource.Id.ButtonFraction);
            Button buttonMultiplication = FindViewById <Button>(Resource.Id.ButtonMulti);
            Button buttonNegation       = FindViewById <Button>(Resource.Id.ButtonNegate);
            Button buttonSqrt           = FindViewById <Button>(Resource.Id.ButtonSqrt);
            Button buttonSubstraction   = FindViewById <Button>(Resource.Id.ButtonSub);
            Button buttonEqual          = FindViewById <Button>(Resource.Id.ButtonEqual);
            Button buttonBackspace      = FindViewById <Button>(Resource.Id.ButtonBackspace);
            Button buttonComma          = FindViewById <Button>(Resource.Id.ButtonComma);

            Button buttonC = FindViewById <Button>(Resource.Id.ButtonC);

            TextView resultField = FindViewById <TextView>(Resource.Id.textView1);
            TextView txt         = FindViewById <TextView>(Resource.Id.TextView);

            Button sin  = FindViewById <Button>(Resource.Id.sin);
            Button asin = FindViewById <Button>(Resource.Id.asin);
            Button cos  = FindViewById <Button>(Resource.Id.cos);
            Button acos = FindViewById <Button>(Resource.Id.acos);
            Button tan  = FindViewById <Button>(Resource.Id.tan);
            Button atan = FindViewById <Button>(Resource.Id.atan);
            Button ctg  = FindViewById <Button>(Resource.Id.ctg);
            Button actg = FindViewById <Button>(Resource.Id.actg);
            Button ln   = FindViewById <Button>(Resource.Id.ln);  // e
            Button log  = FindViewById <Button>(Resource.Id.log); // 10
            Button pi   = FindViewById <Button>(Resource.Id.pi);
            Button exp  = FindViewById <Button>(Resource.Id.exp);
            Button pov  = FindViewById <Button>(Resource.Id.pov);
            Button rand = FindViewById <Button>(Resource.Id.rand);
            Button mod  = FindViewById <Button>(Resource.Id.mod);
            Button div  = FindViewById <Button>(Resource.Id.div);

            #region button[0-9] handlers

            button0.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 0;
                _lastKeyInput     = Keys.Digit;
            };

            button1.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 1;
                _lastKeyInput     = Keys.Digit;
            };

            button2.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 2;
                _lastKeyInput     = Keys.Digit;
            };

            button3.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 3;
                _lastKeyInput     = Keys.Digit;
            };

            button4.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 4;
                _lastKeyInput     = Keys.Digit;
            };

            button5.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 5;
                _lastKeyInput     = Keys.Digit;
            };

            button6.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 6;
                _lastKeyInput     = Keys.Digit;
            };

            button7.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 7;
                _lastKeyInput     = Keys.Digit;
            };

            button8.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 8;
                _lastKeyInput     = Keys.Digit;
            };

            button9.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Operator)
                {
                    resultField.Text = "";
                }
                resultField.Text += 9;
                _lastKeyInput     = Keys.Digit;
            };

            #endregion

            buttonAddition.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign)
                {
                    parameters.Add(resultField.Text);
                    txt.Text        += resultField.Text + "+";
                    resultField.Text = "";

                    if (parameters.Count == 2)
                    {
                        double[] p = this.Parse(parameters);
                        double   r = op.Calculate(p);
                        resultField.Text = r.ToString();
                        parameters.Clear();
                        parameters.Add(r.ToString());
                    }

                    if (op == null || op.GetType() != typeof(Addition))
                    {
                        op = new Addition();
                    }
                    _lastKeyInput = Keys.Operator;
                    return;
                }

                if (_lastKeyInput == Keys.Operator)
                {
                    if (op == null || op.GetType() != typeof(Addition))
                    {
                        op       = new Addition();
                        txt.Text = ReplaceLastChar(txt.Text, '+');
                    }
                    _lastKeyInput = Keys.Operator;
                }
            };

            buttonSubstraction.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign)
                {
                    parameters.Add(resultField.Text);
                    txt.Text        += resultField.Text + "-";
                    resultField.Text = "";

                    if (parameters.Count == 2)
                    {
                        double[] p = this.Parse(parameters);
                        double   r = op.Calculate(p);
                        resultField.Text = r.ToString();
                        parameters.Clear();
                        parameters.Add(r.ToString());
                    }

                    if (op == null || op.GetType() != typeof(Subtraction))
                    {
                        op = new Subtraction();
                    }
                    _lastKeyInput = Keys.Operator;
                    return;
                }

                if (_lastKeyInput == Keys.Operator)
                {
                    if (op == null || op.GetType() != typeof(Subtraction))
                    {
                        op       = new Subtraction();
                        txt.Text = ReplaceLastChar(txt.Text, '-');
                    }
                    _lastKeyInput = Keys.Operator;
                }
            };

            buttonMultiplication.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign)
                {
                    parameters.Add(resultField.Text);
                    txt.Text        += resultField.Text + "*";
                    resultField.Text = "";

                    if (parameters.Count == 2)
                    {
                        double[] p = this.Parse(parameters);
                        double   r = op.Calculate(p);
                        resultField.Text = r.ToString();
                        parameters.Clear();
                        parameters.Add(r.ToString());
                    }

                    if (op == null || op.GetType() != typeof(Multiplication))
                    {
                        op = new Multiplication();
                    }

                    _lastKeyInput = Keys.Operator;
                    return;
                }

                if (_lastKeyInput == Keys.Operator)
                {
                    if (op == null || op.GetType() != typeof(Multiplication))
                    {
                        op       = new Multiplication();
                        txt.Text = ReplaceLastChar(txt.Text, '*');
                    }
                    _lastKeyInput = Keys.Operator;
                }
            };

            buttonDivision.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign)
                {
                    parameters.Add(resultField.Text);
                    txt.Text        += resultField.Text + "/";
                    resultField.Text = "";

                    if (parameters.Count == 2)
                    {
                        double[] p = this.Parse(parameters);
                        double   r = op.Calculate(p);
                        resultField.Text = r.ToString();
                        parameters.Clear();
                        parameters.Add(r.ToString());
                    }
                    if (op == null || op.GetType() != typeof(Division))
                    {
                        op = new Division();
                    }

                    _lastKeyInput = Keys.Operator;
                    return;
                }

                if (_lastKeyInput == Keys.Operator)
                {
                    if (op == null || op.GetType() != typeof(Division))
                    {
                        op       = new Division();
                        txt.Text = ReplaceLastChar(txt.Text, '/');
                    }
                    _lastKeyInput = Keys.Operator;
                }
            };

            buttonEqual.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign)
                {
                    parameters.Add(resultField.Text);
                    txt.Text = string.Empty;
                    if (parameters.Count == 2)
                    {
                        double r = op.Calculate(this.Parse(parameters));
                        resultField.Text = r.ToString();
                    }
                    _lastKeyInput = Keys.Equal;
                    return;
                }

                if (_lastKeyInput == Keys.Operator)
                {
                    txt.Text         = "";
                    resultField.Text = op.Calculate(double.Parse(parameters[0]), double.Parse(resultField.Text)).ToString();
                    parameters.Clear();
                    _lastKeyInput = Keys.Equal;
                    return;
                }

                if (_lastKeyInput == Keys.Equal && op != null)
                {
                    resultField.Text = op.Calculate(double.Parse(resultField.Text), double.Parse(resultField.Text)).ToString();
                    _lastKeyInput    = Keys.Equal;
                }
            };

            buttonNegation.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign || _lastKeyInput == Keys.Equal)
                {
                    //if (op == null || op.GetType() != typeof(Negation))
                    //   op = new Negation();

                    resultField.Text = new Negation().Calculate(double.Parse(resultField.Text)).ToString();
                    //resultField.Text = op.Calculate(double.Parse(resultField.Text)).ToString();
                    _lastKeyInput = Keys.Sign;
                }
            };

            buttonSqrt.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit ||
                    _lastKeyInput == Keys.Sign ||
                    _lastKeyInput == Keys.Equal)
                {
                    resultField.Text = new Sqrt().Calculate(double.Parse(resultField.Text)).ToString();
                }
            };

            buttonFraction.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign || _lastKeyInput == Keys.Equal)
                {
                    resultField.Text = new Fraction().Calculate(double.Parse(resultField.Text)).ToString();
                }
            };

            buttonBackspace.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Equal)
                {
                    if (!(string.IsNullOrEmpty(resultField.Text)))
                    {
                        resultField.Text = resultField.Text.Substring(0, resultField.Text.Length - 1);
                    }
                }
            };

            buttonComma.Click += (object sender, EventArgs e) =>
            {
                if (_lastKeyInput == Keys.Digit || _lastKeyInput == Keys.Sign || _lastKeyInput == Keys.Equal)
                {
                    if (!(string.IsNullOrEmpty(resultField.Text)))
                    {
                        resultField.Text += '.';
                        _lastKeyInput     = Keys.DecimalPoint;
                    }
                    else
                    {
                        resultField.Text += "0.";
                    }
                }
                _lastKeyInput = Keys.DecimalPoint;
            };

            buttonC.Click += (object sender, EventArgs e) =>
            {
                op = null;
                resultField.Text = string.Empty;
                txt.Text         = string.Empty;
                parameters.Clear();
            };

            ViewSwitcher viewSwitcher = FindViewById <ViewSwitcher>(Resource.Id.viewSwitcher);
            LinearLayout calcView1    = FindViewById <LinearLayout>(Resource.Id.calcView1);
            LinearLayout calcView2    = FindViewById <LinearLayout>(Resource.Id.calcView2);
            Button       functions    = FindViewById <Button>(Resource.Id.functions);
            Button       standart     = FindViewById <Button>(Resource.Id.standart);

            functions.Click += (object sender, EventArgs e) =>
            {
                if (viewSwitcher.CurrentView != calcView2)
                {
                    viewSwitcher.ShowNext();
                }
            };

            standart.Click += (object sender, EventArgs e) =>
            {
                if (viewSwitcher.CurrentView == calcView2)
                {
                    viewSwitcher.ShowPrevious();
                }
            };
        }
 public int Calculate(int value)
 {
     return(_second.Calculate(_first.Calculate(value)));
 }
 public int GetNumber()
 {
     return(_operation.Calculate(_value));
 }
Exemplo n.º 14
0
 public void Operation(IOperation op, int numb1, int numb2)
 {
     System.Diagnostics.Debug.WriteLine("Calculando...");
     op.Calculate(numb1, numb2);
 }
Exemplo n.º 15
0
 public int executeStrategy(int num1, int num2)
 {
     return(_operation.Calculate(num1, num2));
 }