開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第5章(ポインタと配列)、5.6(ポインタ配列: ポインタへのポインタ)、演習5-7を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 5-7.
コード
sample.c
#include <stdio.h> #include <string.h> #define MAXLINES 5000 char *lineptr[MAXLINES]; int readlines(char *lineptr[], int, char *); void writelines(char *lineptr[], int); void qsort(char *lineptr[], int, int); int main() { int nlines; char linestor[MAXLINES]; if ((nlines = readlines(lineptr, MAXLINES, linestor)) >= 0) { qsort(lineptr, 0, nlines - 1); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } } #define MAXLEN 1000 #define MAXSTOR 10000 int my_getline(char *, int); int readlines(char *lineptr[], int maxlines, char *linestor) { int len, nlines; char line[MAXLEN]; char *p = linestor; char *linestop = linestor + MAXSTOR; nlines = 0; while ((len = my_getline(line, MAXLEN)) > 0) { if (nlines >= maxlines || p + len > linestop) { return -1; } else { line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; p += len; } } return nlines; } void writelines(char *lineptr[], int nlines) { while (nlines-- > 0) { printf("%s\n", *lineptr++); } } void qsort(char *v[], int left, int right) { int i, last; void swap(char *v[], int i, int j); if (left >= right) { return; } swap(v, left, (left + right) / 2); last = left; for (i = left + 1; i <= right; i++) { if (strcmp(v[i], v[left]) < 0) { swap(v, ++last, i); } } swap(v, left, last); qsort(v, left, last - 1); qsort(v, last + 1, right); } void swap(char *v[], int i, int j) { char *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; } int my_getline(char *s, int lim) { int c; char *t; t = s; while (--lim > 0 && (c = getchar()) != EOF && c != '\n') { *s++ = c; } if (c == '\n') { *s++ = c; } *s = '\0'; return s - t; }
入出力結果(Terminal)
$ ./a.out < sample.txt !@#$%Ah Love! could you and I with Fate conspire !@#$%Re-mould it nearer to the Heart's Desire! !@#$%To grasp this sorry Scheme of Things entire, !@#$%Would not we shatter it to bits -- and then !@#$%ah love! could you and i with fate conspire !@#$%re-mould it nearer to the heart's desire! !@#$%to grasp this sorry scheme of things entire, !@#$%would not we shatter it to bits -- and then Ah Love! could you and I with Fate conspire Re-mould it nearer to the Heart's Desire! To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then ah love! could you and i with fate conspire re-mould it nearer to the heart's desire! to grasp this sorry scheme of things entire, would not we shatter it to bits -- and then $ cat sample.txt Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! ah love! could you and i with fate conspire to grasp this sorry scheme of things entire, would not we shatter it to bits -- and then re-mould it nearer to the heart's desire! !@#$%Ah Love! could you and I with Fate conspire !@#$%To grasp this sorry Scheme of Things entire, !@#$%Would not we shatter it to bits -- and then !@#$%Re-mould it nearer to the Heart's Desire! !@#$%ah love! could you and i with fate conspire !@#$%to grasp this sorry scheme of things entire, !@#$%would not we shatter it to bits -- and then !@#$%re-mould it nearer to the heart's desire! $ ./a.out < sample.c line[len - 1] = '\0'; lineptr[nlines++] = p; p += len; return -1; strcpy(p, line); swap(v, ++last, i); *s++ = c; *s++ = c; if (nlines >= maxlines || p + len > linestop) { if (strcmp(v[i], v[left]) < 0) { printf("%s\n", *lineptr++); printf("error: input too big to sort\n"); qsort(lineptr, 0, nlines - 1); return 0; return 1; return; writelines(lineptr, nlines); } } } else { *s = '\0'; char *linestop = linestor + MAXSTOR; char *p = linestor; char *t; char *temp; char line[MAXLEN]; char linestor[MAXLINES]; for (i = left + 1; i <= right; i++) { if ((nlines = readlines(lineptr, MAXLINES, linestor)) >= 0) { if (c == '\n') { if (left >= right) { int c; int i, last; int len, nlines; int nlines; last = left; nlines = 0; qsort(v, last + 1, right); qsort(v, left, last - 1); return nlines; return s - t; swap(v, left, (left + right) / 2); swap(v, left, last); t = s; temp = v[i]; v[i] = v[j]; v[j] = temp; void swap(char *v[], int i, int j); while ((len = my_getline(line, MAXLEN)) > 0) { while (--lim > 0 && (c = getchar()) != EOF && c != '\n') { while (nlines-- > 0) { } } } } } } } } else { #define MAXLEN 1000 #define MAXLINES 5000 #define MAXSTOR 10000 #include <stdio.h> #include <string.h> char *lineptr[MAXLINES]; int main() int my_getline(char *, int); int my_getline(char *s, int lim) int readlines(char *lineptr[], int maxlines, char *linestor) int readlines(char *lineptr[], int, char *); void qsort(char *lineptr[], int, int); void qsort(char *v[], int left, int right) void swap(char *v[], int i, int j) void writelines(char *lineptr[], int nlines) void writelines(char *lineptr[], int); { { { { { { } } } } } } $
0 コメント:
コメントを投稿