Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. /**** LIST_H ****/
  2.  
  3. #ifndef LIST_H
  4. #define LIST_H
  5.  
  6. #include "tree.h"
  7.  
  8. typedef struct List
  9. {
  10.     ptree info;
  11.    
  12.     struct List *next;
  13.    
  14. }list;
  15.  
  16. typedef list *plist;
  17.  
  18. int contExpr(plist, int);
  19.  
  20. #endif  /* LIST_H */
  21.  
  22. *****************************
  23.  
  24. /**** LIST_C ****/
  25.  
  26. #include "list.h"
  27.  
  28. int contExpr(plist p, int v)
  29. {
  30.     int cont = 0;
  31.    
  32.     while(p != NULL)
  33.     {
  34.         if(resolve(p->info) > v)
  35.             cont++;
  36.     }
  37.    
  38.     return cont;
  39. }
  40.  
  41. *****************************
  42.  
  43. /**** TREE_H ****/
  44.  
  45. #ifndef TREE_H
  46. #define TREE_H
  47.  
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50.  
  51. typedef struct node
  52. {
  53.     int info;
  54.    
  55.     struct node *dx, *sx;
  56. }tree;
  57.  
  58. typedef tree *ptree;
  59.  
  60. int resolve(ptree);
  61.  
  62. #endif  /* TREE_H */
  63.  
  64. *****************************
  65.  
  66. /**** TREE_C ****/
  67.  
  68. #include "tree.h"
  69.  
  70. int resolve(ptree t)
  71. {
  72.     if(!t->dx && !t->sx)
  73.         return t->info;
  74.    
  75.     int val_sx, val_dx;
  76.    
  77.     val_sx = resolve(t->sx);
  78.     val_dx = resolve(t->dx);
  79.    
  80.     switch(t->info)
  81.     {
  82.         case '+':
  83.             return val_sx + val_dx;
  84.             break;
  85.         case '*':
  86.             return val_sx * val_dx;
  87.             break;
  88.         case '-':
  89.             return val_sx - val_dx;
  90.             break;
  91.         default:
  92.             return val_sx / val_dx;
  93.     }
  94.    
  95.     return 0;
  96. }
  97.  
  98. *****************************
  99.  
  100. /**** LOGICTREE_H ****/
  101.  
  102. #ifndef LOGICTREE_H
  103. #define LOGICTREE_H
  104.  
  105. #include "tree.h"
  106. #include "list.h"
  107.  
  108. typedef struct log_tree
  109. {
  110.     ptree info;
  111.    
  112.     struct log_tree *sx, *dx;
  113. }logictree;
  114.  
  115. typedef logictree *plogic;
  116.  
  117. void inserisci(ptree, plogic);
  118.  
  119. plogic createP_ABR(plist);
  120.  
  121. #endif  /* LOGICTREE_H */
  122.  
  123. *****************************
  124.  
  125. /**** LOGICTREE_C ****/
  126.  
  127. #include "logictree.h"
  128.  
  129. void inserisci(ptree p, plogic l)
  130. {
  131.     if(!l)
  132.     {
  133.         l = malloc(sizeof(logictree));
  134.        
  135.         l->info = p;
  136.         l->dx = NULL;
  137.         l->sx = NULL;
  138.     }
  139.     else
  140.     {
  141.         if(resolve(p) > resolve(l->info))
  142.             inserisci(p, l->dx);
  143.         else
  144.             inserisci(p, l->sx);
  145.     }
  146. }
  147.  
  148. plogic createP_ABR(plist p)
  149. {
  150.     plogic l = malloc(sizeof(logictree));
  151.     l = NULL;
  152.        
  153.     while(p != NULL)
  154.         inserisci(p->info, l);
  155.    
  156.     return l;
  157. }
  158.  
  159. *****************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement