[Code example] Pointer to functions page 119,120,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

Pointer to functions page 119,120,121

Post by BJT » Sun May 07, 2017 6:49 pm

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

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

Who is online

Users browsing this forum: No registered users and 2 guests