Page 75 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 75

Advanced Data Structure and Algorithms




                    Notes
                                         Example: To evaluate a Postfi x Expression:
                                          ABC**/DE *+AC*-
                                   The following steps will be executed:



                                        Top     C     **                        E
                                       I. op.   B         I. op.  B ** C        D        *      E*D
                                                                                                        +
                                      II. op.   A        II. op.   A          A/(B**C)       A/(B**C)


                                                C
                                                            +
                                                A                       A + C
                                                                                       = A/(B**C) + (D*E) – (A*C)
                                           A/(B**C) + (D + E)       A/(B**C) + ( D + E)





                                     Lab Exercise   Here is a program that implements the above idea into a program.
                                     #include <stdio.h>
                                     #include <stdlib.h>
                                     #include <math.h>
                                     #define MAXC  80
                                     double eva1(charl[]);
                                     double pop(struct stack *);
                                     void push(struct stack *, double);
                                     int empty(struct stack *);
                                     int isdigit(char);
                                     double oper(intn double, double);
                                     void main()
                                     {
                                     char expr[MAXC];
                                     int position = 0;
                                     whi1e((expr[position++] = getchar()) != ‘\’) ;
                                     expr[--position] = ‘\0’;
                                     printf(“%s%s”,  “the original postfix expression is”,  expr);
                                     printf(“\n%f”, eval(expr));
                                     }
                                     struct stack
                                     {
                                     int top;
                                     double data[MAXC];
                                     }




          70                               LOVELY PROFESSIONAL UNIVERSITY
   70   71   72   73   74   75   76   77   78   79   80