[Exercise] Exercise 5-16. Add the -d ("directory order'') page 121

User avatar

Topic author
BJT
Site Admin
Site Admin
Posts: 141
Joined: Thu Apr 20, 2017 4:43 pm
Instruction: University Student
Gender:
Age: 27
Venezuela

Exercise 5-16. Add the -d ("directory order'') page 121

Post by BJT » Sun May 07, 2017 9:56 pm

Exercise 5-16. Add the -d ("directory order'') option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define NUMERIC 1
  7. #define DECR 2
  8. #define FOLD 4
  9. #define DIR 8
  10. #define MAXLINES 5000       /* max #lines to be sorted */
  11. char *lineptr[MAXLINES];    /* pointers to text lines */
  12.  
  13. int readlines(char *lineptr[], int nlines);
  14. void writelines(char *lineptr[], int nlines, int decr);
  15. void my_qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
  16. int numcmp(char *, char *);
  17. int charcmp(char *, char *);
  18.  
  19. static char option=0;
  20.  
  21. /* sort input lines */
  22. int main(int argc, char *argv[])
  23. {
  24.     int nlines;     /* number of input lines read */
  25.  
  26.     while (--argc>0&& **(++argv)=='-')
  27.     {
  28.         if( *(*(argv)+1)=='n')
  29.         {
  30.             option|=NUMERIC;
  31.         }
  32.         else if ( *(*(argv)+1)=='r')
  33.         {
  34.             option|=DECR;
  35.         }
  36.         else if ( *(*(argv)+1)=='f')
  37.         {
  38.             option|=FOLD;
  39.         }
  40.         else if ( *(*(argv)+1)=='d')
  41.         {
  42.             option|=DIR;
  43.         }
  44.     }
  45.  
  46.     if ((nlines = readlines(lineptr, MAXLINES)) >= 0)
  47.     {
  48.         if(option&NUMERIC)
  49.             my_qsort((void**) lineptr, 0, nlines-1,(int (*)(void*,void*))numcmp);
  50.         else
  51.             my_qsort((void**) lineptr, 0, nlines-1,(int (*)(void*,void*))charcmp);
  52.         writelines(lineptr, nlines, option & DECR);
  53.         return 0;
  54.     }
  55.     else
  56.     {
  57.         printf("input too big to sort\n");
  58.         return 1;
  59.     }
  60. }
  61.  
  62. #define MAXLEN 1000 /* max length of any input line */
  63. size_t get_line(char *, int);
  64. char *alloc(int);
  65.  
  66. /* readlines: read input lines */
  67. int readlines(char *lineptr[], int maxlines)
  68. {
  69.     int len, nlines;
  70.     char *p, line[MAXLEN];
  71.  
  72.     nlines = 0;
  73.     while ((len = get_line(line, MAXLEN)) > 0)
  74.         if (nlines >= maxlines || (p = alloc(len))== NULL)
  75.             return -1;
  76.         else
  77.         {
  78.             line[len-1] = '\0'; /* delete newline */
  79.             strcpy(p, line);
  80.             lineptr[nlines++] = p;
  81.         }
  82.  
  83.     return nlines;
  84. }
  85.  
  86. /* writelines: write output lines */
  87. void writelines(char *lineptr[], int nlines, int decr)
  88. {
  89.     int i;
  90.  
  91.     if (decr) /*Print in reveser order*/
  92.         for(int i=nlines-1;i>=0;i--)
  93.             printf("%s\n", lineptr[i]);
  94.     else
  95.         for (i = 0; i < nlines; i++)
  96.             printf("%s\n", lineptr[i]);
  97. }
  98.  
  99. /* qsort: sort v[left]...v[right] into increasing order */
  100. void my_qsort(void *v[], int left, int right, int (*comp)(void *, void *))
  101. {
  102.     int i, last;
  103.     void swap(void *v[], int, int);
  104.     if (left >= right)              /* do nothing if array contains */
  105.         return;                     /* fewer than two elements */
  106.     swap(v, left, (left + right)/2);
  107.     last = left;
  108.     for (i = left+1; i <= right; i++)
  109.         if ((*comp)(v[i], v[left]) < 0)
  110.             swap(v, ++last, i);
  111.     swap(v, left, last);
  112.     my_qsort(v, left, last-1, comp);
  113.     my_qsort(v, last+1, right, comp);
  114. }
  115.  
  116. int charcmp(char *s, char *t)
  117. {
  118.     char a,b;
  119.     int fold= (option & FOLD) ? 1:0;
  120.     int dir = (option & DIR) ? 1:0;
  121.  
  122.     do
  123.     {
  124.         if(dir)
  125.         {
  126.             while(!isalnum(*s) && *s != ' ' && *s != '\0')
  127.                 s++;
  128.             while(!isalnum(*t) && *t != ' ' && *t != '\0')
  129.                 t++;
  130.         }
  131.         a = fold ? tolower(*s):*s;
  132.         s++;
  133.         b = fold ? tolower(*t):*t;
  134.         t++;
  135.         if (a==b && a=='\0')
  136.             return 0;
  137.     } while(a==b);
  138.  
  139.     return a-b;
  140. }
  141.  
  142. /* numcmp: compare s1 and s2 numerically */
  143. int numcmp(char *s1, char *s2)
  144. {
  145.     double v1, v2;
  146.     v1 = atof(s1);
  147.     v2 = atof(s2);
  148.     if (v1 < v2)
  149.         return -1;
  150.     else if (v1 > v2)
  151.         return 1;
  152.     else
  153.         return 0;
  154. }
  155.  
  156. void swap(void *v[],int i, int j)
  157. {
  158.     void *temp;
  159.  
  160.     temp = v[i];
  161.     v[i] = v[j];
  162.     v[j] = temp;
  163. }
  164.  
  165. #define ALLOCSIZE 10000
  166. static char allocbuf[ALLOCSIZE];
  167. static char *allocp = allocbuf;
  168.  
  169. char *alloc(int n)
  170. {
  171.     if(ALLOCSIZE + allocbuf - allocp>= n )
  172.     {
  173.         allocp += n;
  174.         return allocp - n;
  175.     }
  176.     else
  177.         return 0;
  178. }
  179.  
  180. size_t get_line(char* f_line,int n)
  181. {
  182.     int c;
  183.     size_t size=(size_t)f_line;
  184.  
  185.     while(((size_t)f_line-size)<n && (c=getchar())!=EOF && c!='\n')
  186.         *f_line++=c;
  187.  
  188.     if(c=='\n')
  189.         *(f_line++)=c;
  190.  
  191.     *(f_line)='\0';
  192.  
  193.     return (size_t)f_line-size;
  194. }
word count: 603

Link:
BBcode:
HTML:
Hide post links
Show post links

Who is online

Users browsing this forum: No registered users and 2 guests