Advertisement
Guest User

Revised_STYLEFIX_rdeshpan

a guest
Mar 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. /*
  2.  * Interface for dictionaries
  3.  *
  4.  * 15-122 Principles of Imperative Computation
  5.  */
  6.  
  7. #use <string>
  8. #use <conio>
  9.  
  10. /* Interface to dictionaries (leave this alone) */
  11.  
  12. // typedef ______* dict_t;
  13. typedef struct dict_header* dict_t;
  14.  
  15. // Data structure invariant to ensure safety
  16. bool is_dict(dict_t D);
  17. // Returns NULL if the input array has the same operator twice
  18.  
  19. dict_t dict_new(string[] A, int n)
  20.   /*@requires \length(A) == n; @*/
  21.   /*@requires is_infix_array(A, n); @*/
  22.   /*@ensures is_dict(\result);@*/;
  23. // Returns -1 if no definition is found
  24.  
  25. int dict_lookup(dict_t D, string oper)
  26.   /*@requires is_dict(D); @*/
  27.   /*@ensures \result >= -1; @*/ ;
  28. //implementations for dictionaries
  29.  
  30. typedef struct dict_header dict;
  31.  
  32. struct dict_header {
  33.     int[] precOp;
  34.     int len;
  35. };
  36.  
  37. bool is_dict(dict_t D) {
  38.     //@assert(\length(D->precOP)==D->len);
  39.     return D != NULL && D->len > 0;
  40. }
  41.  
  42. dict_t dict_new(string[] A, int n)
  43. /*@ensures ((\result == NULL)
  44.            || (is_dict(\result))
  45.                && (\length(\result->precOp)==\result->len));@*/
  46. {
  47.     int[] opPrec = parse_ints("-1 -1 -1 -1 -1", 10);
  48.     string[] operations = parse_tokens("** * / + -");
  49.     for(int h=0; h<5; h++) {
  50.         for(int i=0; i<n; i++) {
  51.             string[] opArr = parse_tokens(A[i]);
  52.             for(int j=0; j<num_tokens(A[i]); j++) {
  53.                 if(string_equal(operations[h],opArr[j])) {
  54.                     if(opPrec[h]!=-1) return NULL;
  55.                     opPrec[h]=i;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     dict* d = alloc(dict);
  61.     d->precOp=opPrec;
  62.     d->len = n;
  63.     return d;
  64. }
  65.  
  66. int dict_lookup(dict_t D, string oper)
  67. //@requires is_dict(D);
  68. //@ensures is_dict(D);
  69. {
  70.     string[] ops = parse_tokens("** * / + -");
  71.     for(int i=0; i<5; i++) {
  72.         if(string_equal(ops[i],oper)) return D->precOp[i];
  73.     }
  74.     return -1;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement