Click here to Skip to main content
15,894,410 members
Articles / Programming Languages / C#

Inside the Mathematical Expressions Evaluator

Rate me:
Please Sign up or sign in to vote.
4.88/5 (43 votes)
17 Jan 2008CPOL8 min read 116.2K   3.4K   77  
An article on mathematical expression evaluation
using System;
using System.Text.RegularExpressions;

namespace SevenZ.Calculator
{
   public partial class Calculator
   {
      internal static class Token
      {
         public const string PRight = ")", PLeft = "(", Power = "^", Divide = "/",
                            Multiply = "*", UnaryMinus = "_", Add = "+", Subtract = "-",
                            Sentinel = "#", End = ";", Operand = "@", None = " ";
                            

         static string[] binaryOperators = new string[] { Multiply, Divide, Subtract, 
                                                          Add, Power };
         static string[] unaryOperators = new string[] { UnaryMinus };         

         public static int Precedence(string op)
         {
            switch (op)
            {
               case Subtract:    return 4;
               case Add:         return 4;
               case UnaryMinus:  return 8;
               case Multiply:    return 16;
               case Divide:      return 16;
               case Power:       return 32;
               case PLeft:       return 64;
               case PRight:      return 64;

               default: return 0; //operators END, Sentinel
            }
         }

         #region IsFunctions
         public static bool IsBinary(string op)
         {
            return Contains(op, binaryOperators);
         }

         public static bool IsUnary(string op)
         {
            return Contains(op, unaryOperators);
         }

         public static bool IsDigit(string token)
         {
            return Regex.IsMatch(token.ToString(), @"\d+.?\d*");
         }

         #endregion

         /// <summary>
         /// Converts unary operator from expression to driver-comprehensible mode
         /// </summary>
         /// <param name="op">Unary operator</param>
         /// <returns>Converted operator</returns>
         public static string ConvertOperator(string op)
         {
            switch (op)
            {
               case UnaryMinus: return "_";
               default: return op;
            }
         }

         public static string ToString(string op)
         {
            switch (op)
            {
               case End: return "END";
               default: return op.ToString();
            }
         }
      }

      static bool Contains(string token, string[] array)
      {
         return Array.IndexOf<string>(array, token) != -1;
      }
   }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Finland Finland
I'm a Master degree student, studying at the University of Joensuu, Finland.

Comments and Discussions