Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: jason
- *
- * Created on January 30, 2011, 4:21 AM
- */
- #include "header.h"
- int main(int argc, char** argv) {
- if (argc==1){
- printf("You must give an argument in the command line to run this program\n");
- return (EXIT_SUCCESS);
- }
- else if (strcmp(argv[1], "-h") == 0){
- help();
- return (EXIT_SUCCESS);
- }
- else {
- FILE* wordfile;
- linkedlist * head=NULL;
- wordfile=fopen(argv[1],"r");
- if (wordfile!=NULL){
- char line [ 80 ];
- while ( fgets ( line, sizeof(line), wordfile ) != NULL )
- {
- const char delimiters[]=" .:;()!@&$?-',><1234567890/ |][{}*^%#`~=+_\\\"";
- char *token;
- for (token = strtok (line, delimiters);token!=NULL;token = strtok (NULL, delimiters)){
- Upper(token);
- add(head, token);
- }
- }
- fclose (wordfile);
- printlist(head);
- destroy(head);
- }
- else {
- printf("File cannot be opened!\n");
- }
- }
- return (EXIT_SUCCESS);
- }
- void Upper(char* token){
- int i;
- for(i=0;token[i];i++){
- token[i]=toupper(token[i]);
- }
- return;
- }
- void help(){
- printf("\tThis program takes a text file as input and takes from it\n");
- printf("\tall the words that are in it and then checks if they are in\n");
- printf("\ta dictionary file that must also be included and then it\n");
- printf("\tprints the words out alphabetically and it also prints out\n");
- printf("\ttheir frequency and if they are in the dictionary or not.\n");
- printf("\tTo run this program you must enter an argument in the command line\n");
- printf("\t,which can either be '-h' for the help menu or a text file to be checked\n");
- printf("\tby the program.\n");
- printf("\tAn example input would be ./wordstat -h for help or ./wordstat textfile.txt\n");
- return;
- }
- char *strdup(const char *str)
- {
- int n = strlen(str) + 1;
- char *dup = malloc(n * sizeof(char));
- if(dup)
- {
- strcpy(dup, str);
- }
- return dup;
- }
- void destroy(linkedlist* head)
- {
- linkedlist *current, *tmp;
- current = head->next;
- head->next = NULL;
- while(current != NULL) {
- tmp = current->next;
- free(current->data);
- free(current);
- current = tmp;
- }
- }
- void printlist(linkedlist* head)
- {
- linkedlist * current;
- current = head;
- if(current!= NULL)
- {
- do
- {
- printf("WORDS\tFREQUENCIES\tIN DICTIONARY?\n");
- printf("%s",(current->data));
- printf("%d\t",(current->frequency));
- if (current->indict==1){
- printf("Y\t\n");
- }
- else {
- printf("N\t\n");
- }
- current = current->next;
- } while (current!= head);
- }
- else
- printf("The list is empty\n");
- }
- /*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
- increment its frequency It takes in two parameters and it also sorts the list alphabetically*/
- linkedlist* add(linkedlist * head, char* data){
- linkedlist* tmp=NULL;
- linkedlist* prev=NULL;
- linkedlist* current=NULL;
- int c;
- if(head == NULL){
- head=malloc(sizeof(linkedlist));
- head->data=strdup(data);
- head->frequency++;
- Checkdict(head);
- head->next=NULL;
- return head;
- }
- tmp=head;
- while (tmp!=NULL){
- c=strcmp(data,tmp->data);
- if (c<0){
- prev=malloc(sizeof(linkedlist));
- prev->data=strdup(data);
- prev->frequency++;
- Checkdict(prev);
- if (tmp==head){
- /*before head case*/
- prev->next = tmp;
- tmp = prev;
- }
- else{
- /*in between*/
- prev->next = current->next;
- current->next = prev;
- }
- return head;
- }
- else if (c==0){
- tmp->frequency++;
- return head;
- }
- else {
- tmp=tmp->next;
- current=tmp;
- }
- }
- tmp=malloc(sizeof(linkedlist));
- tmp->data=strdup(data);
- tmp->frequency++;
- Checkdict(tmp);
- prev->next=tmp;
- tmp->next=NULL;
- return head;
- }
- void Checkdict(linkedlist* ptr){
- int c;
- FILE* Dictionary;
- Dictionary=fopen("dict.txt","r");
- if (Dictionary!=NULL){
- char dict [80];
- while ( fgets ( dict, sizeof(dict), Dictionary) != NULL)
- {
- c=strcmp(ptr->data,dict);
- if (c==0){
- ptr->indict=1;
- }
- }
- }
- fclose ( Dictionary );
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement