|
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.
I'm a Master degree student, studying at the University of Joensuu, Finland.