示例#1
0
        /// <summary>
        /// 取得運算結果。body可能會有數字(int)、右括號(bool)、單元運算列(List(char))
        /// </summary>
        /// <param name="userIdForAns">用戶ID</param>
        /// <returns>運算結果</returns>
        public IHttpActionResult PostAnswer(int userIdForAns)
        {
            //讀取body
            EqualExpression      equalexpression = Request.Content.ReadAsAsync <EqualExpression>().Result;
            ExpressionController expController   = Users.GetExpressionController(userIdForAns);

            List <UnaryOperator> unaryList = (equalexpression.UnaryList == null) ?
                                             new List <UnaryOperator>()
                : equalexpression.UnaryList.Select(x => Operators.GetUnary(x)).ToList();

            decimal?number = equalexpression.Number;
            decimal ans    = 0;

            try
            {
                switch (equalexpression.Type())
                {
                case EqualType.NUM_EQUAL:
                    expController.Add(number.Value, unaryList);
                    ans = expController.GetResult();
                    break;

                case EqualType.NUM_RB_EQUAL:
                    expController.Add(number.Value, unaryList);
                    expController.RightBracket();
                    ans = expController.GetResult();
                    break;

                default:
                    throw new Exception("EqualType錯誤");
                }
            }
            catch (Exception)
            {
                expController.Clear();
                return(Ok("fail"));
            }
            return(Ok(ans));
        }
示例#2
0
 public void Clear()
 {
     Number = null;
     expController.Clear();
 }