//  private string instuction;
 public void handle(string instuction)
 {
     AbstractNode left = null, right = null;
     AbstractNode direction = null, action = null, distance = null;
     Stack<AbstractNode> stack = new Stack<AbstractNode>();
     string[] words = instuction.Split(' ');
     for (int i = 0; i < words.Length; i++)
     {
         if (words[i] == "and")
         {
             left = stack.Pop();
             string word1 = words[++i];
             direction = new DirectionNode(word1);
             string word2 = words[++i];
             action = new ActionNode(word2);
             string word3 = words[++i];
             distance = new DistanceNode(word3);
             right = new SentenceNode(direction, action, distance);
             stack.Push(new AndNode(left, right));
         }
         else
         {
             string word1 = words[i];
             direction = new DirectionNode(word1);
             string word2 = words[++i];
             action = new ActionNode(word2);
             string word3 = words[++i];
             distance = new DistanceNode(word3);
             left = new SentenceNode(direction, action, distance);
             stack.Push(left);
         }
     }
     this.node = stack.Pop();
 }
Ejemplo n.º 2
0
        //  private string instuction;

        public void handle(string instuction)
        {
            AbstractNode         left = null, right = null;
            AbstractNode         direction = null, action = null, distance = null;
            Stack <AbstractNode> stack = new Stack <AbstractNode>();

            string[] words = instuction.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i] == "and")
                {
                    left = stack.Pop();
                    string word1 = words[++i];
                    direction = new DirectionNode(word1);
                    string word2 = words[++i];
                    action = new ActionNode(word2);
                    string word3 = words[++i];
                    distance = new DistanceNode(word3);
                    right    = new SentenceNode(direction, action, distance);
                    stack.Push(new AndNode(left, right));
                }
                else
                {
                    string word1 = words[i];
                    direction = new DirectionNode(word1);
                    string word2 = words[++i];
                    action = new ActionNode(word2);
                    string word3 = words[++i];
                    distance = new DistanceNode(word3);
                    left     = new SentenceNode(direction, action, distance);
                    stack.Push(left);
                }
            }
            this.node = stack.Pop();
        }
Ejemplo n.º 3
0
        public bool AnalyzeCommand(string cmd)
        {
            if (string.IsNullOrEmpty(cmd))
            {
                return(false);
            }

            var cmdlist = cmd.Split(' ');

            Stack <Node> stack = new Stack <Node>();

            for (int index = 0; index < cmdlist.Count(); index++)
            {
                var str = cmdlist[index];
                if (str.Equals("and"))
                {
                    var           left    = stack.Pop();
                    DirectionNode dir     = new DirectionNode(cmdlist[++index]);
                    WayNode       way     = new WayNode(cmdlist[++index]);
                    DistanceNode  dis     = new DistanceNode(cmdlist[++index]);
                    var           right   = new CommandNode(dir, way, dis);
                    AndNode       andNode = new AndNode(left, right);
                    stack.Push(andNode);
                }
                else
                {
                    DirectionNode dir     = new DirectionNode(str);
                    WayNode       way     = new WayNode(cmdlist[++index]);
                    DistanceNode  dis     = new DistanceNode(cmdlist[++index]);
                    CommandNode   cmdnode = new CommandNode(dir, way, dis);
                    stack.Push(cmdnode);
                }
            }
            node = stack.Pop();
            return(true);
        }