Page 1 of 1

C Assignment

PostPosted: Mon Jan 28, 2008 12:00 pm
by whitfield
My friend asked me today to do his assignment. It's in C and seems quite simple. Here it is:

http://w3.cs.jmu.edu/arch/crs/cs462/prog/pa1.html

Re: C Assignment

PostPosted: Mon Jan 28, 2008 2:38 pm
by Bob
Code:
Code: Select all
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define IO_BUFFER_LENGTH 1024
#define MAX_WORD_ENTRIES 10000
#define FORMAT_STRING "%6d %6d %-s\n"

#define ERROR_TOOMANYWORDS "Too many words encountered, maximum is 1000.\n"
#define ERROR_COULDNOTOPEN "Could not open text file `%s' for reading.\n"
#define ERROR_NOMOREMEMORY "System error: the program's request for additional memory was denied.\n"
#define ERROR_LINETOOLONG "Input line number %d too long, more than 1024 characters.\n"

typedef struct Word MyWord;

int CheckArgs( int, char **);
int AddWord(MyWord **, char*, int);

void PrintWords(MyWord *B);
void CleanUp(MyWord **B);

MyWord * LoadFile(FILE*);
MyWord * CreateEntry(char *string, int iLine);

struct Word
{
   int FirstLine;
   int Count;

   char* string;

   MyWord *pNext;
};

int main ( int argc, char *argv[] )
{
   FILE * fp = stdin;
   MyWord *B;

   if (CheckArgs(argc, argv))
   {
      perror("Usage: WordCounter.exe [InputFile]\n\n");
      exit(1);
   }

   if(argc == 2)
   {
      fp = fopen(argv[1], "r");
      if (fp == NULL)
      {
         fprintf(stderr, ERROR_COULDNOTOPEN, argv[1]);
         exit(1);
      }
   }

   B = LoadFile(fp);
   PrintWords(B);
   // CleanUp(&B);

   return 0;
}

int CheckArgs( int argc, char *argv[] )
{
   int Error = 0;   

   if (argc > 2)
      Error = 1;

   return Error;
}

MyWord * LoadFile(FILE* fp)
{
   int iChar, iLength = 0, iLine = 1, iEntryCount = 0;
   char Buffer[IO_BUFFER_LENGTH];
   MyWord *pMain = NULL;
   
   while((iChar = fgetc(fp)) != EOF)
   {
      iChar = tolower(iChar);
   
      if ( (iChar  < 'a' || iChar > 'z') && (iChar != '\'' && iChar != '\n' ))
         iChar = ' ';

      if (iChar == ' ' || iChar == '\n')
      {
         Buffer[iLength] = '\0';

         if (strlen(Buffer) && AddWord(&pMain, Buffer, iLine))
            iEntryCount++;

         if (iChar == '\n')
            iLine++;
         
         iLength = 0;

         if (iEntryCount > MAX_WORD_ENTRIES)
         {
            fprintf(stderr, ERROR_TOOMANYWORDS);
            exit(1);
         }

         continue;
      }

      if (iLength >= IO_BUFFER_LENGTH)
      {
         fprintf(stderr, ERROR_LINETOOLONG, iLine);
         exit(1);
      }

      Buffer[iLength] = iChar;
      iLength++;
   }

   return pMain;
}

int AddWord(MyWord **pHead, char* string, int iLine)
{
   MyWord ** pWalker = pHead;
   int Added = 0;
 
   if (*pHead == NULL)
   {
      *pHead = CreateEntry(string, iLine);
      Added = 1;
   }
   else
   {
      while ((*pWalker)->pNext != NULL)
      {   
         if (strcmp((*pWalker)->string, string) == 0)
         {
            (*pWalker)->Count++;
            break;
         }
         
         pWalker = &(*pWalker)->pNext;
      }

      if (strcmp((*pWalker)->string, string) == 0)
      {
         (*pWalker)->Count++;
      }
      else if ((*pWalker)->pNext == NULL )   
      {
         (*pWalker)->pNext = CreateEntry(string, iLine);
         Added = 1;
      }
   }

   return Added;
}

MyWord *CreateEntry(char *string, int iLine)
{
   MyWord *tEntry = (MyWord *)malloc(sizeof(MyWord));

   if (tEntry == NULL)
   {
      fprintf(stderr, ERROR_NOMOREMEMORY);
      exit(1);
   }

   tEntry->Count = 1;
   tEntry->FirstLine = iLine;
   tEntry->string = (char *)malloc(strlen(string) + 1);
   strcpy(tEntry->string, string);
   tEntry->pNext = NULL;

   return tEntry;
}

void PrintWords(MyWord *B)
{
   printf(" First  Count Word\n==================\n");
   while (B != NULL)
   {
      printf(FORMAT_STRING, B->FirstLine, B->Count, B->string);
      B = B->pNext;
   }
}


Input File:
Code: Select all
Program 1: C, Basic program
Synopsis
words [ InputFile ]

