GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jun-2004, 12:17
alexandro alexandro is offline
New Member
 
Join Date: Jun 2004
Posts: 2
alexandro is on a distinguished road

testing word file "need help"


CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXWORD      40
#define DELIMITER_SET	" /|\\\"\t\n\r,;:~!#%^*()=+[]{}<>?"

struct wordnode
{

   char word[MAXWORD];
   int num;
   struct wordnode *next;
};

typedef struct wordnode WordPtr;

void sorted_insert(struct wordnode **hd, WordPtr *elem);
WordPtr *ins_sort(WordPtr *list);
WordPtr *newWord(void);
void setWordData(WordPtr *list, char *word, int num);
WordPtr *insert(WordPtr *list, WordPtr *element);
void printWord(WordPtr *list, char *filename);
WordPtr *deleteAll(WordPtr *list);
FILE *openFile(char *filename, char *mode);
void fileToStruct(char *fileName);

int main(int argc, char *argv[])
{
   if (argc != 2)
   {
      printf("Syntax : wordcount filename\n");
      exit(0);
   }
   fileToStruct(argv[1]);
   return 0;
}

void sorted_insert(struct wordnode **hd, WordPtr *elem)
{
   while (*hd && strcmp((*hd)->word, elem->word) <= 0)
   {
      hd = &(*hd)->next;
   } 
   elem->next =  *hd;
   *hd = elem;
}

/* insertion sort for linked lists */
WordPtr *ins_sort(WordPtr *list)
{
   WordPtr *sorted = 0;
   WordPtr *t;

   while (list != 0)
   {
      t = list;
      list = list->next;
      sorted_insert(&sorted, t);
   }
   return sorted;
}

void fileToStruct(char *fileName)
{
   FILE *fin = openFile(fileName, "r");
   char string[MAXWORD];
   char *longstring;
   char *tokagain;
   WordPtr *head = NULL, *tmp = NULL;
   int same;
   char line[150];

   while (fscanf(fin, "%s", line) != EOF)
   {
      /* set the word is not in the list yet */
      same = 0;

      longstring = strtok(line, DELIMITER_SET);
      while (longstring != NULL)
      {
         strcpy(string, longstring);
         //printf ("%s\n",string);
         tokagain = strtok (string, ".");
         printf ("tok again = %s \n",tokagain);
         if (head == NULL)
         {
            /* insert string, 1, tmp into linked list as the head */
            head = newWord(); 

            /* set a new struct */
            setWordData(head, string, 1);
         }
         else
         {
            for (tmp = head; tmp != NULL; tmp = tmp->next)
            {
               if ((strcmp((tmp->word), string)) == 0)
               {
                  tmp->num++;

                  /* set the word is in the list already */
                  same = 1;
               }
            }
            tmp = NULL;

            /* If the word is not in the list yet, add it */
            if (same != 1)
            {
               tmp = newWord();
               setWordData(tmp, string, 1);
               head = insert(head, tmp);

               /* insert string, 1, tmp, into the back of the linked list */
            }
         }
         longstring = strtok(NULL, DELIMITER_SET);
      }
   }

   head = ins_sort(head);
   printWord(head, fileName);
   head = deleteAll(head);
   fclose(fin);
}

FILE *openFile(char *filename, char *mode)
{
   /* open a FILE stream to filename with given mode */

   FILE *file = fopen(filename, mode);

   if (file == NULL)
   {
      printf("ERROR - Unable to access %s\n", filename);
      exit(0);
   }

   return file;
}

/* a function to form a new structure */
WordPtr *newWord(void)
{
   WordPtr *element; 

   /* initialise a pointer */
   element = (WordPtr*)malloc(sizeof(WordPtr)); 

   /* aloocate a memory */
   if (element == NULL)
   {
      printf("\nWARNING - Memory allocation error\n");
      exit(0);
   }
   element->next = NULL;
   return (element);
}

/* set the input data into the struct */
void setWordData(WordPtr *list, char *word, int num)
{
   strcpy(list->word, word);
   list->num = num;
   return ;
}

/* a function to join a list with another existing list */
WordPtr *insert(WordPtr *list, WordPtr *element)
{
   element->next = list;
   return (element);
}

/* a function to print out the structure */
void printWord(WordPtr *list, char *filename)
{
   WordPtr *tmp;
   int size = 0;
   int total = 0;

   for (tmp = list; tmp != NULL; tmp = tmp->next)
   {
      total += tmp->num;
      size += 1;
   }

   printf("\nfilename : %s\n", filename);
   printf("\n");
   printf("COUNT | WORD\n");
   printf("------+------\n");

   for (tmp = list; tmp != NULL; tmp = tmp->next)
   {
      printf("%4d  | %s \n", tmp->num, tmp->word);
   }
   printf("\n");
   printf("Number of unique words in %s is %d\n", filename, size);
   printf("Total number of words in %s is  %-2d\n", filename, total);

   size = 0;
   total = 0;
   return ;
}

/* a function to free the aloocated memory */
WordPtr *deleteAll(WordPtr *list)
{
   WordPtr *tmp;

   for (; list != NULL; list = tmp)
   {
      tmp = list->next;
      free(list);
   }
   return (NULL);
}



here is the test file contents...copy/paste and save it as test3.dat
12.34.
.3
3..3
.3.4
..8
.this
test.this
test.12.34
test1.this
test1.12.34


compile the code and run it... "programname test3.dat"

current result:
filename : wtest3.dat

COUNT | WORD
------+------
1 | ..8
1 | .3
1 | .3.4
1 | .this
1 | 12.34.
1 | 3..3
1 | test.12.34
1 | test.this
1 | test1.12.34
1 | test1.this


expected result shouldbe
alexandro@alex#wordcount wtest3.dat

filename : wtest3.dat

COUNT | WORD
------+------
1 | .3
1 | .3.4
2 | 12.34
2 | 3
1 | 8
2 | test
1 | test1
1 | test1.12.34
3 | this

Number of unique words in wtest3.dat is 9
Total number of words in wtest3.dat is 14



CAN ANYONE PLEASEEE HELP MEEEEEEEEEEEE!!!!!
Last edited by dsmith : 02-Jun-2004 at 16:09. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 02-Jun-2004, 20:38
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Hi Alexandro,

What is it you're actually tryng to do?? You posted a complete code with the results you get and the expected result, but still I can't figure out what you are tryng to do... is that some king of sorting the data??

And it would be better if you could point where actually in the code youre having problem with. since it would take time to analyze your code one by one before anyone could figure it out..
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 03-Jun-2004, 14:38
alexandro alexandro is offline
New Member
 
Join Date: Jun 2004
Posts: 2
alexandro is on a distinguished road

thanks for helping


actually i want to make ..8 is 8
and 3..3 is 3 with two occurences
but like 3.4 is still counted one word

may be my tokenizer must be recoded again
can you bug it for me?

thank a lot
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56
read specified range of lines from file etron C Programming Language 8 18-Feb-2004 08:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.