Пример #1
0
        /// <summary>
        /// Invoked when a key is pressed, checked the key types and do the required operation as per the condition or case.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string SendKeyPress(char key)
        {
            // Add your implementation here.
            if (IsListOfKeys(key))
            {
                if (CalculatorUtility.IsNumber(key))
                {
                    if (calStr.ToString() == "0" && key == '0')
                    {
                        return(calStr.ToString());
                    }
                    else if (calStr.ToString() == "0" && key != '0')
                    {
                        calStr.Clear();
                    }
                    //return calStr.Append(key.ToString()).ToString();
                    calStr.Append(key.ToString()).ToString();
                    double.TryParse(calStr.ToString(), out double TotalNum);
                    entity.lastCalcNumber = TotalNum;
                    return(calStr.ToString());
                }
                else if (CalculatorUtility.IsDecimalPoint(key))
                {
                    if (calStr.Length > 0 && !calStr.ToString().Contains("."))
                    {
                        return(calStr.Append(key.ToString()).ToString());
                    }
                    else
                    {
                        return(calStr.ToString());
                    }
                }
                else if (CalculatorUtility.IsOperator(key))
                {
                    CallConditionalCalc(key);
                }

                else if (IsReset(key))
                {
                    return(Reset());
                }
                else if (IsSign(key) && calStr.Length > 0)
                {
                    return(ChangeSign());
                }
                return(entity.errorChar ?? entity.result.ToString());
            }
            return(calStr.ToString());

            //throw new NotImplementedException();
        }
Пример #2
0
 public string SendKeyPress(char key)
 {
     if (calculatorUtility.IsResetKey(key))
     {
         calculatorUtility = new CalculatorUtility();
         return("0");
     }
     else if (calculatorUtility.isKeyZero(key))
     {
         if (!calculatorUtility.Number.Equals("0"))
         {
             calculatorUtility.Number += key;
         }
         return(calculatorUtility.Number);
     }
     else if (calculatorUtility.IsNumber(key))
     {
         calculatorUtility.Number += key;
         return(calculatorUtility.Number);
     }
     else if (calculatorUtility.IsNegativeNumber(key))
     {
         int temp;
         if (int.TryParse(calculatorUtility.Number, out temp))
         {
             calculatorUtility.Number = (-temp).ToString();
         }
         else
         {
             calculatorUtility.Number = (-float.Parse(calculatorUtility.Number)).ToString();
         }
         return(calculatorUtility.Number);
     }
     else if (calculatorUtility.IsDecimalPoint(key))
     {
         int temp;
         if (int.TryParse(calculatorUtility.Number, out temp))
         {
             calculatorUtility.Number += key;
         }
         return(calculatorUtility.Number);
     }
     else if (calculatorUtility.IsArithmeticOperator(key))
     {
         if (calculatorUtility.Flag == 0 && calculatorUtility.PreviousOperator != '?')
         {
             if (!SetFlagAndPreviousOperator(key))
             {
                 return("-E-");
             }
         }
         else
         {
             if (calculatorUtility.calculateResult(key) == "-E-")
             {
                 return("-E-");
             }
         }
         return(calculatorUtility.Result.ToString());
     }
     else if (calculatorUtility.IsEqualOperator(key))
     {
         if (calculatorUtility.Number == "")
         {
             calculatorUtility.Number = calculatorUtility.Result.ToString();
         }
         return(SendKeyPress(calculatorUtility.PreviousOperator));
     }
     else
     {
         return(calculatorUtility.Number);
     }
     //throw new NotImplementedException();
 }