Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <getopt.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. typedef struct bstsort_node
  8. {
  9.     char *word;
  10.     int count;
  11.    
  12.     struct bstsort_node* left;
  13.     struct bstsort_node* right;
  14.    
  15. }node;
  16.  
  17. int stringCompare(char *str1, char *str2)
  18. {
  19.    int i = 0;
  20.    char case1, case2;
  21.    
  22.    while(str1[i] != 0 && str2[i] != 0)
  23.    {
  24.        case1 = str1[i];
  25.        case2 = str2[i];
  26.        if(case1 != case2)
  27.        {
  28.            return case1-case2;
  29.            i++;
  30.        }
  31.        if(str1[i] == 0 && str2 != 0)
  32.        {
  33.            return str2[i];
  34.        }
  35.        else if(str1 != 0 && str2 == 0)
  36.        {
  37.            return str1[i];
  38.        }
  39.        else
  40.        {
  41.            return 0;
  42.        }
  43.    }
  44. }
  45. int stringCompare2(char *str1, char *str2)
  46. {
  47.    int i = 0;
  48.    char case1, case2;
  49.    while(str1[i] != 0 && str2[i] != 0)
  50.    {
  51.        case1 = str1[i];
  52.        case2 = str2[i];
  53.        if(case1 >= 'A' && case1 <= 'Z')
  54.        {
  55.            case1 = case1 - 'A' + 'a';
  56.        }
  57.        if(case2 >= 'A' && case2 <= 'Z')
  58.        {
  59.            case2 = case2 - 'A' + 'a';
  60.        }
  61.          
  62.        if(case1 != case2)
  63.        {
  64.            return case1 - case2;
  65.            i++;
  66.        }
  67.        if(str1[i] == 0 && str2 != 0)
  68.        {
  69.            return str2[i];
  70.        }
  71.        else if(str1 != 0 && str2 == 0)
  72.        {
  73.            return str1[i];
  74.        }
  75.        else
  76.        {
  77.            return 0;
  78.        }
  79.    }
  80. }
  81.  
  82. void freeNode(node *root)
  83. {
  84.  
  85.     if (root != NULL)
  86.     {
  87.         freeNode(root->left);
  88.         freeNode(root->right);
  89.        
  90.         free(root->word);
  91.         free(root);
  92.      }
  93.      
  94. }
  95. node* createNode(char *string)
  96. {
  97.    node* n = (node*) malloc(sizeof(node));
  98.    n->word = (char*) malloc(strlen(string)+1);
  99.    strcpy(n->word, string);
  100.    n->left = NULL;
  101.      n->right = NULL;
  102.     return n;
  103. }
  104. void insert(node *currentNode, char *string, int cases)
  105. {
  106.     node *position;
  107.     int compare;
  108.    
  109.     if(currentNode == NULL)
  110.     {
  111.         currentNode = createNode(string);
  112.     }
  113.         if(cases==1)
  114.         {
  115.             if(stringCompare(string, currentNode->word) < 0)
  116.             {
  117.                 insert(currentNode->right,string, cases);
  118.             }
  119.             else if(stringCompare(string,currentNode->word) == 0)
  120.             {
  121.                 currentNode->count = currentNode-> count + 1;
  122.             }
  123.         }
  124.         if(cases == 0)
  125.         {
  126.             if(stringCompare2(string,currentNode->word) < 0)
  127.             {
  128.                 insert(currentNode->left, string, cases);
  129.             }
  130.             else if(stringCompare2(string, currentNode->word) > 0)
  131.             {
  132.                 insert(currentNode->right, string, cases);
  133.             }
  134.             else if(stringCompare2(string,currentNode->word) == 0)
  135.             {
  136.                 currentNode->count = currentNode->count + 1;
  137.             }
  138.         }  
  139.         //return node;
  140.         printf("on insert %p", currentNode);
  141. }      
  142.  
  143. void inOrder(FILE *outFile, node *node)
  144. {
  145.    printf("for inorder %p", node);
  146.    if(node == NULL)
  147.    {
  148.        return;
  149.    }
  150.    
  151.    inOrder(outFile, node->left);
  152.    fprintf(outFile, "%s[%d]\n", node->word, node->count);
  153.    inOrder(outFile, node->right);
  154.    
  155. }
  156.  
  157. int main(int argc, char **argv)
  158. {
  159.    extern char *optarg;
  160.    extern int optind;
  161.    int c, err = 0;
  162.    int cflag=0, oflag=0;
  163.    int cases = 0;
  164.    char string[100], temp[100], ch;
  165.    node *root = NULL;
  166.    int results = 1;
  167.    //int i = 0;
  168.    char *output_file_name, *input_file_name;
  169.    FILE* inFile = stdin;
  170.    FILE* outFile = stdout;
  171.    static char usage[] = "usage: % bstsort [-c][-o output_file_name][input_file_name]\n";
  172.  
  173.    while ((c = getopt(argc, argv, "co:")) != -1)
  174.        switch (c) {
  175.        case 'c':
  176.            cflag = 1;
  177.            break;
  178.        case 'o':
  179.            oflag = 1;
  180.            output_file_name = optarg;
  181.            break;
  182.        case '?':
  183.            err = 1;
  184.            break;
  185.        }
  186.     if(cflag == 1)
  187.     {
  188.        cases = 1;
  189.     }
  190.     if(oflag == 1)
  191.     {
  192.        if((outFile = fopen(output_file_name, "r")) == NULL)
  193.        {
  194.            
  195.            printf("Could not open file.\n");
  196.            
  197.            exit(1);
  198.        
  199.        }
  200.    
  201.     }
  202.     if(optind > argc)
  203.    {
  204.        input_file_name = argv[optind];
  205.    
  206.        if((inFile = fopen(input_file_name, "r")) == NULL)
  207.        {
  208.          
  209.            printf("Could not open file.\n");
  210.            
  211.          
  212.            exit(1);
  213.        
  214.        }
  215.    }
  216.  
  217.    while(fgets(string, 100, inFile) != NULL)
  218.    {
  219.        if(stringCompare("\n", string) == 0)
  220.        break;
  221.      
  222.        string[strlen(string) - 1] = 0;
  223.        insert(root, string, cases);
  224.    }
  225.    fclose(inFile);
  226.    inOrder(outFile, root);
  227.    fclose(outFile);
  228.    freeNode(root);
  229.    exit(0);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement