<<<>>>Need C code advice with functions and sorting.<<<>>> 
Author Message
 <<<>>>Need C code advice with functions and sorting.<<<>>>

    Write a program which uses function pointers to implement a variety of
    sorting criteria.

    Your program will be given two command-line arguments: the first is the
    name of a file to be sorted; the second (which is optional) is a string of
    characters which describes how the file is to be sorted (in all cases, the
    file will be sorted line-by-line).

    In your program, main() will read the command-line arguments, load the
    file, and determine which sorting approach will use. It will then call the
    function sort, which you will write, and pass the necessary parameters,
    including pointers to functions which implement the appropriate sorting
    criteria, then output the results.

    The input file will contain a maximum of 100 lines, each of which has a
    maximum length of 40 characters. You may use an array of strings to
    store the contents of the file.

    You must typedef the compfun type, as well as modifying the body of
    the functionappropriately.

    The contents of the second command-line argument are to be interpreted
    as follows:

        The default behavior (i.e. if the second command-line argument does
        not exist) is for the file to be sorted in ascending order using
        lexicographic ordering (which is the ordering provided by strcmp().
        If the second command-line argument contains the character 'r', the
        sort is to be carried out in reverse order.
        If the second command-line argument contains the character 'n', the
        sort should treat each line as an integer (using atoi()) and sort the
        file numerically.
        If the second command-line argument contains the character 'f', the
        sort should "fold" upper and lower case letters together so the sorting
        is case-insensitive -- for example, "A" and "and" should both come
        before "banana" (unless the 'r' flag reverses the order of the sort).
        You may wish to use tolower() and/or toupper().

    Note that the n and f options may not appear together, but that both may
    appear with r.

    Examples

    The file

    17
    2001
    20
    1700
    35
    -57

    sorted with nr results in

    2001
    1700
    35
    20
    17
    -57

    The file

    the
    quick
    brown
    fox

    sorted with no flags produces

    brown
    fox
    quick
    the

I missed the class when my teacher was talking about Function pointers and
sorting so this is a little hard for me. Here is what I came up with.
Can anyone please tell me how to make this work? Thank You.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort(char *[], int , int);

typedef int (*compfun) (void *, void *);
static char string[40];
FILE *fileread;

int main(int argc, char *argv[])
{

argc=argc-1;
fileread=fopen(argv[1],"r");

if (fileread == NULL){
      printf("Unable to open %s\n", argv[2]);
          exit(EXIT_FAILURE);

Quote:
}

   while (fgets(string, 40, fileread) != NULL) {
             if(isdigit(string)==1) /* I know it accepts chars but would this
still work? */
           sort(string,40,             );

          if(isalpha(string)==1)
                sort(string,40              );
   }
return 0;

Quote:
}

/* Bubble sort function for but it's in's only */

void sort (char *lines[], int size, compfun cmp)
{
        int i,max,temp;

        for (max=size-1; max > 0; max--)  
                for (i=0; i< max; i++)
                        if (lines[i] > lines[i+1]) {
                                temp = lines[i];
                                lines[i] = lines[i+1];
                                lines[i+1] = temp;
                      }

Quote:
}



Thu, 01 Nov 2001 03:00:00 GMT  
 <<<>>>Need C code advice with functions and sorting.<<<>>>

[...snip description of assignment...]

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>

   void sort(char *[], int , int);

   typedef int (*compfun) (void *, void *);
   static char string[40];
   FILE *fileread;

You only have space for one string.  You'll need space for up to 100
strings.  For instance, you could declare `char strings[100][41];'.
Note that you need 41 bytes to store a 40-character string, if you
include the null terminator.

   int main(int argc, char *argv[])
   {

   argc=argc-1;
   fileread=fopen(argv[1],"r");

   if (fileread == NULL){
         printf("Unable to open %s\n", argv[2]);
             exit(EXIT_FAILURE);
   }
      while (fgets(string, 40, fileread) != NULL) {
                if(isdigit(string)==1) /* I know it accepts chars but would this
   still work? */
              sort(string,40,             );

             if(isalpha(string)==1)
                   sort(string,40              );
      }
   return 0;
   }
   /* Bubble sort function for but it's in's only */

Ugh.  Why not use qsort() inside sort()?  It would be faster, I'm
sure.  It would be valid, too, as long as you change `void *' to
`const void *' for your typedef of compfun.

   void sort (char *lines[], int size, compfun cmp)
   {
[...]
   }

You know, now that I come to think of it, it would be more convenient
to use char * pointers in place of void * pointers, so maybe you'll
want to ignore my qsort() advice above.  I'll assume you use char *
pointers below.

Okay, now I'm sure what you're having trouble with is the function
pointers.  I think that you're expected to do something like this:

/* Sorts lexicographically in forward order. */
int
srt_lf (const char *a, const char *b)
{
  return strcmp (a, b);

Quote:
}

/* Sorts lexicographically in reverse order. */
int
srt_lr (const char *a, const char *b)
{
  return -strcmp (a, b);

Quote:
}

/* Sorts numerically in forward order.
   Ignores overflow; a proper function that handles it is
   left as a (trivial) exercise for the reader. */
int
srt_lr (const char *a, const char *b)
{
  return atoi (a) - atoi (b);

Quote:
}

/* Sorts lexicographically in reverse order. */
int
srt_lr (const char *a, const char *b)
{
  return atoi (b) - atoi (a);

Quote:
}

etc.

Then you have to decide which of these to pass to sort().  You'll want
to do something like this:

if (argc < 3)
  sort_func = srt_lf;
else if (strchr (argv[2], 'r'))
  {
    /* Reverse order.  Test for n and f. */
    ...
  }
else if (strchr (argv[2], 'n'))
  sort_func = srt_nf;
else if (strchr (argv[2], 'f'))
  sort_func = srt_ff;
else
  sort_func = srt_lf;

Of course you'll need to declare or prototype all those functions
before trying to use them that way.

There.  I hope I've given enough detail that you'll be able to figure
out your assignment but not enough that I just gave you the answer.
--
"In My Egotistical Opinion, most people's C programs should be indented six
 feet downward and covered with dirt." -- Blair P. Houghton
Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Thu, 01 Nov 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

4. >>>Windows Service<<<

5. <<<< C Grammar for yacc needed >>>>

6. >>I need help!<<(duh)

7. proposal: <basic.h>, <pascal.h>, <fortran.h>, <cobol.h>

8. - - - = = =<><> W e b - U S A <><>= = = - - -

9. >>>> * Get keypress * <<<<

10. >>> GIVE COMMERCIAL SOFTWARES against source code <<<<

11. <Ctrl>+<Key>

12. <<testing>>

 

 
Powered by phpBB® Forum Software