|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include "my_io.h" |
| 5 | +#include "alloc.h" |
| 6 | + |
| 7 | +#define MAXLINES 5000 |
| 8 | +char *lineptr[MAXLINES]; |
| 9 | + |
| 10 | +void _qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *), int order); |
| 11 | + |
| 12 | +int numcmp(char *, char *); |
| 13 | +int strcmpi(char *, char *); |
| 14 | + |
| 15 | +int main(int argc, char **argv) { |
| 16 | + int numeric = 0; |
| 17 | + //int reverse = -1; |
| 18 | + int order = 1; |
| 19 | + int ignore_case = 0; |
| 20 | + char *name = *argv; |
| 21 | + char c; |
| 22 | + /*if (argc > 1 && strcmp(*++argv, "-n") == 0) |
| 23 | + numeric = 1;*/ |
| 24 | + while (--argc > 0 && **++argv == '-') { |
| 25 | + while (c = *++(*argv)) |
| 26 | + switch(c) { |
| 27 | + case 'n': |
| 28 | + numeric = 1; |
| 29 | + break; |
| 30 | + case 'r': |
| 31 | + order *= -1; |
| 32 | + break; |
| 33 | + case 'f': |
| 34 | + ignore_case = 1; |
| 35 | + break; |
| 36 | + default: |
| 37 | + printf("%s: illegal character %c\n", name, c); |
| 38 | + argc = -1; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + if (argc != 0) { |
| 43 | + printf("Usage: %s -n -r\n" |
| 44 | + "n: numeric sort\n" |
| 45 | + "r: reverse sort\n" |
| 46 | + "f : fold (ignore case)\n", name); |
| 47 | + return 0; |
| 48 | + } |
| 49 | + //return 0; |
| 50 | + int nlines; |
| 51 | + if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { |
| 52 | + _qsort((void **) lineptr, 0, nlines-1, (int (*)(void*, void*))(numeric ? numcmp : ((ignore_case) ? strcmpi : strcmp)), order); |
| 53 | + writelines(lineptr, nlines); |
| 54 | + return 0; |
| 55 | + } else { |
| 56 | + printf("input too big to sort\n"); |
| 57 | + return 1; |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +/* qsort: sort v[left]...v[right] into increasing order */ |
| 63 | +void _qsort(void *v[], int left, int right, int (*comp)(void *, void *), int order) { |
| 64 | + int i, last; |
| 65 | + void swap(void *v[], int, int); |
| 66 | + |
| 67 | + if (left >= right) return; |
| 68 | + swap(v, left, (left + right) / 2); |
| 69 | + last = left; |
| 70 | + for (i = left + 1 ; i <= right ; i++) |
| 71 | + if ((order)*(*comp)(v[i], v[left]) < 0) |
| 72 | + swap(v, ++last, i); |
| 73 | + swap(v, left, last); |
| 74 | + _qsort(v, left, last - 1, comp, order); |
| 75 | + _qsort(v, last + 1, right, comp, order); |
| 76 | +} |
| 77 | + |
| 78 | +/* numcmp: compare s1 and s2 numerically */ |
| 79 | +int numcmp(char *s1, char *s2) { |
| 80 | + double v1, v2; |
| 81 | + |
| 82 | + v1 = atof(s1); |
| 83 | + v2 = atof(s2); |
| 84 | + if (v1 < v2) |
| 85 | + return -1; |
| 86 | + else if (v1 > v2) |
| 87 | + return 1; |
| 88 | + else |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +int strcmpi(char *s1, char *s2) { |
| 93 | + void strupr(char *); |
| 94 | + |
| 95 | + char v1[1024], v2[1024]; |
| 96 | + strcpy(v1, s1); |
| 97 | + strupr(v1); |
| 98 | + strcpy(v2, s2); |
| 99 | + strupr(v2); |
| 100 | + |
| 101 | + return strcmp(v1, v2); |
| 102 | +} |
| 103 | + |
| 104 | +void strupr(char *s) { |
| 105 | + while(*s != '\0') { |
| 106 | + if (*s >= 'a' && *s <= 'z') |
| 107 | + *s = 'A' + (*s-'a'); |
| 108 | + s++; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void swap(void *v[], int i, int j) { |
| 113 | + void *temp; |
| 114 | + temp = v[i], v[i] = v[j], v[j] = temp; |
| 115 | +} |
0 commit comments