1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

implement a Implementation of prefix expression

Discussion in 'C' started by go4expert, Aug 13, 2004.

  1. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    7
    Trophy Points:
    0
    Implementation of prefix expression

    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: Aug 13, 2004
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Re: Implementation of prefix expression

    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
     

Share This Page