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

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-03-22 11:18:18

  1. /*
  2.    k&r(5.6): Pointer Arrays
  3.    created on Mar 22, 2011
  4.    */
  5. #include "stdio.h"
  6. #include "string.h"

  7. #define MAXLINES 5000 /* max #lines to be sorted */

  8. char *lineptr[MAXLINES]; /*pointers to text lines */

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

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

  12. /* sort input lines */
  13. main()
  14. {
  15.     int nlines; /* number of input lines read */

  16.     if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
  17.         qsort (lineptr, 0, nlines-1);
  18.         writelines(lineptr, nlines);
  19.         return 0;
  20.     }
  21.     else {
  22.         printf("error: input too big to sort\n");
  23.         return 1;
  24.     }
  25. }

  26. #define MAXLEN 1000 /* max length of any input line */
  27. int getline1(char *, int);
  28. char *alloc(int);

  29. /* readlines: read input lines */
  30. int readlines(char *lineptr[], int maxlines)
  31. {
  32.     int len, nlines;
  33.     char *p, line[MAXLEN];

  34.     nlines = 0;
  35.     while ((len = getline1(line, MAXLEN)) > 0)
  36.      if (nlines >= maxlines || (p = alloc(len)) == NULL)
  37.         return -1;
  38.      else {
  39.          line[len-1] = '\0'; /* delete newline */
  40.          strcpy(p, line);
  41.          lineptr[nlines++] = p;
  42.      }
  43.     return nlines;
  44. }

  45. /* writelines: write output lines */
  46. void writelines(char *lineptr[], int nlines)
  47. {
  48.     int i;

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

  51. }

  52. /* qsort: sort v[left]...v[right] into increasing order */
  53. void qsort(char *v[], int left, int right)
  54. {
  55.     int i, last;
  56.     void swap(char *v[], int i, int j);

  57.     if (left >= right) /* do nothing if array contains */
  58.      return; /* fewer than two elements */
  59.     swap (v, left, (left+right)/2);
  60.     last = left;
  61.     for (i = left+1; i <= right; i++)
  62.      if (strcmp(v[i], v[left]) < 0)
  63.         swap(v, ++last, i);
  64.     swap (v, left, last); /* restore partition elem */
  65.     qsort (v, left, last-1);
  66.     qsort (v, last+1, right);
  67. }

  68. /* swap: interchange v[i] and v[j] */
  69. void swap (char *v[], int i, int j)
  70. {
  71.     char *temp;
  72.     temp = v[i];
  73.     v[i] = v[j];
  74.     v[j] = temp;
  75. }

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


  88. #define ALLOCSIZE 10000

  89. static char allocbuf[ALLOCSIZE];
  90. static char *allocp = allocbuf;

  91. char *alloc(int n) /* return pointer to n characters */
  92. {
  93.     if (allocbuf + ALLOCSIZE - allocp >= n) {
  94.         allocp += n;
  95.         return allocp - n;
  96.     } else
  97.      return 0;
  98. }
阅读(430) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~