пятница, 19 июня 2009 г.

Math parse expressions. c# java

PS math parser .net , Math expression parser, Mathematical Expression Parser , .Net Math Parser, mathematical expression parsing

easy to use use and modify ( add functions , add your objects and so on)

If you can parse math expressions like this

5*4+3*7+8/3+abs(10-15 ) + max( 100 , 11, 1 ) + min ( 4 , 2) + X +X*X + ......

Visual studio solution you can download here http://stream.ifolder.ru/12757267
http://rapidshare.com/files/276525245/MathParse__1_.zip





1) generate list of tokens , where one token is object
2) create tree of tokens
3) create IValue object of math expression

1)
token is
  public class Token
  {
    public List<Token> tokens;
    public IValue ivalue;

    public string type;   
    public string value;  
    public string parseInfo;
  }

* This source code was highlighted with Source Code Highlighter.


2) 5+max(1 ,2 ) = INT ACTION FUNCTION

  public interface IValue
  {
    int Value( int x );
  }


* This source code was highlighted with Source Code Highlighter.


3)
public class INT : IValue
  {
    int i;

    public int I
    {
      set { i = value; }
      get { return i; }
    }

    public INT(int i)
    {
      this.i = i;
    }

    public int Value( int x )
    {
      return i;
    }
  }


  public class VARIABLE : IValue
  {
    public int Value( int x )
    {
      return x;
    }
  }

  class ACTION : IValue
  {
    public string action;
    public IValue v0;
    public IValue v1;

    public ACTION(IValue value0, string action, IValue value1)
    {
      this.v0 = value0;
      this.v1 = value1;
      this.action = string.Intern(action);
    }

    public int Value(int x)
    {
      switch (action)
      {
        case "&&":

          if (v0.Value(x) == 0) return 0;
          if (v1.Value(x) == 0) return 0;

          return 1;
        case "||":

          if (v0.Value(x) != 0) return 1;
          if (v1.Value(x) != 0) return 1;

          return 0;
      }

      int i0 = v0.Value(x);
      int i1 = v1.Value(x);

      switch (action)
      {
        case "+":
          return i0 + i1;
        case "-":
          return i0 - i1;
        case "*":
          return i0 * i1;
        case "/":
          return i0 / i1;
        case "%":
          return i0 % i1;

        case "<":
          return i0 < i1 ? 1 : 0;
        case ">":
          return i0 > i1 ? 1 : 0;
        case "<=":
          return i0 <= i1 ? 1 : 0;
        case ">=":
          return i0 >= i1 ? 1 : 0;
        case "==":
          return i0 == i1 ? 1 : 0;
        case "!=":
          return i0 != i1 ? 1 : 0;
      }

      throw new Exception("dsqwertyds");

    }

  }

  class FUNCTION : IValue
  {
    public List<IValue> value;
    public string nameFunction;

    public FUNCTION(string nameFunction, List<IValue> value)
    {
      this.nameFunction = string.Intern(nameFunction);
      this.value = value;
    }

    public int Value( int x)
    {
      int xx;

      switch (nameFunction)
      {
        case "max":

          if (value.Count == 0) throw new Exception("ERROR : function max dont have argument");

          xx = int.MinValue;
          int[] ii = new int[value.Count];

          for (int j = 0; j < value.Count; j++)
          {
            int a = ii[j] = value[j].Value(x);
            if (a > xx) xx = a;
          }
          return xx;
        case "min":

          if (value.Count == 0) throw new Exception("ERROR : function min dont have argument");

          xx = int.MaxValue;
          int[] iii = new int[value.Count];

          for (int j = 0; j < value.Count; j++)
          {
            int a = iii[j] = value[j].Value(x);
            if (a < xx) xx = a;
          }

          return xx;
        case "abs":

          if (value.Count != 1) throw new Exception("ERROR : function abs dont have argument");

          xx = value[0].Value(x);



          return (xx < 0) ? -xx : xx;
      }

      throw new Exception("function not exist : " + nameFunction);

    }

  }


* This source code was highlighted with Source Code Highlighter.

Комментариев нет:

Отправить комментарий