2013年5月12日日曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第5章(ポインタと配列)、5.11(関数へのポインタ)、演習5-14を解いてみる。

その他参考書籍

演習 5-14.

コード

sample.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLINES 5000
#define MAXLEN 1000
#define MAXSTOR 5000
#define ALLOCSIZE 10000

void tail(int argc, char *argv[]);
int readlines(char *lineptr[], int nlines);
int my_getline(char *, int);
char *alloc(int);
char allocbuf[ALLOCSIZE];
char *allocp = allocbuf;

int main(int argc, char *argv[])
{
    tail(argc, argv);
    return 0;
}

void tail(int argc, char *argv[])
{
    int n, nlines, i;
    char *lineptr[MAXLINES];
    if (argc == 1 || (*++argv)[0] != '-') {
        n = 10;
    } else
        n = atoi(&(*argv)[1]);
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
        if (n > nlines)
            n = nlines;
        for (i = nlines - n; i < nlines; i++)
            printf("%2d行目、%2d行: %s\n", (n + 1) - (nlines - i), i + 1, lineptr[i]);
    } else
        printf("error: input too big\n");
}

int readlines(char *lineptr[], int maxlines)
{
    int len, nlines;
    char *p, line[MAXLEN];
    nlines = 0;
    while ((len = my_getline(line, MAXLEN)) > 0) {
        if (nlines >= maxlines || (p = alloc(len)) == NULL)
            return -1;
        else {
            line[len - 1] = '\0';
            strcpy(p, line);
            lineptr[nlines++] = p;
        }
    }
    return nlines;
}

int my_getline(char *s, int lim)
{
    int c;
    char *t = s;
    
    while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
        *s++ = c;
    if (c == '\n')
        *s++ = c;
    *s = '\0';
    return s - t;
}

char *alloc(int n)
{
    if (allocbuf + ALLOCSIZE - allocp >= n) {
        allocp += n;
        return allocp - n;
    } else 
        return 0;
}

入出力結果(Terminal)

$ 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!
20
11
19
12
18
13
17
14
15
$ cat sample.txt | ./a.out
11
12
13
14
15
17
18
19
20
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 | ./a.out -r
Would not we shatter it to bits -- and then
To grasp this sorry Scheme of Things entire,
Re-mould it nearer to the Heart's Desire!
Ah Love! could you and I with Fate conspire
20
19
18
17
15
14
13
12
11
$ cat sample.txt | ./a.out -n
Ah Love! could you and I with Fate conspire
Would not we shatter it to bits -- and then
To grasp this sorry Scheme of Things entire,
Re-mould it nearer to the Heart's Desire!
11
12
13
14
15
17
18
19
20
$ cat sample.txt | ./a.out -r -n
20
19
18
17
15
14
13
12
11
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
$ cat sample.txt | ./a.out -n -r
20
19
18
17
15
14
13
12
11
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
$ cat sample.txt | ./a.out -nr
20
19
18
17
15
14
13
12
11
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
$ cat sample.txt | ./a.out -rn
20
19
18
17
15
14
13
12
11
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
$ cat sample.txt | ./a.out -a
error: option a
11
12
13
14
15
17
18
19
20
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
$

0 コメント:

コメントを投稿