Exemple #1
0
        private void PerformOperator(OperatorType oprt)
        {
            if (_operation.FirstValue == "")
            {
                if (label1.Text != "")
                {
                    _operation.FirstValue = label1.Text;
                    _operation.Operator   = oprt;
                    label1.Text           = "";
                }
                return;
            }
            if (label1.Text != "")
            {
                _operation.SecondValue = label1.Text;

                var result = _operation.Calculate();

                if (double.IsInfinity(Convert.ToDouble(result)))
                {
                    label2.Text = "Cannot divide by zero";
                }
                else
                {
                    _operation.FirstValue  = result;
                    label2.Text            = result;
                    _operation.SecondValue = string.Empty;
                    label1.Text            = string.Empty;
                    _operation.Operator    = oprt;
                }
            }
            else
            {
                _operation.Operator = oprt;
            }
        }
Exemple #2
0
 public double Calculate()
 {
     return(oper.Calculate(NumberA, NumberB));
 }
Exemple #3
0
        protected void OpButton_Click(object sender, EventArgs e)
        {
            Button Button     = (Button)sender;
            string ButtonText = Button.Text;

            // if last entry is a digit, calculate the new result and record the new operand and add it to the expression
            if (char.IsDigit(LastEntry))
            {
                Operation.SecondNumber = Double.Parse(CurrNumberString);
                CurrNumberString       = Math.Round(Operation.Calculate(), 10).ToString();

                Operation.FirstNumber = Double.Parse(CurrNumberString);
                Operation.Operand     = ButtonText;

                if (Expression == "")
                {
                    Expression = CurrNumberString;
                }
                Expression = Expression.Insert(Expression.Length, ButtonText);
                LastEntry  = Expression[Expression.Length - 1];
            }
            else if (LastEntry == '=')
            {
                Expression = CurrNumberString.Insert(CurrNumberString.Length, ButtonText);
                LastEntry  = Expression[Expression.Length - 1];
            }
            // if last entry is not a digit, aka it's an operand, update the operation and change the expression and last entry
            else
            {
                Operation.Operand = ButtonText;

                if (Expression != "")
                {
                    Expression = Expression.Remove(Expression.Length - 1);
                }
                else
                {
                    Expression = CurrNumberString;
                }
                Expression = Expression.Insert(Expression.Length, ButtonText);
                LastEntry  = Expression[Expression.Length - 1];
            }

            ExpressionText.Text = Expression;
            CurrNumberText.Text = CurrNumberString;

            if (Operation.OperationFlag == 1)
            {
                CurrNumberText.Text     = "Cannot divide by zero";
                CurrNumberString        = "0";
                Expression              = "";
                ExpressionText.Text     = Expression;
                LastEntry               = '0';
                Operation.OperationFlag = 0;
            }
            else if (Operation.OperationFlag == 2)
            {
                CurrNumberText.Text     = "Result is undefined";
                CurrNumberString        = "0";
                Expression              = "";
                ExpressionText.Text     = Expression;
                LastEntry               = '0';
                Operation.OperationFlag = 0;
            }

            Session["CurrNumberString"] = CurrNumberString;
            Session["Expression"]       = Expression;
            Session["LastEntry"]        = LastEntry;
            Session["Operation"]        = Operation;
        }
Exemple #4
0
        private string ParseOperation()
        {
            string numbers   = "0123456789.";
            string operators = "+-*/";

            try
            {
                string userInput = this.userInputText.Text;
                userInput = userInput.Replace(" ", ""); //remove spaces

                if (userInput.Length == 0)
                {
                    throw new ArgumentNullException();
                }

                var operation = new Operation();
                var leftSide  = true;

                for (int i = 0; i < userInput.Length; i++)
                {
                    if (numbers.Any(c => userInput[i] == c))
                    {
                        if (leftSide)
                        {
                            operation.LeftSide = AddNumberPart(operation.LeftSide, userInput[i]);
                        }
                        else
                        {
                            operation.RightSide = AddNumberPart(operation.RightSide, userInput[i]);
                        }
                    }
                    else if (operators.Any(c => userInput[i] == c))
                    {
                        if (!leftSide)
                        {
                            var operatorType = GetOperationType(userInput[i]);

                            if (operation.RightSide.Length == 0)
                            {
                                if (operatorType != OperartionType.Minus)
                                {
                                    throw new InvalidOperationException();
                                }

                                operation.RightSide += userInput[i];
                            }
                            else
                            {
                                operation.LeftSide       = operation.Calculate();
                                operation.operartionType = operatorType;
                                operation.RightSide      = string.Empty;
                            }
                        }
                        else
                        {
                            var operatorType = GetOperationType(userInput[i]);

                            if (operation.LeftSide.Length == 0)
                            {
                                if (operatorType != OperartionType.Minus)
                                {
                                    throw new InvalidOperationException();
                                }

                                operation.RightSide += userInput[i];
                            }
                            else
                            {
                                operation.operartionType = operatorType;

                                leftSide = false;
                            }
                        }
                    }
                }

                return(operation.Calculate());
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }