Implementation of prefix expression

go4expert's Avatar, Join Date: Aug 2004
Team Leader
please help with following code. I am trying to implement a function that uses queue to implement a prefix expression

ex:
+ 1 * 2 3
1 * 2 3 +
* 2 3 + 1
+ 1 6
7
it should stop at when 7 is the only item in the queue

my code is (can use a normal queue)
Code:
int doParse(char *input) //passing a string
{
      QueueType* queue; //ADT pointer to queue
      int i=0,num1,n,num2, interAns,count=0;
      queue = CreateQueueType(); // create queue
      while(*input){
      Enqueue(queue, *input);
      input++;
      }
while(*input=='\0'){
if (*input == '+' || *input == '-' || *input == '*' || *input == '/' )
               {
            if(isdigit(input[i+1]) && isdigit(input[i+2]))
                  { 
                        num1=input[i+1]-'0';
                        num2=input[i+2]-'0';
                        switch(input[i])
                        {
                        case '+':
                              interAns = num1 + num2;
                              break;
                        case '-':
                              interAns = num1 - num2;
                              break;
                        case '*':
                              interAns = num1 * num2;
                              break;
                        case '/':
                              interAns = num1 / num2;
                              break;
                        }
                        Enqueue(queue, interAns - '0');
                        Dequeue(queue, &n);
                        Dequeue(queue, &n);
                        Dequeue(queue, &n);
                       
                  }else{
                            Dequeue(queue, &n);
                            Enqueue(queue, *input - '0');
                        }
            }else if(isdigit(*input)){
                  Dequeue(queue, &n);
                  Enqueue(queue, *input - '0');
                  }
           
            input++;
            count++;
      }
      if(GetLength(queue)==1){
            Dequeue(queue, &n);
           
      DeleteQueueType(queue);     
      return n;
      }
}

Last edited by go4expert; 13Aug2004 at 12:16..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Please mention what type of help is needed you havent mentioned or any errors that are coming.

Just to help you
Code:
        while(*input){
        Enqueue(queue, *input);
        input++;
        }
After Enqueueing why you prefer to use input rather use queue var.
Code:
while(*input=='\0'){
Thanks
Shabbir Bhimani