public ActionResult Index(string operationType, string operation, string FirstArgument, string SecondArgument)
 {
     String result;
     try
     {
         var validator = new Validator();
         switch (operationType)
         {
             case "oneArgument":
                 var argument = validator.ValidateNumber(FirstArgument);
                 var calculator = OneArgmumentFactory.CreateCalculator(operation);
                 result = Convert.ToString(calculator.Calculate(argument));
                 break;
             case "twoArguments":
                 var firstArgument = validator.ValidateNumber(FirstArgument);
                 var secondArgument = validator.ValidateNumber(SecondArgument);
                 var calculator2 = TwoArgmumentFactory.CreateCalculator(operation);
                 result = Convert.ToString(calculator2.Calculate(firstArgument, secondArgument));
                 break;
             case "order":
                 var array = validator.ValidateArray(FirstArgument);
                 var calculator3 = ArraySortFactory.CreateCalculator(operation);
                 calculator3.Calculate(array);
                 result = "";
                 for (Int16 i = 0; i < array.Length; i++)
                 {
                     result += Convert.ToString(array[i]);
                     result += ", ";
                 }
                 break;
             default:
                 result = "Undefined operation";
                 break;
         }
     }
     catch (InvalidOperationException)
     {
         result = "Error: second argument is empty";
     }
     catch (Exception ex)
     {
         result = ex.Message;
     }
     TempData["Result"] = new Dictionary<string, object>
                              {
                                  { "result", result }
                              };
     return RedirectToAction("Result");
 }
Example #2
0
 /// <summary>
 /// Event and exception handler for one-operand functoins
 /// </summary>
 /// <param name="sender">Pressed button name</param>
 private void ComputeOne(object sender, EventArgs e)
 {
     try
       {
       var validator = new Validator();
       double argument = validator.ValidateNumber(FirstValue.Text);
       string operation = ((Button)sender).Name;
       var op = OneArgmumentFactory.CreateCalculator(operation);
       double result = op.Calculate(argument);
       Result.Text = Convert.ToString(result);
       }
       catch (Exception ex)
       {
       MessageBox.Show(ex.Message, "You are a clayfish", MessageBoxButtons.OK, MessageBoxIcon.Warning);
       }
 }
Example #3
0
 /// <summary>
 /// Function for operations with two numbers
 /// </summary>
 /// <param name="sender">Button, that activated ivent</param>
 /// <param name="e">Operation type</param>
 private void Calculator(object sender, EventArgs e)
 {
     try
     {
         var validator = new Validator();
         double firstArgument = validator.ValidateNumber(FirstInput.Text);
         double secondArgument = validator.ValidateNumber(SecondInput.Text);
         string operationName = (((Button) sender).Name);
         var operation = TwoArgumentFactory.CreateCalculator(operationName);
         double result = operation.Calculate(firstArgument, secondArgument);
         Value.Text = Convert.ToString(result);
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK);
     }
 }
Example #4
0
        /// <summary>
        /// Function for array sorting
        /// </summary>
        /// <param name="sender">Button, that activated ivent</param>
        /// <param name="e">Operation type</param>
        private void ArraySorting(object sender, EventArgs e)
        {
            try
            {
                var validator = new Validator();
                string[] split = FirstInput.Text.Split(' ');
                double[] array = new double[split.Length];

                for (int i = 0; i < split.Length; i++)
                {
                    array[i] = validator.ValidateNumber(split[i]);
                }

                string operationName = (((Button) sender).Name);
                var operation = ArraySortingFactory.CreateSorting(operationName);
                double[] resultDoubleArray = operation.Sort(array);

                string[] resultStringArray = new string[split.Length];

                for (int i = 0; i < array.Length; i++)
                {
                    resultStringArray[i] = Convert.ToString(resultDoubleArray[i]);
                }

                string resultString = "";

                for (int i = 0; i < resultStringArray.Length; i++)
                {
                    resultString += resultStringArray[i] + ' ';
                }

                Value.Text = resultString;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK);
            }
        }
Example #5
0
 /// <summary>
 /// Event and exception handler for array sorting functoins
 /// </summary>
 /// <param name="sender">Pressed button name</param>
 private void SortArray(object sender, EventArgs e)
 {
     try
     {
         if (FirstValue.Text.Equals(""))
         {
             throw new Exception("Input field is empty");
         }
         var validator = new Validator();
         double[] argument = validator.ValidateArray(FirstValue.Text);
         string operation = ((Button) sender).Name;
         var op = ArraySortFactory.CreateCalculator(operation);
         op.Calculate(argument);
         Result.Text = "";
         for (Int16 i = 0; i < argument.Length; i++)
         {
             Result.Text += Convert.ToString(argument[i]);
             Result.Text += ", ";
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "You are a clayfish", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }