Ejemplo n.º 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);
     }
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Expression e = new Expression(args[1]);

            System.Console.WriteLine(e.Evaluate());
        }
Ejemplo n.º 3
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
     }
 }