Пример #1
0
        public void SubstituteOperationsWithResult(ref string calculationString, string symbol)
        {
            do
            {
                if (!calculationString.Contains(symbol))
                {
                    break;
                }

                // Break if Negative Result, with no other calculations eg. -50
                if (symbol == OperatorSymbol.Subtract && calculationString.Substring(0, 1) == symbol && !calculationString.Substring(1).Contains(symbol))
                {
                    break;
                }

                // Break if Double Negative Result, with no other calculations eg. --50
                if (symbol == OperatorSymbol.Subtract && calculationString.Substring(0, 2) == (symbol + symbol) && !calculationString.Substring(2).Contains(symbol))
                {
                    calculationString = calculationString.Replace((symbol + symbol), "");

                    break;
                }

                string operationString = _operationIdentifier.GetNextOperationMatch(calculationString, symbol);

                IOperator Operation = _operationFactory.Create(operationString);

                decimal Result = Operation.GetResult();

                calculationString = calculationString.Replace(operationString, Result.ToString());
            }while (calculationString.Contains(symbol));
        }
Пример #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var readerJObject = JObject.Load(reader);

            if (readerJObject == null)
            {
                throw new JsonSerializationException("Cannot deserialize, not a valid operation Json.");
            }

            var type = readerJObject["type"].ToString();

            if (!Enum.TryParse(type, true, out OperationType operationType))
            {
                throw new JsonSerializationException("Cannot deserialize, not a valid operation type " +
                                                     $"'{type}'.");
            }

            var @operator = _operatorFactory.Create(operationType);

            var operation = @operator.CreateOperation();

            serializer.Populate(readerJObject.CreateReader(), operation);
            return(operation);
        }
Пример #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request)
        {
            var operation = await request.Content.ReadAsAsync <IOperation>(new[] { new JsonMediaTypeFormatter {
                                                                                       SerializerSettings = new JsonSerializerSettings {
                                                                                           Converters = new List <JsonConverter> {
                                                                                               new OperationConverter(_operatorFactory)
                                                                                           },
                                                                                           ContractResolver  = new CamelCasePropertyNamesContractResolver(),
                                                                                           NullValueHandling = NullValueHandling.Ignore
                                                                                       }
                                                                                   } });

            var @operator = _operatorFactory.Create(operation.Type);

            if (@operator == null)
            {
                return(new BadRequestObjectResult("Invalid operation"));
            }

            var operationResult = @operator.Operate(operation);

            return(new OkObjectResult(operationResult));
        }