Write a program that constructs a concordance from an input stream. A concordance is a list of the distinct words that appear in the text and, for each word, the line number of its first occurence and total number of occurences. The text should be read from InputFile. If no InputFile argument is specified, the program should read from standard input. The program should output (to standard output) the list of words, line numbers, and counts, one entry per line.

   1. Uppercase alphabetic characters in the input stream should be converted to lowercase.
   2. The input stream should be filtered to convert all characters but space, newline, letters, and apostrophe to space.
   3. Words are contiguous letters and apostrophes, separated by white-space (spaces or newlines).
   4. Input continues until EOF is reached.
   5. The line of output for each word should be in the following form where the first number is the line number (one-based) of the word's first occurence and the second number is the total number of occurences of the word.
          "%6d %6d %-s\n"
   6. The program should handle up to (and including) 1000 unique words in the text.
   7. The program should handle lines up to 1024 characters long (including the newline but not including the end-of-string character).
   8. In response to errors, the program should output to standard error output one of the following error messages and terminate with exit status 1.
         1. "Too many words encountered, maximum is 10000.\n"
         2. "Could not open text file `%s' for reading.\n"
         3. "System error: the program's request for additional memory was denied.\n"
         4. "Input line number %d too long, more than 1024 characters.\n"


Clarifications:

    * Programming Assignment 1 Q&A
    * 01/26/08: A specification was added for maximum length of input lines, see above.
    * 01/26/08: Depending on how you implement your program, error message 8c may not be necessary. But if you use malloc in your program and the call to malloc fails, 8c is the error message your program should print.
    * Tip: the function strtok(3) is very useful in parsing a string into words.

Source code of the program must be submitted using submit, with penalty points for late submissions as follows:

   AFTER THIS DATE   PENALTY
   08/01/28,23:00   5
   08/01/29,08:00   10
   08/01/29,23:00   30
   08/01/30,23:00   60
   08/01/30,08:00   100
   YR/MO/DY,HR:MI

Printouts of the PDF report file (stapled) must be handed at the beginning of the next class period after they are submitted. Points may be deducted for excessive submissions. Programs must adhere to the course style guidelines.


Output:
Code: Select all
 First  Count Word
==================
     1     23 program
     1      5 c
     1      1 basic
     2      1 synopsis
     3     13 words
     3      5 inputfile
     5      1 write
     5     13 a
     5      3 that
     5      1 constructs
     5      3 concordance
     5      5 from
     5      1 an
     5     13 input
     5      5 stream
     5     15 is
     5      3 list
     5     27 of
     5     65 the
     5      1 distinct
     5      1 appear
     5     13 in
     5      7 text
     5     17 and
     5     13 for
     5      3 each
     5      5 word
     5     11 line
     5     13 number
     5      1 its
     5      5 first
     5      3 occurence
     5      3 total
     5      3 occurences
     5     19 should
     5     15 be
     5      3 read
     5      3 if
     5      1 no
     5      1 argument
     5      1 specified
     5      5 standard
     5      9 output
     5     19 to
     5      1 numbers
     5      1 counts
     5      5 one
     5      1 entry
     5      1 per
     7      1 uppercase
     7      1 alphabetic
     7      7 characters
     7      1 converted
     7      1 lowercase
     8      1 filtered
     8      1 convert
     8      1 all
     8      5 but
     8      5 space
     8      3 newline
     8      3 letters
     8      1 apostrophe
     9      3 are
     9      1 contiguous
     9      1 apostrophes
     9      1 separated
     9      1 by
     9      1 white
     9      1 spaces
     9      1 or
     9      1 newlines
    10      1 continues
    10      1 until
    10      1 eof
    10      1 reached
    11      3 following
    11      1 form
    11      1 where
    11      1 based
    11      1 word's
    11      1 second
    12      4 d
    12      1 s
    12      9 n
    13      3 handle
    13      3 up
    13      5 including
    13      1 unique
    14      3 lines
    14      3 long
    14      5 not
    14      1 end
    14      3 string
    14      1 character
    15      1 response
    15      1 errors
    15      8 error
    15      1 messages
    15      1 terminate
    15      3 with
    15      1 exit
    15      1 status
    16      3 too
    16      1 many
    16      1 encountered
    16      3 maximum
    17      1 could
    17      1 open
    17      3 file
    17      1 s'
    17      1 reading
    18      1 system
    18      1 program's
    18      1 request
    18      1 additional
    18      1 memory
    18      3 was
    18      1 denied
    19      1 more
    19      1 than
    22      1 clarifications
    24      1 programming
    24      1 assignment
    24      1 q
    25      1 specification
    25      1 added
    25      1 length
    25      1 see
    25      1 above
    26      1 depending
    26      1 on
    26      1 how
    26      3 you
    26      1 implement
    26      5 your
    26      3 message
    26      3 may
    26      1 necessary
    26      1 use
    26      3 malloc
    26      1 call
    26      1 fails
    26      1 print
    27      1 tip
    27      1 function
    27      1 strtok
    27      1 very
    27      1 useful
    27      1 parsing
    27      1 into
    29      1 source
    29      1 code
    29      5 must
    29      3 submitted
    29      1 using
    29      1 submit
    29      3 penalty
    29      3 points
    29      1 late
    29      3 submissions
    29      1 as
    29      1 follows
    31      3 after
    31      1 this
    31      1 date
    37      1 yr
    37      1 mo
    37      1 dy
    37      1 hr
    37      1 mi
    39      1 printouts
    39      1 pdf
    39      1 report
    39      1 stapled
    39      1 handed
    39      1 at
    39      1 beginning
    39      1 next
    39      1 class
    39      1 period
    39      1 they
    39      1 deducted
    39      1 excessive
    39      1 programs
    39      1 adhere
    39      1 course
    39      1 style
    39      1 guidelines