2013年5月13日月曜日

開発環境

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

その他参考書籍

演習 5-15.

コード

sample.c

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

#define MAXLINES 5000
#define MAXLEN 1000
#define MAXSTOR 5000
#define ALLOCSIZE 10000
#define NUMERIC 1
#define REVERSE 2
#define FOLD 4

int readlines(char *lineptr[], int nlines);
int my_getline(char *, int);
char *alloc(int);
void my_qsort(void *lineptr[], int left, int right,
           int (*comp)(void *, void*));
int numcmp(char *, char *);
int fold_strcmp(char *, char *);
void swap(void *v[], int, int);
char allocbuf[ALLOCSIZE];
char *allocp = allocbuf;

int main(int argc, char *argv[])
{
    char *lineptr[MAXLINES];
    int nlines;
    char options = 0;
    char option;
    int i;
    char c;
    
    while (--argc > 0 && (*++argv)[0] == '-')
        while (c = *++argv[0])
            switch (c) {
                case 'n':
                    options |= NUMERIC;
                    break;
                case 'r':
                    options |= REVERSE;
                    break;
                case 'f':
                    options |= FOLD;
                    break;
                default:
                    printf("error: option %c\n", c);
                    break;
            }
    if ((nlines = readlines(lineptr, MAXLINES)) > 0) {
        if (options & NUMERIC)
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*)(void *, void *)) numcmp);
        else if (options & FOLD)
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*) (void *, void *)) fold_strcmp);
        else
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*) (void *, void *)) strcmp);
        if (options & REVERSE)
            for (i = nlines - 1; i >= 0; i--)
                printf("%s\n", lineptr[i]);
        else
            for (i = 0; i < nlines; i++)
                printf("%s\n", lineptr[i]);
        return 0;
    } else {
        printf("input too big to sort\n");
        return 1;
    }
}

void my_qsort(void *v[], int left, int right,
           int (*cmp)(void *, void *))
{
    int i, last;
    
    if (left >= right)
        return;
    swap(v, left, (left + right) / 2);
    last = left;
    for (i = left+1; i <= right; i++)
        if ((*cmp)(v[i], v[left]) < 0)
            swap(v, ++last, i);
    swap(v, left, last);
    my_qsort(v, left, last-1, cmp);
    my_qsort(v, last + 1, right, cmp);
}

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;
}

int numcmp(char *s1, char *s2)
{
    double v1, v2;
    
    v1 = atof(s1);
    v2 = atof(s2);
    if (v1 < v2)
        return -1;
    else if (v1 > v2)
        return 1;
    else
        return 0;
}

int fold_strcmp(char *s, char *t)
{
    for (; tolower(*s) == tolower(*t); s++, t++)
        if (*s == '\0')
            return 0;
    return tolower(*s) - tolower(*t);
}
void swap(void *v[], int i, int j)
{
    void *temp;
    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

入出力結果(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
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!
$ cat sample.txt | ./a.ou
-bash: ./a.ou: No such file or directory
$ 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
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!
$ 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
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 -f
11
12
13
14
15
17
18
19
20
Ah Love! could you and I with Fate conspire
ah love! could you and i with fate conspire
Re-mould it nearer to the Heart's Desire!
re-mould it nearer to the heart's desire!
To grasp this sorry Scheme of Things entire,
to grasp this sorry scheme of things entire,
would not we shatter it to bits -- and then
Would not we shatter it to bits -- and then
$ cat sample.txt | ./a.out -fr
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
20
19
18
17
15
14
13
12
11
$ cat sample.txt | ./a.out -rf
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
20
19
18
17
15
14
13
12
11
$ cat sample.txt | ./a.out -f -r
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
20
19
18
17
15
14
13
12
11
$ cat sample.txt | ./a.out -r -f
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
20
19
18
17
15
14
13
12
11
$

演習 5-16.

コード

sample.c

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

#define MAXLINES 5000
#define MAXLEN 1000
#define MAXSTOR 5000
#define ALLOCSIZE 10000
#define NUMERIC 1
#define REVERSE 2
#define FOLD 4
#define DIR 8

int readlines(char *lineptr[], int nlines);
int my_getline(char *, int);
char *alloc(int);
void my_qsort(void *lineptr[], int left, int right,
           int (*comp)(void *, void*));
int numcmp(char *, char *);
int fold_strcmp(char *, char *);
int dir_strcmp(char *, char *);
int fold_dir_strcmp(char *, char *);
void swap(void *v[], int, int);
char allocbuf[ALLOCSIZE];
char *allocp = allocbuf;

int main(int argc, char *argv[])
{
    char *lineptr[MAXLINES];
    int nlines;
    char options = 0;
    char option;
    int i;
    char c;
    
    while (--argc > 0 && (*++argv)[0] == '-')
        while (c = *++argv[0])
            switch (c) {
                case 'n':
                    options |= NUMERIC;
                    break;
                case 'r':
                    options |= REVERSE;
                    break;
                case 'f':
                    options |= FOLD;
                    break;
                case 'd':
                    options |= DIR;
                    break;
                default:
                    printf("error: option %c\n", c);
                    break;
            }

    if ((nlines = readlines(lineptr, MAXLINES)) > 0) {
        if (options & NUMERIC)
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*)(void *, void *)) numcmp);
        else if (options & FOLD && options & DIR)
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*) (void *, void *)) fold_dir_strcmp);     
        else if (options & FOLD)
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*) (void *, void *)) fold_strcmp);
        else if (options & DIR)
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*) (void *, void *)) dir_strcmp);       
        else
            my_qsort((void **) lineptr, 0, nlines - 1,
                  (int (*) (void *, void *)) strcmp);
        if (options & REVERSE)
            for (i = nlines - 1; i >= 0; i--)
                printf("%s\n", lineptr[i]);
        else
            for (i = 0; i < nlines; i++)
                printf("%s\n", lineptr[i]);
        return 0;
    } else {
        printf("input too big to sort\n");
        return 1;
    }
}

