Advertisement
BlankOD

Untitled

Mar 19th, 2022
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <stdio.h>  /* printf */
  2. #include <stdlib.h> /* malloc, free */
  3. #include <assert.h> /* assert */
  4. #include <string.h>
  5. #include "scanner.h"
  6. #include "recognizeExp.h"
  7. #include "evalExp.h"
  8. #include "infixExp.h"
  9.  
  10. int isOperatorAddSub(char c) {
  11.   return ( c == '+' || c == '-' );
  12. }
  13. //Function that checks if an operator seperates 'expressions'.
  14. int valueOperatorExp(List *lp, char *cp) {
  15.  
  16.   if ( *lp != NULL && (*lp)->tt == Symbol && isOperatorAddSub(((*lp)->t).symbol) ) {
  17.     *cp = ((*lp)->t).symbol;
  18.     *lp = (*lp)->next;
  19.     return 1;
  20.   }
  21.  
  22.   return 0;
  23. }
  24.  
  25. int isOperatorMulDiv(char c) {
  26.   return ( c == '*' || c == '/' );
  27. }
  28. //Function that checks if an operator seperates 'terms'
  29. int valueOperatorTerm(List *lp, char *cp) {
  30.  
  31.   if (*lp != NULL && (*lp)->tt == Symbol && isOperatorMulDiv(((*lp)->t).symbol) ) {
  32.     *cp = ((*lp)->t).symbol;
  33.     *lp = (*lp)->next;
  34.     return 1;
  35.   }
  36.  
  37.   return 0;
  38. }
  39.  
  40. //Main function that converts the infix expression 'List'
  41. //into an expression 'Tree'
  42. ExpTree treeExpression(List *lp) {
  43.  
  44.   char c;
  45.   Token t;
  46.   ExpTree tr = NULL;
  47.  
  48.   //Base case to check if the list is empty (meaning we've reached the end)
  49.   if (*lp == NULL) {
  50.     return NULL;
  51.   }
  52.  
  53.   //Create a tree for the left term.
  54.   tr = treeTerm(lp);
  55.  
  56.   //If a + or - is found, the right term is evaluated and
  57.   //given its own treenode
  58.   while (valueOperatorExp(lp, &c)) {
  59.     t.symbol = c;
  60.     tr = newExpTreeNode(Symbol, t, tr, NULL);
  61.     tr->right = treeTerm(lp);
  62.   }
  63.  
  64.   return tr;
  65. }
  66.  
  67. ExpTree treeTerm(List *lp) {
  68.  
  69.   char c;
  70.   Token t;
  71.   ExpTree tr = NULL;
  72.  
  73.   if(*lp == NULL) {
  74.     return NULL;
  75.   }
  76.  
  77.   //identically to expressionToTree, a tree is made for the first factor
  78.   tr = treeFactor(lp);
  79.  
  80.   while(valueOperatorTerm(lp, &c)) {
  81.     t.symbol = c;
  82.     tr = newExpTreeNode(Symbol, t, tr, NULL);
  83.     tr->right = treeFactor(lp);
  84.   }
  85.  
  86.   return tr;
  87. }
  88.  
  89. ExpTree treeFactor(List *lp) {
  90.  
  91.   double w;
  92.   char *s;
  93.   Token t;
  94.   ExpTree tr = NULL;
  95.  
  96.   if (*lp == NULL) {
  97.     return NULL;
  98.   }
  99.  
  100.   //If the factor is a number, a new tree is created
  101.   //with this number as the root.
  102.   if (valueNumber(lp, &w)) {
  103.     t.number = (int)w;
  104.     tr = newExpTreeNode(Number, t, NULL, NULL);
  105.     return tr;
  106.   }
  107.   //If the factor is an identifier, the same happens
  108.   //but instead with an identifier as the root.
  109.   else if (valueIdentifier(lp, &s)) {
  110.     t.identifier = s;
  111.     tr = newExpTreeNode(Identifier, t, NULL, NULL);
  112.     return tr;
  113.   }
  114.  
  115.   //Neither a number nor identifier results in a new expression
  116.   //(possibly with brackets).
  117.   acceptCharacter(lp, '(');
  118.   tr = treeExpression(lp);
  119.   acceptCharacter(lp, ')');
  120.  
  121.   return tr;
  122. }
  123.  
  124. void infixSolver() {
  125.  
  126.   char *ar;
  127.   List tl, tl1, tl2;
  128.   ExpTree t = NULL;
  129.  
  130.   printf("give an expression: ");
  131.   ar = readInput();
  132.  
  133.   while (ar[0] != '!') {
  134.    
  135.     tl = tokenList(ar);
  136.     printList(tl);
  137.     tl1 = tl;
  138.     tl2 = tl;
  139.  
  140.     //Check if the expression is valid, if not skip the solver
  141.     //and request a new expression.
  142.     if (acceptExpression(&tl2) && tl2 == NULL) {
  143.       t = treeExpression(&tl1);
  144.       printf("in infix notation: ");
  145.       printExpTreeInfix(t);
  146.       printf("\n");
  147.       if (isNumerical(t)) {
  148.         printf("the value is %g\n", valueExpTree(t));
  149.       } else {
  150.          printf("this is not a numerical expression\n");
  151.       }
  152.     } else {
  153.       printf("this is not an expression\n");
  154.     }
  155.    
  156.     freeExpTree(t);
  157.     t = NULL;
  158.     freeTokenList(tl);
  159.     free(ar);
  160.     printf("\ngive an expression: ");
  161.     ar = readInput();
  162.   }
  163.  
  164.   free(ar);
  165.   printf("good bye\n");
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement