Hi all...
How can I control any postfix expression is valid or not???
I controlled it using a stack(the code is below) and it worked but I 'm using the stack again after clearing it for evaluation the postfix expression to the infix expression.I'm using the stack twice and I didn't find it efficient.
Is there a more efficient way to control wheter the postfix expression is valid???

for(i = 0; postfix[i] != '\0'; i++)
    {
                 if(postfix[i] != '+' && postfix[i] != '-' && postfix[i] != '/' && postfix[i] != '*')
                 {
                               Push(postfix[i],&myStack);/*If it is not an operator, push it to the stack...*/
                 }
                 
                 else        
                 {                           
                             if(StackSize(&myStack) >= 2)
                                 Pop(&num2,&myStack);
                             
                             else
                             {
                                 printf("Invalid postfix expression\n");
                                 ClearStack(&myStack);
                                 break;
                             }
                 }
    }

>I'm using the stack twice and I didn't find it efficient.
Are you just eyeballing it and thinking "that can't be efficient", or did you profile multiple executions and get hard numbers that proved it's consistently slower than your allowable limit?

>I'm using the stack twice and I didn't find it efficient.
Are you just eyeballing it and thinking "that can't be efficient", or did you profile multiple executions and get hard numbers that proved it's consistently slower than your allowable limit?

Honestly I didn't profiled it multiple executions.That's a simple assignmet which I've got and it wasn't indicated wheter we have to control the postfix expression is valid.Probably the teacher won't look at the program's execution time so it is not a matter for the assignment but I guessed how any postfix expression is controlled wheter is valid.

I thought if we input a very long postfix expression it won't be efficient.(It can be more efficient if I use an array implementation of the stack but I'm using the linked list implemantation of it.)Can it controlled without using a stack twice??? Is there a standart postfix template so that I can compare my postfix expression to it but if it is I am still not sure this is the more efficient way...

>I thought if we input a very long postfix expression it won't be efficient.
Efficiency is relative. Don't forget that computers are pretty darn zippy, and something that doesn't seem efficient could very well be more than fast enough for your purposes. Programmers are notoriously bad at guessing where inefficiencies lie.

>Can it controlled without using a stack twice???
Your terminology is confusing. What do you mean by "controlled"? Presumably you're trying to evaluate a postfix expression with error handling. You didn't provide much code, so I'm a bit confused as to why you can't validate the expression as you evaluate it. That's one of the nice things about postfix, you immediately know when the next operation won't work and can fail intelligently.

Actually I'm evaluating it like using low level codes.
For example : A B C * + D E - / must be evaluated like below where LD means loading a value to a register, AD means addition,SB means subtraction,ML means multiplication,DV means divide and ST means store.

LD B
ML C
ST TEMP1
LD A
AD TEMP1
ST TEMP2
LD D
SB E
ST TEMP3
LD TEMP2
DV TEMP3
ST TEMP4

If I give an error for any input like below it won't be conveniet so I thought that it will be more convenient if I check wheter it is valid before trying evaluate it...

LD B
ML C
ST TEMP1
LD A
AD TEMP1
Error the postfix expression is invalid

Finally I give the whole source code(I'm entering the postfix expression using lowercase letters and operators) :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "STACK.H"

int main()
{
    Stack myStack;
    char postfix[20],op[3],num1,num2;
    int i,j = 0;


    CreateStack(&myStack);
    
    printf("Enter the postfix expression\n");
    scanf("%s",postfix);
    
    for(i = 0; postfix[i] != '\0'; i++)
    {
                 if(postfix[i] != '+' && postfix[i] != '-' && postfix[i] != '/' && postfix[i] != '*')
                 {
                               Push(postfix[i],&myStack);
                 }
                 
                 else        
                 {                           
                             if(StackSize(&myStack) >= 2)
                                 Pop(&num2,&myStack);
                             
                             else
                             {
                                 printf("Invalid postfix expression\n");
                                 ClearStack(&myStack);
                                 return 0;
                             }
                 }
    }
    
    ClearStack(&myStack);
    
    for(i = 0; postfix[i] != '\0'; i++)
    {
                 if(postfix[i] != '+' && postfix[i] != '-' && postfix[i] != '/' && postfix[i] != '*')
                 {
                               Push(postfix[i],&myStack);
                 }
                 
                 else if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '/' || postfix[i] == '*')
                 {
                               if(postfix[i] == '+') 
                                    strcpy(op,"AD");
                               else if(postfix[i] == '-')
                                    strcpy(op,"SB");
                               else if(postfix[i] == '*')
                                    strcpy(op,"ML");
                               else if(postfix[i] == '/')
                                    strcpy(op,"DV");
                           
                               Pop(&num2,&myStack);
                               Pop(&num1,&myStack);
                               
                               if(num1 < 97)
                                       printf("LD TEMP%d\n",num1);
                               else
                                       printf("LD %c\n",num1);
                               
                               if(num2 < 97)
                               {
                                       printf("%s TEMP%d\n",op,j);
                                       j++;
                                       Push(j,&myStack);
                                       printf("ST TEMP%d\n",j);
                               }
                               
                               else
                               {
                                   printf("%s %c\n",op,num2);
                                   j++;
                                   Push(j,&myStack);
                                   printf("ST TEMP%d\n",j);
                               }
                 }
    }
    
    return 0;
}

>so I thought that it will be more convenient if I
>check wheter it is valid before trying evaluate it...
Well, then you have to decide which you want more: the efficiency of validating during evaluation, or the convenience of validating before evaluation. If you want to validate separately, you're going to process the expression at least two times. There's no simple way of getting around that.

>so I thought that it will be more convenient if I
>check wheter it is valid before trying evaluate it...
Well, then you have to decide which you want more: the efficiency of validating during evaluation, or the convenience of validating before evaluation. If you want to validate separately, you're going to process the expression at least two times. There's no simple way of getting around that.

I think I will choose the convenience of validating before evaluation.If I were working with numbers and if the output doesn't need to be in the form of which was indicated my previous post(i.e if it has to be a number) I would choose the efficiency of validating during evaluation...

Thanks for your help...
If there are any suggestions I'll appreciate it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.