Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.40 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: jason
  4.  *
  5.  * Created on January 30, 2011, 4:21 AM
  6.  */
  7.  
  8. #include "header.h"
  9.  
  10. int main(int argc, char** argv) {
  11.     if (argc==1){
  12.         printf("You must give an argument in the command line to run this program\n");
  13.         return (EXIT_SUCCESS);
  14.     }
  15.         else if (strcmp(argv[1], "-h") == 0){
  16.             help();
  17.             return (EXIT_SUCCESS);
  18.     }
  19.     else {
  20.         FILE* wordfile;
  21.         linkedlist * head=NULL;
  22.         wordfile=fopen(argv[1],"r");
  23.         if (wordfile!=NULL){
  24.             char line [ 80 ];
  25.            
  26.                  while ( fgets ( line, sizeof(line), wordfile ) != NULL )
  27.                   {
  28.                 const char delimiters[]=" .:;()!@&$?-',><1234567890/ |][{}*^%#`~=+_\\\"";
  29.                
  30.                 char *token;
  31.                 for (token = strtok (line, delimiters);token!=NULL;token = strtok (NULL, delimiters)){
  32.                     Upper(token);
  33.                     add(head, token);
  34.                 }
  35.                  }
  36.                     fclose (wordfile);
  37.                 printlist(head);
  38.                 destroy(head);
  39.             }
  40.            
  41.         else {
  42.             printf("File cannot be opened!\n");
  43.         }
  44.        
  45.     }
  46.  
  47.    
  48.  return (EXIT_SUCCESS);
  49. }
  50. void Upper(char* token){
  51.     int i;
  52.     for(i=0;token[i];i++){
  53.         token[i]=toupper(token[i]);
  54. }
  55. return;
  56. }
  57.  
  58. void help(){
  59.    
  60.         printf("\tThis program takes a text file as input and takes from it\n");
  61.         printf("\tall the words that are in it and then checks if they are in\n");
  62.         printf("\ta dictionary file that must also be included and then it\n");
  63.         printf("\tprints the words out alphabetically and it also prints out\n");
  64.         printf("\ttheir frequency and if they are in the dictionary or not.\n");
  65.         printf("\tTo run this program you must enter an argument in the command line\n");
  66.         printf("\t,which can either be '-h' for the help menu or a text file to be checked\n");
  67.         printf("\tby the program.\n");
  68.         printf("\tAn example input would be ./wordstat -h for help or ./wordstat textfile.txt\n");
  69.         return;
  70.    
  71. }
  72. char *strdup(const char *str)
  73. {
  74.     int n = strlen(str) + 1;
  75.     char *dup = malloc(n * sizeof(char));
  76.     if(dup)
  77.     {
  78.         strcpy(dup, str);
  79.     }
  80.     return dup;
  81. }
  82. void destroy(linkedlist* head)
  83. {
  84.     linkedlist *current, *tmp;
  85.    
  86.     current = head->next;
  87.     head->next = NULL;
  88.     while(current != NULL) {
  89.         tmp = current->next;
  90.         free(current->data);
  91.         free(current);
  92.         current = tmp;
  93.     }
  94. }
  95.  
  96. void printlist(linkedlist* head)
  97. {
  98.     linkedlist * current;
  99.     current = head;
  100.     if(current!= NULL)
  101.     {
  102.         do
  103.         {  
  104.             printf("WORDS\tFREQUENCIES\tIN DICTIONARY?\n");
  105.             printf("%s",(current->data));
  106.             printf("%d\t",(current->frequency));
  107.             if (current->indict==1){
  108.                 printf("Y\t\n");
  109.             }
  110.             else {
  111.                 printf("N\t\n");
  112.             }
  113.             current = current->next;
  114.         } while (current!= head);
  115.        
  116.     }
  117.     else
  118.         printf("The list is empty\n");
  119.  
  120. }
  121. /*This function will add a word to the linked list and also make sure to check if the word is already in there and just
  122. increment its frequency It takes in two parameters and it also sorts the list alphabetically*/
  123. linkedlist* add(linkedlist * head, char* data){
  124. linkedlist* tmp=NULL;
  125. linkedlist* prev=NULL;
  126. linkedlist* current=NULL;
  127. int c;
  128. if(head == NULL){
  129.     head=malloc(sizeof(linkedlist));
  130.         head->data=strdup(data);
  131.     head->frequency++;
  132.     Checkdict(head);
  133.     head->next=NULL;
  134.     return head;
  135. }
  136. /*head is not null so must check to see if word is equal to head.data*/
  137. tmp=head;
  138. while (tmp!=NULL){
  139.     c=strcmp(data,tmp->data);
  140.     if (c<0){
  141.         /*word comes before tmp and not in list*/
  142.         prev=malloc(sizeof(linkedlist));
  143.             prev->data=strdup(data);
  144.         prev->frequency++;
  145.         Checkdict(prev);
  146.         if (tmp==head){
  147.             /*before head case*/
  148.             prev->next = tmp;
  149.             tmp = prev;
  150.         }
  151.         else{
  152.             /*in between*/
  153.             prev->next = current->next;
  154.             current->next = prev;
  155.         }  
  156.         return head;
  157. }   /*Word is equal to tmp so just increase frequency*/
  158.     else if (c==0){
  159.         tmp->frequency++;
  160.         return head;
  161. }   /*not equal or less so must be greater*/
  162.     else {
  163.         tmp=tmp->next;
  164.         current=tmp;
  165.     }
  166. }
  167.     /*word not in list so must be added to the end of list*/
  168.     tmp=malloc(sizeof(linkedlist));
  169.         tmp->data=strdup(data);
  170.     tmp->frequency++;
  171.     Checkdict(tmp);
  172.     prev->next=tmp;
  173.     tmp->next=NULL;
  174.     return head;
  175. }
  176.  
  177. void Checkdict(linkedlist* ptr){
  178.  
  179. int c;
  180. FILE* Dictionary;
  181. Dictionary=fopen("dict.txt","r");
  182. if (Dictionary!=NULL){
  183.     char dict [80];
  184.         while ( fgets ( dict, sizeof(dict), Dictionary) != NULL)
  185.  { 
  186.     c=strcmp(ptr->data,dict);
  187.     if (c==0){
  188.         ptr->indict=1;
  189.         }
  190.                
  191.       }
  192.    
  193. }
  194.     fclose ( Dictionary );
  195.     return;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement