Hi, actually I'd already read this article. I really focused on it now and I was able to implement the algorithm.
Here's my final implementation of a PrefixParser class:
public class PrefixParser
{
public string Formula { get; set; }
public PrefixParser(string formula)
{
Formula = formula;
ASTree tree = new ASTree();
}
public ASTree Parse()
{
Stack stack = new Stack();
string[] tokens = Formula.Split(' ');
Array.Reverse(tokens);
for (int i = 0; i < tokens.Length; i++)
{
string token = tokens[i];
if (!IsOperator(token))
{
ASTreeNode newNode = new ASTreeNode();
newNode.Root = token;
newNode.SetTokenCode();
newNode.LeftNode = null;
newNode.RightNode = null;
stack.Push(newNode);
}
else
{
ASTreeNode newNode = new ASTreeNode();
newNode.Root = token;
newNode.SetTokenCode();
ASTreeNode opNode1 = (ASTreeNode)stack.Pop();
ASTreeNode opNode2 = (ASTreeNode)stack.Pop();
newNode.LeftNode = opNode1;
newNode.RightNode = opNode2;
stack.Push(newNode);
}
}
ASTree tree = new ASTree();
tree.RootNode = (ASTreeNode)stack.Pop();
return tree;
}
private bool IsOperator(string token)
{
return (token == "+" || token == "-" || token == "/" || token == "*");
}
}
Main Topics
Browse All Topics





by: rambovnPosted on 2008-09-16 at 02:52:31ID: 22486536
pls see this: e.net/foru ms/showtop ic37428.ht m
http://www.dreamincod