void my_qsort(void *v[], int left, int right,
           int (*cmp)(void *, void *))
{
    int i, last;
    
    if (left >= right)
        return;
    swap(v, left, (left + right) / 2);
    last = left;
    for (i = left+1; i <= right; i++)
        if ((*cmp)(v[i], v[left]) < 0)
            swap(v, ++last, i);
    swap(v, left, last);
    my_qsort(v, left, last-1, cmp);
    my_qsort(v, last + 1, right, cmp);
}

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;
}

int numcmp(char *s1, char *s2)
{
    double v1, v2;
    
    v1 = atof(s1);
    v2 = atof(s2);
    if (v1 < v2)
        return -1;
    else if (v1 > v2)
        return 1;
    else
        return 0;
}

int fold_strcmp(char *s, char *t)
{
    for (; tolower(*s) == tolower(*t); s++, t++)
        if (*s == '\0')
            return 0;
    return tolower(*s) - tolower(*t);
}

int dir_strcmp(char *s, char *t)
{
    do {    
        while (!(isalnum(*s) || *s == ' ' || *s == '\0'))
            s++;
        while (!(isalnum(*t) || *t == ' ' || *t == '\0'))
            t++;
        if (*s == *t && *s == '\0')
            return 0;
    } while (*s++ == *t++);
    return *--s - *--t;
}

int fold_dir_strcmp(char *s, char *t)
{
    char a, b;
    do {    
        while (!(isalnum(*s) || *s == ' ' || *s == '\0'))
            s++;
        while (!(isalnum(*t) || *t == ' ' || *t == '\0'))
            t++;
        a = tolower(*s);
        b = tolower(*t);
        s++;
        t++;
        if (a == b && a == '\0')
            return 0;
    } while (a == b);
    return a - b;
}

void swap(void *v[], int i, int j)
{
    void *temp;
    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

入出力結果(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!
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!
$ cat sample.txt | ./a.out
!@#$%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 | ./a.out -d
!@#$%Ah Love! could you and I with Fate conspire
Ah Love! could you and I with Fate conspire
Re-mould it nearer to the Heart's Desire!
!@#$%Re-mould it nearer to the Heart's Desire!
To grasp this sorry Scheme of Things entire,
!@#$%To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
!@#$%Would not we shatter it to bits -- and then
ah love! could you and i with fate conspire
!@#$%ah love! could you and i with fate conspire
re-mould it nearer to the heart's desire!
!@#$%re-mould it nearer to the heart's desire!
!@#$%to grasp this sorry scheme of things entire,
to grasp this sorry scheme of things entire,
!@#$%would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
$ cat sample.txt | ./a.out -df
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
!@#$%ah love! could you and i with fate conspire
!@#$%Ah Love! could you and I with Fate conspire
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%re-mould it nearer to the heart's desire!
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%to grasp this sorry scheme of things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%would not we shatter it to bits -- and then
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
$ cat sample.txt | ./a.out -fd
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
!@#$%ah love! could you and i with fate conspire
!@#$%Ah Love! could you and I with Fate conspire
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%re-mould it nearer to the heart's desire!
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%to grasp this sorry scheme of things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%would not we shatter it to bits -- and then
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
$ cat sample.txt | ./a.out -d -f
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
!@#$%ah love! could you and i with fate conspire
!@#$%Ah Love! could you and I with Fate conspire
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%re-mould it nearer to the heart's desire!
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%to grasp this sorry scheme of things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%would not we shatter it to bits -- and then
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
$ cat sample.txt | ./a.out -f -d
ah love! could you and i with fate conspire
Ah Love! could you and I with Fate conspire
!@#$%ah love! could you and i with fate conspire
!@#$%Ah Love! could you and I with Fate conspire
re-mould it nearer to the heart's desire!
Re-mould it nearer to the Heart's Desire!
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%re-mould it nearer to the heart's desire!
to grasp this sorry scheme of things entire,
To grasp this sorry Scheme of Things entire,
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%to grasp this sorry scheme of things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%would not we shatter it to bits -- and then
Would not we shatter it to bits -- and then
would not we shatter it to bits -- and then
$

0 コメント:

コメントを投稿