public SqrtRes Post([FromBody] SqrtReq value) { SqrtRes result = new SqrtRes(); try { /* Obtiene la raiz cuadrada del numero dentro de la peticion, * cuando el encabezado no contiene el identificador "X-Evi-Tracking-Id" */ if (String.IsNullOrEmpty(Request.Headers["X-Evi-Tracking-Id"])) { result.Square = Math.Sqrt(value.Number); } /* Si el encabezado contiene el "X-Evi-Tracking-Id" se efectua la operacion * y se crea un archivo con el nombre del id del usuario y los datos de la * operacion realizada */ else { result.Square = Math.Sqrt(value.Number); LogJournal logJournal = new LogJournal(Request.Headers["X-Evi-Tracking-Id"]); logJournal.Add("{" + "\"" + "Operation" + "\"" + ": " + "\"" + "Sqrt" + "\"" + ", " + "\"" + "Calculation" + "\"" + ": " + "\"" + value.Number + " root = " + result.Square + "\"" + ", " + "\"" + "Date" + "\"" + ": " + "\"" + DateTime.Now.ToString("u") + "\"" + "}"); } } catch (Exception ex) { Log log = new Log(); log.Add("Error en SubController.cs " + ex.Message); } return(result); }
public static double CalculateRPN(string inputStr) { List <string> resList = new List <string>(); string num = string.Empty; if (!Char.IsDigit(inputStr.Last()) && inputStr.Last() != ')') { inputStr = inputStr.TrimEnd(inputStr.Last()); } for (int i = 0; i < inputStr.Length; i++) { char c = inputStr[i]; if (IsSqrt(inputStr, out double SqrtRes, ref i)) { resList.Add(SqrtRes.ToString()); continue; } if (Char.IsDigit(c) || c == ',' || c == '.') { num += c; } else if ((i == 0 && c == '-') || (c == '-' && !Char.IsDigit(inputStr[i - 1]) && inputStr[i - 1] != ')')) { num += c.ToString(); } else { if (num != "") { resList.Add(num); } resList.Add(c.ToString()); num = string.Empty; } } if (num != "") { resList.Add(num); } return(CalculateRPN(resList)); }
// // POST: /Calculator/Sqrt public void Sqrt() { try { // Receive and process the POST data string res = HttpContext.Request.Method.ToString(); Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); StreamReader responseStream = new StreamReader(HttpContext.Request.Body, enc); string body = responseStream.ReadToEnd(); Sqrt sqrt = JsonConvert.DeserializeObject <Sqrt>(body); // Get the result Calculator C = new Calculator(); int squ = C.sqrt(sqrt.Number); // Create a Result Object SqrtRes sqrtres = new SqrtRes(); sqrtres.Square = squ; // Serialize it string param = JsonConvert.SerializeObject(sqrtres); // Send it back using POST HttpContext.Response.ContentType = "application/json"; using (var streamWriter = new StreamWriter(HttpContext.Response.Body, enc)) { streamWriter.Write(param); streamWriter.Flush(); streamWriter.Close(); } var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Sqrt operation"); } catch (System.Exception e) { // Create a Result Object CalculatorService.Client.Exception ex = new CalculatorService.Client.Exception(); ex.ErrorCode = "InternalError"; ex.ErrorStatus = 500; ex.ErrorMessage = e.Message; // Serialize it string exec = JsonConvert.SerializeObject(ex); // Send it back using POST HttpContext.Response.ContentType = "application/json"; Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); using (var streamWriter = new StreamWriter(HttpContext.Response.Body, enc)) { streamWriter.Write(exec); streamWriter.Flush(); streamWriter.Close(); } var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Object reference not set to an instance of an object."); } }