示例#1
0
 static void Main(string[] args)
 {
     Stopwatch timer = new Stopwatch();
     string expString;
     Expression exp;
     double a = 0;
     while ((expString = Console.ReadLine()) != string.Empty)
     {
         try
         {
             exp = new Expression(expString);
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
             continue;
         }
         timer.Reset();
         timer.Start();
         for (int i = 0; i < 1000000; i++)
         {
             a = exp.Evaluate();
         }
         timer.Stop();
         Console.WriteLine(a);
         Console.WriteLine("{0} calculations per second", 1000000 / timer.Elapsed.TotalSeconds);
     }
 }
示例#2
0
        public void AddExpression(double arg1, double arg2, char operation)
        {
            var @operator = _operators.FirstOrDefault(x => x.Key == operation);

            if (@operator == null)
            {
                //throw new InvalidOperationException("Operator not found");
                Console.WriteLine("Operator is not implemented");
                return;
            }

            var expr = new Expression(arg1, arg2, @operator);
            expr.Calculate();
            AddToHistoryList(expr);
            System.Console.WriteLine(expr.ToString());
        }
示例#3
0
 private Expression ParseDouble(LinkedListNode<string> node)
 {
     double termDouble;
     if (double.TryParse(node.Value, out termDouble))
     {
         Number term = new Number(termDouble);
         Expression expressionFromDouble = new Expression(term);
         return expressionFromDouble;
     }
     return new Expression();
 }
示例#4
0
 private Expression ParseSubtraction(Expression leftOperand, Expression rightOperand)
 {
     //return new Expression();
     return Expression.Minus(leftOperand, rightOperand);
 }
示例#5
0
 private Expression ParseAddition(Expression leftOperand, Expression rightOperand)
 {
     //return new Expression();
     return Expression.Plus(leftOperand, rightOperand);
 }
示例#6
0
 private Expression ParseDivision(Expression leftOperand, Expression rightOperand)
 {
     //return new Expression();
     return Expression.Divided(leftOperand, rightOperand);
 }
示例#7
0
 private Expression ParseMultiplication(Expression leftOperand, Expression rightOperand)
 {
     //return new Expression();
     return Expression.Times(leftOperand, rightOperand);
 }
示例#8
0
 private Expression ParseExponent(Expression leftOperand, Expression rightOperand)
 {
     return Expression.Power(leftOperand, rightOperand);
 }
示例#9
0
 private Expression ParseOperator(Expression leftExpression, Expression rightExpression, string oper)
 {
     switch (oper)
     {
         case "**":
             return ParseExponent(leftExpression, rightExpression);
         case "*":
             return ParseMultiplication(leftExpression, rightExpression);
         case "/":
             return ParseDivision(leftExpression, rightExpression);
         case "+":
             return ParseAddition(leftExpression, rightExpression);
         case "-":
             return ParseSubtraction(leftExpression, rightExpression);
     }
     //}
     return new Expression();
 }
示例#10
0
        private void ParseListToExpression(LinkedListNode<string> node)
        {
            int expressionNumber = 0;
            double throwAway;
            for (LinkedListNode<string> current = node.List.First; current != null; current = current?.Next)
            {

                if (operators.Contains(current.Value))
                {
                    if (double.TryParse(current?.Previous?.Value, out throwAway) &&
                        double.TryParse(current?.Next?.Value, out throwAway))
                    {
                        
                        _expression = ParseOperator(ParseDouble(current.Previous), ParseDouble(current.Next),
                            current.Value);
                        _expressionList.AddLast(_expression);                                             
                        RemoveParen(current, expressionNumber);
                        expressionNumber++;
                    }
                    if (double.TryParse(current?.Previous?.Value, out throwAway) &&
                        (current?.Next?.Value?.Contains("E") ?? false))
                    {

                        LinkedListNode<Expression> expressionNode =  FindExpressionNode(current?.Next);
                        expressionNode.Value = ParseOperator(ParseDouble(current.Previous), expressionNode.Value, current.Value);
                        RemoveParen(current, GetIntFromString(current?.Next?.Value.Remove(0, 1)));
                    }
                    if ((current?.Previous?.Value?.Contains("E") ?? false) &&
                        double.TryParse(current?.Next?.Value, out throwAway))
                    {
                        LinkedListNode<Expression> expressionNode = FindExpressionNode(current?.Previous);
                        expressionNode.Value = ParseOperator(expressionNode.Value, ParseDouble(current.Next), current.Value);
                        RemoveParen(current, GetIntFromString(current?.Previous?.Value.Remove(0, 1)));
                    }
                    if ((current?.Previous?.Value?.Contains("E") ?? false) && (current?.Next?.Value?.Contains("E") ?? false))
                    {
                        LinkedListNode<Expression> expressionNode = FindExpressionNode(current?.Previous);
                        expressionNode.Value = ParseOperator(expressionNode.Value, expressionNode?.Next?.Value, current.Value);
                        expressionNode.List.Remove(expressionNode?.Next);
                        RemoveParen(current, GetIntFromString(current?.Next?.Value.Remove(0, 1)));
                        RenameExpressionNodes(current);

                    }
                    
                }
            }
            if (_expressionList.Count > 1)
            {
                //throw new ExecutionEngineException("_expressionList has too many items and was not processed.");
            }
            else
            {
                _expression = _expressionList.First.Value;
            }
        }
示例#11
0
 public void AddToHistoryList(Expression e)
 {
     _historylist.Add(e);
 }
示例#12
0
        static void Main(string[] args)
        {
            Expression e = new Expression(args[1]);

            System.Console.WriteLine(e.Evaluate());
        }
示例#13
0
 void calculate()
 {
     try
     {
         Expression exp = new Expression(Display);
         exp.SetVariable("Ans", answer);
         answer = exp.Evaluate();
         string ans = answer.ToString();
         allClear();
         int i = 0;
         if (ans[0] == '-')
         {
             addToDisplay(Tokens.UnaryMinus, "-");
             i++;
         }
         if (char.IsDigit(ans[i]))
         {
             foreach (char c in ans)
             {
                 if (char.IsDigit(c))
                     addToDisplay(Tokens.Digit, c.ToString());
                 else if (c == '.')
                     addToDisplay(Tokens.Point, c.ToString());
                 else if (c == 'E')
                     addToDisplay(Tokens.Exp, c.ToString());
                 else if (c == '+' || c == '-')
                     addToDisplay(Tokens.ExpOperator, c.ToString());
             }
         }
         else
         {
             // NaN, Infinity etc.
             addToDisplay(Tokens.Variable, ans.Substring(i, ans.Length - i));
         }
         resultState = true;
     }
     catch (Exception e) when (e is ArgumentException || e is OverflowException || e is FormatException)
     {
         // OverflowException and FormatException are thrown by Double.Parse from within Expression.Evaluate
     }
 }