Jim

Jim

  • NA
  • 1
  • 0

adding a factorial operator to an expression tree

Dec 7 2006 4:48 PM
My task is to add a factorial operator within this expression tree through this code:

public class EvalTree // see the BST words example

{

public long i;

private Node root = null;

private string rpnExpr;

private Dictionary<string, long> values = new Dictionary<string, long>();

// This is a special list where all the words are stored and a copy of it is here

// The copy is named in this section values

private class Node

{

public Node(char d)//constuction func for private class Node taking one parameter

{

data = d; // a charecter as defined above

leftPtr = null;

rightPtr = null;

singlePtr = null;

}

public char data;

public Node leftPtr;

public Node rightPtr;

public Node singlePtr;

//public Node x;

//public Node y;

}

public string RpnExpr { get { return rpnExpr; } }//returns the value of rpnExpr

public void addValue(char c, long v)//function to add values to the dictionary

{

values.Add(c.ToString(), v);// v is long int and c is a charecter

}

public void buildTree(string rpn) //function with one parameter that returns no value (void)

{

rpnExpr = rpn;

build(ref root, ref rpn);//niether will be changed after the funtion...reference

}

public long evalTree()

{

return eval(root); // return eval that has the parameter root (value parmaeter)

}

public long getValue(char c)

{

return values[c.ToString()]; // return values of the array holding charecters to string

}

public void showTree(EvalTree t, TreeNode tn)//returns nothing but calls the show function

{

show(t.root, tn);//show called taking eval tree and tree node as parameters from above.

}

public void reset()

{

root = null;//root is empty

rpnExpr = "";//rpnExpr is an empty string

values.Clear();//clears

}

private void build(ref Node p, ref string rpn)

{

char c = rpn[rpn.Length - 1]; //array of rpn that will -1 from length each time func called

rpn = rpn.Substring(0, rpn.Length - 1);

if (p == null) p = new Node(c);// if node not there make a node

if (!((c >= 'a') && (c <= 'z')))

{ //if word more than a but less than z

build(ref p.rightPtr, ref rpn); //carry out this build function

build(ref p.leftPtr, ref rpn);//none of the reference values change!!!

}

}

private long eval(Node p) // the eval funtion with parmeter p.

{

//return Factorial(p.data);

if (!((p.data >= 'a') && (p.data <= 'z')))

{ //if data bigger than a/less than z

switch (p.data)

{

case '+': return eval(p.leftPtr) + eval(p.rightPtr);//pending the case

case '-': return eval(p.leftPtr) - eval(p.rightPtr);//you carry out one of these

case '*': return eval(p.leftPtr) * eval(p.rightPtr);// if its a charecter

case '/': return eval(p.leftPtr) / eval(p.rightPtr);

case '^': return (long)Math.Pow(Convert.ToDouble(eval(p.leftPtr)), Convert.ToDouble(eval(p.rightPtr)));

case '!': return                     <--This is where I need the code

}

else

{ // else its a number, put in a string

return values[p.data.ToString()];

}

}

Thanks for any help,

Jim

I am not even close to a C# expert so please help.