Guest User

Untitled

a guest
Mar 22nd, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.02 KB | None | 0 0
  1. #include <stdio.h>  /* printf */
  2. #include <stdlib.h> /* malloc, free*/
  3. #include <assert.h> /* assert */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include "scanner.h"
  8. #include "recognizeExp.h"
  9. #include "evalExp.h"
  10. #include "prefixExp.h"
  11. #include "infixExp.h"                
  12.  
  13. struct infix
  14. {
  15.     char target[100] ;
  16.     char stack[100] ;
  17.     char *s ,*t;
  18.     int top, l ;
  19. } ;
  20.  
  21.  
  22. void initinfix ( struct infix * ) ;
  23. void setexpr ( struct infix *, char * ) ;
  24. void push ( struct infix *, char ) ;
  25. char pop ( struct infix * ) ;
  26. void convert ( struct infix * ) ;
  27. int priority ( char c ) ;
  28. List getPrefixlist ( struct infix ) ;
  29.  
  30.  
  31. // simple function to reverse a string since linux has problems with strrev
  32. void reverse(char *str1)  
  33. {  
  34.     // declare variable  
  35.     int i, len, temp;  
  36.     len = strlen(str1); // use strlen() to get the length of str string  
  37.      
  38.     // use for loop to iterate the string  
  39.     for (i = 0; i < len/2; i++)  
  40.     {  
  41.         // temp variable use to temporary hold the string  
  42.         temp = str1[i];  
  43.         str1[i] = str1[len - i - 1];  
  44.         str1[len - i - 1] = temp;  
  45.     }  
  46. }  
  47.  
  48.  
  49. /* The function treeExpression tries to build a tree from the tokens in the token list
  50.  * (its first argument) and makes its second argument point to the tree.
  51.  * The return value indicates whether the action is successful.
  52.  * Observe that we use ordinary recursion, not mutual recursion.
  53.  */
  54.  
  55. int TreePrefixEx(List *lp, ExpTree *tp) {
  56.   double w;
  57.   char *s;
  58.   char c;
  59.   Token t;
  60.   ExpTree tL, tR;
  61.   if ( valueNumber(lp,&w) ) {
  62.     t.number = (int)w;
  63.     *tp = newExpTreeNode(Number, t, NULL, NULL);
  64.     return 1;
  65.   }
  66.   if ( valueIdentifier(lp,&s) ) {
  67.     t.identifier = s;
  68.     *tp = newExpTreeNode(Identifier, t, NULL, NULL);
  69.     return 1;
  70.   }
  71.   if ( valueOperator(lp,&c) && TreePrefixEx(lp,&tL) ) {
  72.     if ( TreePrefixEx(lp,&tR) ) {
  73.       t.symbol = c;
  74.       *tp = newExpTreeNode(Symbol, t, tL, tR);
  75.       return 1;
  76.     } else { /* withuot 'else' the program works fine, but there is a memory leak */
  77.       freeExpTree(tL);
  78.       return 0;
  79.     }
  80.   }
  81.   return 0;
  82. }
  83. void initinfix ( struct infix *pq )
  84. {
  85.     pq -> top = -1 ;
  86.     strcpy ( pq -> target, "" ) ;
  87.     strcpy ( pq -> stack, "" ) ;
  88.     pq -> l = 0 ;
  89. }
  90. /* reverses the given expression */
  91. void setexpr ( struct infix *pq, char *str )
  92. {
  93.     pq -> s = str ;
  94.     reverse ( pq -> s ) ;
  95.     pq -> l = strlen ( pq -> s ) ;
  96.     *( pq -> target + pq -> l ) = '\0' ;
  97.     pq -> t = pq -> target + ( pq -> l - 1 ) ;
  98. }
  99. /* adds operator to the stack */
  100. void push ( struct infix *pq, char c )
  101. {
  102.     if ( pq -> top == 10 - 1 )
  103.         printf ( "\nStack is full.\n" ) ;
  104.     else
  105.     {
  106.         pq -> top++ ;
  107.         pq -> stack[pq -> top] = c ;
  108.     }
  109. }
  110. /* pops an operator from the stack */
  111. char pop ( struct infix *pq )
  112. {
  113.     if ( pq -> top == -1 )
  114.     {
  115.         printf ( "Stack is empty\n" ) ;
  116.         return -1 ;
  117.     }
  118.     else
  119.     {
  120.         char item = pq -> stack[pq -> top] ;
  121.         pq -> top-- ;
  122.         return item ;
  123.     }
  124. }
  125. /* converts the infix expr. to prefix form */
  126. void convert ( struct infix *pq )
  127. {
  128.     char opr ;
  129.     while ( *( pq -> s ) )
  130.     {
  131.         if ( *( pq -> s ) == ' ' || *( pq -> s ) == '\t' )
  132.         {
  133.             pq -> s++ ;
  134.             continue ;
  135.         }
  136.         if ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
  137.         {
  138.             while ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
  139.             {
  140.                 *( pq -> t ) = *( pq -> s ) ;
  141.                 pq -> s++ ;
  142.                 pq -> t-- ;
  143.             }
  144.         }
  145.         if ( *( pq -> s ) == ')' )
  146.         {
  147.             push ( pq, *( pq -> s ) ) ;
  148.             pq -> s++ ;
  149.         }
  150.         if ( *( pq -> s ) == '*' || *( pq -> s ) == '+' || *( pq -> s ) == '/' || *( pq -> s ) == '%' || *( pq -> s ) == '-' || *( pq -> s ) == '$' )
  151.         {
  152.             if ( pq -> top != -1 )
  153.             {
  154.                 opr = pop ( pq ) ;
  155.                 while ( priority ( opr ) > priority ( *( pq -> s ) ) )
  156.                 {
  157.                     *( pq -> t ) = opr ;
  158.                     pq -> t-- ;
  159.                     opr = pop ( pq ) ;
  160.                 }
  161.                 push ( pq, opr ) ;
  162.                 push ( pq, *( pq -> s ) ) ;
  163.             }
  164.             else
  165.                 push ( pq, *( pq -> s ) ) ;
  166.                 pq -> s++ ;
  167.         }
  168.         if ( *( pq -> s ) == '(' )
  169.         {
  170.             opr = pop ( pq ) ;
  171.             while ( opr != ')' )
  172.             {
  173.                 *( pq -> t ) = opr ;
  174.                 pq -> t-- ;
  175.                 opr = pop ( pq ) ;
  176.             }
  177.             pq -> s++ ;
  178.         }
  179.     }
  180.     while ( pq -> top != -1 )
  181.     {
  182.         opr = pop ( pq ) ;
  183.         *( pq -> t ) = opr ;
  184.         pq -> t-- ;
  185.     }
  186.     pq -> t++ ;
  187. }
  188. /* returns the priotity of the operator */
  189. int priority ( char c )
  190. {
  191.     if ( c == '$' )
  192.         return 3 ;
  193.     if ( c == '*' || c == '/' || c == '%' )
  194.         return 2 ;
  195.     else
  196.     {
  197.         if ( c == '+' || c == '-' )
  198.             return 1 ;
  199.         else
  200.             return 0 ;
  201.     }
  202. }
  203. /* displays the prefix form of given expr. */
  204. List getPrefixlist ( struct infix pq )
  205. {
  206.     char* ar = malloc(sizeof(*ar));
  207.     int i=0;
  208.     List tl;
  209.     while ( *( pq.t ) )
  210.     {
  211.         ar[i]=*( pq.t );
  212.         pq.t++ ;
  213.     }
  214.     tl= tokenList(ar);
  215.     free(ar);
  216.     return tl; 
  217. }
  218. // the main conversion function , calling it should return the expression in prefix form
  219. List Conv( )
  220. {
  221.     struct infix q ;
  222.     char *ar ;
  223.     List tl;
  224.     ar = readInput();
  225.     initinfix ( &q ) ;
  226.     setexpr ( &q, ar ) ;
  227.     convert ( &q ) ;
  228.     tl = getPrefixlist ( q ) ;
  229.     return tl;
  230. }
  231. /* the function prefExpressionExpTrees performs a dialogue with the user and tries
  232.  * to recognize the input as a prefix expression. When it is a numerical prefix
  233.  * expression, its value is computed and printed.
  234.  */
  235.  
  236. void prefExpTr() {
  237.   char *ar;
  238.   List tl, tl1;
  239.   ExpTree t = NULL;
  240.   printf("give an expression: ");
  241.   ar = readInput();
  242.   while (ar[0] != '!') {
  243.     tl = tokenList(ar);
  244.     printList(tl);
  245.     tl=Conv();
  246.     tl1 = tl;
  247.     if ( TreePrefixEx(&tl1,&t) && tl1 == NULL ) {
  248.          /* there should be no tokens left */
  249.       printf("in infix notation: ");
  250.       printExpTreeInfix(t);
  251.       printf("\n");
  252.       if ( isNumerical(t) ) {
  253.         printf("the value is %g\n",valueExpTree(t));
  254.       } else {
  255.         printf("this is not a numerical expression\n");
  256.       }
  257.     } else {
  258.       printf("this is not an expression\n");
  259.     }
  260.     freeExpTree(t);
  261.     t = NULL;
  262.     freeTokenList(tl);
  263.     free(ar);
  264.     printf("\ngive an expression: ");
  265.     ar = readInput();
  266.   }
  267.   free(ar);
  268.   printf("good bye\n");
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment