Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220975
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-03-23 10:00:29

  1. /* k&r5.11: Pointers to Functions
  2.    created on Mar 23, 2011
  3.    */
  4. #include "stdio.h"
  5. #include "string.h"

  6. #define MAXLINES 5000 /* max #lines to be sorted */
  7. char *lineptr[MAXLINES]; /* pointers to text lines */

  8. int readlines(char *lineptr[], int nlines);
  9. void writelines(char *lineptr[], int nlines);

  10. void qsort(void *lineptr[], int left, int right,
  11.             int (*comp) (void *, void *));
  12. int numcmp(char *, char *);

  13. /* sort input lines */
  14. main(int argc, char *argv[])
  15. {
  16.     int nlines; /* number of input lines read */
  17.     int numeric = 0; /* 1 if numberic sort */

  18.     if (argc > 1 && strcmp(argv[1], "-n") == 0)
  19.      numeric = 1;
  20.     if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
  21.         qsort((void**) lineptr, 0, nlines-1,
  22.                     (int (*)(void*, void *)) (numeric ? numcmp : strcmp));
  23.         writelines(lineptr, nlines);
  24.         return 0;
  25.     } else {
  26.         printf("input too big to sort\n");
  27.         return 1;
  28.     }
  29. }

  30. /* qsort: sort v[left]...v[right] into increasing order */
  31. void qsort (void *v[], int left, int right,
  32.             int (*comp)(void *, void *))
  33. {
  34.     int i, last;

  35.     void swap(void *v[], int, int);
  36.     if (left >= right)
  37.      return;
  38.     swap (v, left, (left+right)/2);
  39.     last = left;
  40.     for (i = left+1; i <= right; i++)
  41.      if ((*comp)(v[i], v[left]) < 0)
  42.         swap(v, ++last, i);
  43.     swap(v, left, last);
  44.     qsort(v, left, last-1, comp);
  45.     qsort(v, last+1, right, comp);
  46. }

  47. /* swap: interchage tow elements */
  48. void swap(void *v[], int i, int j)
  49. {
  50.     void *temp;
  51.     temp = v[i];
  52.     v[i] = v[j];
  53.     v[j] = temp;
  54. }




  55. #define MAXLEN 1000 /* max length of any input line */
  56. int getline1(char *, int);
  57. char *alloc(int);

  58. /* readlines: read input lines */
  59. int readlines(char *lineptr[], int maxlines)
  60. {
  61.     int len, nlines;
  62.     char *p, line[MAXLEN];

  63.     nlines = 0;
  64.     while ((len = getline1(line, MAXLEN)) > 0)
  65.      if (nlines >= maxlines || (p = alloc(len)) == NULL)
  66.         return -1;
  67.      else {
  68.          line[len-1] = '\0'; /* delete newline */
  69.          strcpy(p, line);
  70.          lineptr[nlines++] = p;
  71.      }
  72.     return nlines;
  73. }

  74. /* writelines: write output lines */
  75. void writelines(char *lineptr[], int nlines)
  76. {
  77.     int i;

  78.     for (i = 0; i < nlines; i++)
  79.      printf("%s\n", lineptr[i]);

  80. }


  81. int getline1(char *s, int lim)
  82. {
  83.     int i,c;
  84.    for(i = 0; i < lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
  85.      s[i] = c;
  86.    if (c == '\n') {
  87.      s[i] = c;
  88.      ++i;
  89.     }
  90.     s[i] = '\0';
  91.     return i;
  92. }

  93. #define ALLOCSIZE 10000

  94. static char allocbuf[ALLOCSIZE];
  95. static char *allocp = allocbuf;

  96. char *alloc(int n) /* return pointer to n characters */
  97. {
  98.     if (allocbuf + ALLOCSIZE - allocp >= n) {
  99.         allocp += n;
  100.         return allocp - n;
  101.     } else
  102.      return 0;
  103. }

  104. /* numcmp.c */
  105. #include "stdlib.h"

  106. /* numcmp: compare s1 and s2 numerically */
  107. int numcmp(char *s1, char *s2)
  108. {
  109.     double v1, v2;

  110.     v1 = atof(s1);
  111.     v2 = atof(s2);
  112.     if (v1 < v2)
  113.      return -1;
  114.     else if (v1 > v2)
  115.      return 1;
  116.     else
  117.      return 0;
  118. }
阅读(571) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~