예제 #1
0
        /// <summary>
        /// Implementation of multiplication method.
        /// </summary>
        /// <param name="multiplicationRequest"></param>
        /// <param name="logMult"></param>
        /// <returns></returns>
        public static MultiplicationResult Mult(MultiplicationRequest multiplicationRequest, string logMult)
        {
            try
            {
                MultiplicationResult multiplicationResult = new MultiplicationResult();

                int    counter     = 0;
                string calculation = "";
                multiplicationResult.Product = 1;

                foreach (int number in multiplicationRequest.Factors)
                {
                    counter += 1;
                    multiplicationResult.Product *= number;
                    calculation += number.ToString() + (counter == multiplicationRequest.Factors.Length ? "" : "*");
                }

                if (int.Parse(logMult) != 0)
                {
                    SaveLog(int.Parse(logMult), "Multiplicación ", calculation, multiplicationResult.Product);
                }

                return(multiplicationResult);
            }
            catch (Exception)
            {
                throw;
            }
        }
        private MultiplicationResult MultiplyTwoStringNumbers(string a, string b)
        {
            var result = new MultiplicationResult();

            var first  = a.Length >= b.Length ? a : b;
            var second = a.Length >= b.Length ? b : a;

            var addResult = "0";

            for (var i = second.Length - 1; i >= 0; i--)
            {
                for (var j = first.Length - 1; j >= 0; j--)
                {
                    MultiplyTwoDigits(second[i], first[j], result);
                }

                var trailingZeros = new string('0', second.Length - 1 - i);
                var tmpMulResult  = $"{result}{trailingZeros}";

                addResult = _adderService.Add(addResult, tmpMulResult);
                result.Reset();
            }

            result.Result = addResult;

            return(result);
        }
예제 #3
0
    // mr is a reference variable
    //caller of callback - caller always creates object for the delegate class
    // mr will recieve referance of delegate object
    // the delegate object holds address of function and [ref of object to be passes to function]
    // if function is static ref of object to be pases = null
    public static void multiplicationTable(int number, MultiplicationResult mr)
    {
        for (int i = 0; i < 10; i++)
        {
            int result = number * i;
            //Should make a callback with all the data that is required by caller.

            mr.Invoke(number, i, result); //callback with the help of delegate object
        }
    }
예제 #4
0
        public string FormatToString(MultiplicationResult result)
        {
            var leftColumnWidth  = result.LeftInput.ToString().Length;
            var rightColumnWidth = result.Steps.Last().Right.ToString().Length;

            return(FormatInputLine(result.LeftInput, result.RightInput, leftColumnWidth, rightColumnWidth) + "\n"
                   + Repeat('=', leftColumnWidth + rightColumnWidth + 3) + "\n"
                   + result.Steps
                   .Select(step => FormatStepLine(step.Left, step.Right, leftColumnWidth, rightColumnWidth))
                   .JoinToString("\n") + "\n"
                   + Repeat('=', leftColumnWidth + rightColumnWidth + 3) + "\n"
                   + FormatResultLine(result.Result, leftColumnWidth, rightColumnWidth)
                   );
        }
예제 #5
0
        public JsonResult PostMultiplication([FromBody] MultiplicationOperands multiplicationOperands)
        {
            MultiplicationResult result = new MultiplicationResult();
            string trackingId           = Request.Headers[MyStaticData.HEADER_TRACKING_ID];

            result = multiplicationService.Operate(multiplicationOperands);

            if (trackingId != string.Empty)
            {
                Operation operation = new Operation("Mul", string.Join(" * ", multiplicationOperands.Factors) + " = " + result.Product, DateTime.Now);
                User      user      = new User(new TrackingId(trackingId), operation);
                operationService.SaveOperations(user);
            }

            return(Json(result));
        }
        /// <summary>
        /// Method that connects to the Web API service to perform the multiplication operation.
        /// </summary>
        /// <param name="values"></param>
        /// <param name="logMultiplication"></param>
        public static void Multiplication(MultiplicationRequest values, string logMultiplication)
        {
            try
            {
                var clientRest  = new RestClient(urlWebAPI);
                var requestRest = new RestRequest("Mult", Method.POST);

                if (logMultiplication == "")
                {
                    logMultiplication = "0";
                }
                requestRest.AddHeader("x-evi-tracking-id", logMultiplication);

                requestRest.AddParameter("application/json", new JavaScriptSerializer().Serialize(values), ParameterType.RequestBody);
                IRestResponse responseRest = clientRest.Execute(requestRest);
                Console.WriteLine();
                if (responseRest.StatusCode == HttpStatusCode.OK)
                {
                    MultiplicationResult resultRest = (new JavaScriptSerializer()).Deserialize <MultiplicationResult>(responseRest.Content);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("\n\nRESULTADO, MULTIPLICACION: " + resultRest.Product + ". " + (logMultiplication != "0" ? " La operacion fue registrada en el log." : ""));
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error al realizar la operación: " + responseRest.ErrorMessage);
                }

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Operación finalizada.");
                Console.WriteLine();
            }
            catch (Exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\n\nError al conectarse con el servicio que realiza la operacion de multiplicación. ");
                throw;
            }
        }
예제 #7
0
        private static async void Multiply()
        {
            string url = BASE_URL + "multiplication";
            MultiplicationOperands multiplicationOperands = new MultiplicationOperands();
            HttpContent            content;
            HttpResponseMessage    response = new HttpResponseMessage();

            multiplicationOperands.Factors = AskForOperands();

            string json = JsonConvert.SerializeObject(multiplicationOperands);

            content = new StringContent(json, Encoding.UTF8, MyStaticData.MIME_TYPE_JSON);

            response = await client.PostAsync(url, content);

            Stream responseStream = await response.Content.ReadAsStreamAsync();

            string streamContent            = new StreamReader(responseStream).ReadToEnd();
            MultiplicationResult resultJson = JsonConvert.DeserializeObject <MultiplicationResult>(streamContent);

            Console.WriteLine(resultJson.ToString());
        }
        private void MultiplyTwoDigits(char a, char b, MultiplicationResult res)
        {
            var aNumber = (int)char.GetNumericValue(a);
            var bNumber = (int)char.GetNumericValue(b);

            if (res == null)
            {
                // First result
                res = new MultiplicationResult();
            }

            var result = aNumber * bNumber + res.CarryInDigit;

            if (result < 10)
            {
                res.CarryInDigit = 0;
                res.Result       = res.Result.Insert(0, result.ToString());
            }
            else
            {
                res.CarryInDigit = result / 10;
                res.Result       = res.Result.Insert(0, (result % 10).ToString());
            }
        }