rrz0

printf() & fprintf()

Dec 21st, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_STRING_SIZE 100
  6. #define HASHSIZE 101  /* comment here */
  7. #define ALLOCSIZE 1000 /*size of available space*/
  8. #define N 1000 // max number of lines to be read
  9. #define VALLEN 100 //max number of chars for description
  10. #define MAXC 1024
  11.  
  12. static char allocbuf[ALLOCSIZE]; /* storage for alloc*/
  13. static char *allocp = allocbuf; /* next free position*/
  14. static struct map *hashtab[HASHSIZE]; /* pointer table */
  15. unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
  16. char *strdup(char *);
  17.  
  18. char *alloc(int n) { /* return a pointer to n characters*/
  19.     if (allocbuf + ALLOCSIZE - allocp >= n) { /*it fits*/
  20.         allocp += n;
  21.         return allocp - n; /*old p*/
  22.     } else /*not enough room*/
  23.         return 0;
  24. }
  25.  
  26. typedef struct map { /* creating a map structure */
  27.     struct map *next;
  28.     char *value;
  29.     char *KEY;
  30. } map;
  31.  
  32. typedef struct node {
  33.     int KEY;
  34.     char *value;
  35.     int node_count;
  36.     struct node *left, *right;
  37.     int unique_count;
  38. } node;
  39.  
  40. // A utility function to create a new BST node
  41.  
  42. node *newNode(int KEY, char *value) {
  43.     //Allocate a new overall structure
  44.     node *temp = (node *) malloc(sizeof (node));
  45.  
  46.     //Copy the integer key
  47.     temp->KEY = KEY;
  48.     temp->value = value;
  49.  
  50.     temp->left = temp->right = NULL;
  51.     temp->node_count = 1;
  52.     return temp;
  53. }
  54.  
  55. // A utility function to do inorder traversal of BST
  56.  
  57. void inorder(node *root, int *pin_count) {
  58.     if (root != NULL) {
  59.         inorder(root->left, pin_count);
  60.         //printf("%d(%d)\n", root->KEY, root->node_count);
  61.         inorder(root->right, pin_count);
  62.     }
  63. }
  64.  
  65. /* A utility function to find deepest keys of BST */
  66.  
  67. /* This is done by extracting the key with the lowest count,
  68. since count member of node struct gets incremented each time it is read. */
  69. void unique_key(node *root, int *p_unique_count, int p_unique_key_arr[], char *p_value_arr[]) {
  70.     int count = 0;
  71.     char *p;
  72.  
  73.     if (root != NULL) {
  74.         unique_key(root->left, p_unique_count, p_unique_key_arr, p_value_arr);
  75.         if (root->node_count == 1) {
  76.  
  77.             if ((p = alloc(strlen(root[count].value) + 1)) == NULL) {
  78.                 return;
  79.             } else {
  80.                 root[count].unique_count = *p_unique_count;
  81.                 p_unique_key_arr[*p_unique_count] = root[count].KEY;
  82.  
  83.                 strcpy(p, root[count].value);
  84.                 p_value_arr[*p_unique_count] = p;
  85.                 //printf("%s\n",p_value_arr[*p_unique_count][*p_unique_count]);
  86.  
  87.                 //printf("%d(%d) -> %s %d\n", root->KEY, root->node_count, root->value, root->unique_count);
  88.                 (*p_unique_count)++;
  89.                 count++;
  90.             }
  91.         }
  92.         unique_key(root->right, p_unique_count, p_unique_key_arr, p_value_arr);
  93.     }
  94. }
  95.  
  96. /* A utility function to insert a new node with given key in BST */
  97. node* insert_node(node* node, int key, char *value) {
  98.     /* If the tree is empty, return a new node */
  99.     if (node == NULL)
  100.         return newNode(key, value);
  101.  
  102.     // If key already exists in BST, icnrement count and return
  103.     if (key == node->KEY) {
  104.         (node->node_count)++;
  105.         //  return node;
  106.     }
  107.  
  108.     /* Otherwise, recur down the tree */
  109.     if (key < node->KEY)
  110.         node->left = insert_node(node->left, key, value);
  111.     else
  112.         node->right = insert_node(node->right, key, value);
  113.  
  114.     /* return the (unchanged) node pointer */
  115.     return node;
  116. }
  117.  
  118. /* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
  119. map *lookup(char *s) {
  120.     map *np;
  121.  
  122.     for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
  123.         if (strcmp(s, np->KEY) == 0) {
  124.             return np;
  125.         }
  126.     }
  127.     return NULL;
  128. }
  129.  
  130. /* insert function takes a pointer to a char - KEY, value and returns a pointer to map structure */
  131. map *insert_map(char* KEY, char* value) {
  132.     /* install: put (name, defn) in */
  133.     /* Install uses lookup to determine whether the KEY being installed
  134.        is already present. Proceeds to create a new entry or update*/
  135.     map *np;
  136.     unsigned hashval;
  137.  
  138.     if ((np = lookup(KEY)) == NULL) {
  139.         np = (map *) malloc(sizeof (*np));
  140.         if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
  141.             return NULL;
  142.         }
  143.         hashval = hash(KEY);
  144.         np->next = hashtab[hashval];
  145.         hashtab[hashval] = np;
  146.     } else {
  147.         free((void *) np->value);
  148.     }
  149.     if ((np->value = strdup(value)) == NULL) { //map has no field value
  150.         return NULL;
  151.     }
  152.  
  153.     return np;
  154. }
  155.  
  156. /* uninstall function removes a key and calendar from table maintained by lookup and install */
  157. map *remove_map(char *KEY) {
  158.     map *found;
  159.  
  160.     found = lookup(KEY);
  161.  
  162.     if (found == NULL) /* not found and nothing to do */ {
  163.         printf("%s - Nothing to delete.\n", KEY);
  164.         return NULL;
  165.     } else {
  166.         if (found->next != NULL) {
  167.             found->next = found->next->next;
  168.             found = found->next;
  169.         } else {
  170.             hashtab[hash(KEY)] = NULL;
  171.             free((void *) found);
  172.         }
  173.     }
  174.     return found;
  175. }
  176.  
  177. /* simple implementation of strdup*/
  178. char *strdup(char *s) {
  179.  
  180.     char *d = (char *)malloc(strlen(s) + 1); // Space for length plus null
  181.     if (d != NULL) {
  182.         strcpy(d, s); // Copy the characters
  183.     }
  184.     return d; // Return the new string
  185. }
  186.  
  187. unsigned hash(char *s) {
  188.     /* hashing function: form hash value for string s */
  189.     /* used by both lookup and install*/
  190.     unsigned hashval;
  191.  
  192.     for (hashval = 0; *s != '\0'; s++) {
  193.         hashval = *s + 31 * hashval;
  194.     }
  195.     return hashval % HASHSIZE;
  196. }
  197.  
  198. // Driver Program to test above functions
  199.  
  200. int main() {
  201.     FILE *f_cal;
  202.     FILE *f_cal2;
  203.     FILE *f_op;
  204.     int total_unique = 0;
  205.     int in_count, count_create = 0;
  206.     int count_delete = 0;
  207.     int unique_key_arr[100]; // Make dynamic
  208.     char *value_arr[100];
  209.     char *VAL_arr_create[100]; // an array of pointers [unique][chars]
  210.     char *VAL_arr_delete[100];
  211.     char *KEY_arr_create[100];
  212.     char *KEY_arr_delete[100];
  213.  
  214.     f_cal = fopen("calendar.txt", "r+");
  215.     f_op = fopen("operations.txt", "r+");
  216.  
  217.     char buf[MAXC];
  218.     char *line[1024];
  219.     char *p_line;
  220.     char *ptr;
  221.     char *in_val_arr[100];
  222.     int in_key_arr[100];
  223.     char *in_val_op[100];
  224.     int in_key_op[100];
  225.     long key_arr_op;
  226.     int count_cal = 0;
  227.     int count_op = 0;
  228.     char delimiter[] = " ";
  229.     long key_arr;
  230.     node *root = NULL;
  231.  
  232.     /* Both input files must wned with a new line*/
  233.     if (f_cal) {
  234.         printf("Processing file...\n");
  235.         while (fgets(buf, MAXC, f_cal)) {
  236.             p_line = malloc(strlen(buf) + 1); // malloced with size of buffer.
  237.             //p_line = malloc(sizeof (char) + 1); // malloced with size of buffer.
  238.             char *in_val;
  239.             char *in_key;
  240.  
  241.             strcpy(p_line, buf); //used to create a copy of input buffer
  242.             line[count_cal] = p_line;
  243.  
  244.             /* separating the line based on the first space. The words after
  245.              * the delimeter will be copied into in_val */
  246.             char *copy = strchr(p_line, ' ');
  247.             if (copy) {
  248.                 if ((in_val = alloc(strlen(line[count_cal]) + 1)) == NULL) {
  249.                     return -1;
  250.                 } else {
  251.                     strcpy(in_val, copy + 1);
  252.                     //printf("arr: %s", in_val);
  253.                     in_val_arr[count_cal] = in_val;
  254.                 }
  255.             } else
  256.                 printf("Could not find a space\n");
  257.  
  258.             /* We now need to get the first word from the input buffer*/
  259.             in_key = strtok(buf, delimiter);
  260.             char *newkey = malloc(strlen(in_key) + 1); // <<<< allocate new memory
  261.             strcpy(newkey, in_key);
  262.             key_arr = strtol(newkey, &ptr, 10);
  263.             in_key_arr[count_cal] = key_arr;
  264.             count_cal++;
  265.         }
  266.         char *p_nl; // remove new line
  267.         for (int i = 0; i < count_cal; ++i) {
  268.             if ((p_nl = strchr(in_val_arr[i], '\n')) != NULL) { //removing new line at the end, allows for easier comparison
  269.                 *p_nl = '\0'; //adding string terminator at the end.
  270.             }
  271.             printf("key=%d, val = %s\n", in_key_arr[i], in_val_arr[i]);
  272.             root = insert_node(root, in_key_arr[i], in_val_arr[i]);
  273.         }
  274.         printf("\n");
  275.         fclose(f_cal);
  276.     }
  277.  
  278.     if (f_op) {
  279.         printf("Processing file...\n");
  280.         while (fgets(buf, MAXC, f_op)) {
  281.             p_line = malloc(strlen(buf) + 1); // malloced with size of buffer.
  282.             //p_line = malloc(sizeof (char) + 1); // malloced with size of buffer.
  283.             char *in_valop;
  284.             char *in_keyop;
  285.             char *in_op;
  286.  
  287.             strcpy(p_line, buf); //used to create a copy of input buffer
  288.             line[count_op] = p_line;
  289.  
  290.             /* separating the line based on the first space. The words after
  291.              * the delimeter will be copied into in_val */
  292.             char *copy = strchr(p_line, ' ');
  293.             if (copy) {
  294.                 if ((in_valop = alloc(strlen(line[count_op]) + 1)) == NULL) {
  295.                     return -1;
  296.                 } else {
  297.                     strcpy(in_valop, copy + 1);
  298.                     //printf("arr: %s", in_val);
  299.                     in_val_op[count_op] = in_valop;
  300.                 }
  301.             } else
  302.                 printf("Could not find a space\n");
  303.  
  304.             /* We now need to get the first word from the input buffer*/
  305.             in_keyop = strtok(buf, delimiter);
  306.             char *newkey = malloc(strlen(in_keyop) + 1); // <<<< allocate new memory
  307.             strcpy(newkey, in_keyop);
  308.             key_arr_op = strtol(newkey, &ptr, 10);
  309.             in_key_op[count_op] = key_arr_op;
  310.             //printf("%d\n", in_key_op[count]);
  311.             count_op++;
  312.         }
  313.         char *p_nl; // remove new line
  314.         for (int i = 0; i < count_op; ++i) {
  315.             if ((p_nl = strchr(in_val_op[i], '\n')) != NULL) { //removing new line at the end, allows for easier comparison
  316.                 *p_nl = '\0'; //adding string terminator at the end.
  317.             }
  318.             printf("key=%d, val = %s\n", in_key_op[i], in_val_op[i]);
  319.             root = insert_node(root, in_key_op[i], in_val_op[i]);
  320.         }
  321.         printf("\n");
  322.         fclose(f_op);
  323.     }
  324.  
  325.     /* Operating on data from operations file */
  326.     printf("Inorder traversal of the given tree \n");
  327.     inorder(root, &in_count);
  328.  
  329.     printf("\nDeepest unique keys of the given tree \n");
  330.     unique_key(root, &total_unique, unique_key_arr, value_arr);
  331.     printf("\n");
  332.  
  333.     printf("\nDeepest unique keys of the given tree in main\n");
  334.     for (int i = 0; i < total_unique; i++) {
  335.         printf("%d %s\n", unique_key_arr[i], value_arr[i]);
  336.         if (*value_arr[i] == 'C') { //Compare first char of pointer type
  337.         } else if (*value_arr[i] == 'U') { //Compare first char of pointer type
  338.         } else if (*value_arr[i] == 'D') { //Compare first char of pointer type
  339.         }
  340.     }
  341.     printf("\n");
  342.  
  343.     /* Map is for storing calendar. This should contain the unique elements
  344.      * from the BST above. Based on the first char inside `value_arr`, we
  345.      * should insert or delete a map. Number of operations to perform
  346.       depends on variable `unique_count`. Use strcpy()*/
  347.  
  348.     printf("\nDeepest unique keys to map structure: %d\n", total_unique);
  349.     for (int unique_count = 0; unique_count < total_unique; unique_count++) {
  350.         char* p_val; //pointer to char for value
  351.         char* p_key; //pointer to char for key
  352.         p_key = malloc(sizeof (char) + 1); //this is malloced to access different memory each time.
  353.  
  354.         if (*value_arr[unique_count] == 'C' || *value_arr[unique_count] == 'U') { //Compare first char of pointer type
  355.  
  356.             if ((p_val = alloc(strlen(value_arr[unique_count]) + 1)) == NULL) {
  357.                 return -1;
  358.             } else {
  359.                 strcpy(p_val, value_arr[unique_count]); //copying value to pointer to char
  360.                 VAL_arr_create[count_create] = p_val; //p passed to value array
  361.             }
  362.  
  363.             //Convert integer to pointer to char
  364.             sprintf(p_key, "%d", unique_key_arr[unique_count]);
  365.             KEY_arr_create[count_create] = p_key; //array containing all create/updates
  366.  
  367.                 printf("I am here %s, %s\n", KEY_arr_create[count_create], VAL_arr_create[count_create]);
  368.             count_create++;
  369.  
  370.         } else if (*value_arr[unique_count] == 'D') { //must be a delete
  371.  
  372.             if ((p_val = alloc(strlen(value_arr[unique_count]) + 1)) == NULL) {
  373.                 return -1;
  374.             } else {
  375.                 strcpy(p_val, value_arr[unique_count]); //copying value to pointer to char
  376.                 VAL_arr_delete[count_delete] = p_val; //p passed to value array
  377.             }
  378.  
  379.             //Convert integer to pointer to char
  380.             sprintf(p_key, "%d", unique_key_arr[unique_count]);
  381.             KEY_arr_delete[count_delete] = p_key; //array containing all deletes
  382.             printf("I am here2 %s, %s\n", KEY_arr_delete[count_delete], VAL_arr_delete[count_delete]);
  383.             count_delete++;
  384.         }
  385.     }
  386.  
  387.     map * table[100]; //pointer to type map structure
  388.  
  389.     printf("\nBefore insert CREATE %d\n", count_create);
  390.  
  391.     for (int i = 0; i < count_create; i++) {
  392.         printf("CREATE %s, %s\n", KEY_arr_create[i], VAL_arr_create[i]);
  393.         /* Here I need to remove leading operation letter to display*/
  394.         char *p;
  395.         char *copy_val_create = strchr(VAL_arr_create[i], ' ');
  396.         if (copy_val_create)
  397.             printf("COPY: %s\n", copy_val_create + 1);
  398.         else
  399.             printf("Could not find a space\n");
  400.         table[i] = insert_map(KEY_arr_create[i], copy_val_create + 1);
  401.     }
  402.  
  403.     printf("\nBefore insert DELETE %d\n", count_delete);
  404.     for (int i = 0; i < count_delete; i++) {
  405.         printf("DELETE %s, %s\n", KEY_arr_delete[i], VAL_arr_delete[i]);
  406.         remove_map(KEY_arr_delete[i]);
  407.     }
  408.  
  409.     f_cal = fopen("calendar.txt", "w+");
  410.     printf("\nUPDATED CALENDAR\n");
  411.     for (int i = 0; i < count_create; i++) {
  412.         printf("%s %s \n", table[i]->KEY, table[i]->value);
  413.         fprintf(f_cal, "%s %s\n", table[i]->KEY, table[i]->value);
  414.     }
  415.     fclose(f_cal);
  416.     printf("\n");
  417.  
  418.     //remove_map("4082018");
  419.  
  420.     return 0;
  421. }
Advertisement
Add Comment
Please, Sign In to add comment