[Exercise] Exercise 5-14. Modify the sort program to handle a -r flag 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-14. Modify the sort program to handle a -r flag page 121

Post by BJT » Sun May 07, 2017 7:40 pm

Exercise 5-14. Modify the sort program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r works with -n.

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

input

Code: Select all

./cbook -n -r
12
3
4
57
89
453
23423
6757
895643
435
79
234
656
437
output

Code: Select all

895643
23423
6757
656
453
437
435
234
89
79
57
12
4
3
input

Code: Select all

./c -n
1323
45
676
43
65
435
8546
345
87
6543
657
67
64
output

Code: Select all

43
45
64
65
67
87
345
435
657
676
1323
6543
8546
word count: 568

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

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

Re: Exercise 5-14. Modify the sort program to handle a -r flag page 121

Post by BJT » Thu May 11, 2017 1:20 am

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

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

New Topic Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 1 guest