2013年7月12日金曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第3章(制御の流れ)3.4(Switch)の演習3-2を解いてみる。

その他参考書籍

演習 3-2.

コード

sample.c

#include <stdio.h>
#define MAXLENGTH 10000

void escape(char s[], char t[]);
void unescape(char s[], char t[]);

int main()
{
    char s[MAXLENGTH], t[MAXLENGTH], v[MAXLENGTH];
    int c, i;
    
    i = 0;
    
    while ((c = getchar()) != EOF) {
        s[i] = c;
        i++;
    }
    
    escape(s, t);
    unescape(t, v);
    printf("%s\n", s);
    printf("escape**********\n");
    printf("%s\n", t);
    printf("unescape**********\n");
    printf("%s\n", v);
    return 0;
}

void escape(char s[], char t[])
{
    int i, j;
    
    for (i = 0, j = 0; s[i] != '\0'; i++, j++) {
        switch (s[i]) {
            case '\n':
                t[j++] = '\\';
                t[j] = 'n';
                break;
            case '\t':
                t[j++] = '\\';
                t[j] = 't';
                break;
            default:
                t[j] = s[i];
                break;
        }
    }
    t[j] = '\0';
}

void unescape(char s[], char t[])
{
    int i, j;
    for(i = 0, j = 0; s[i] != '\0'; i++, j++) {
        switch (s[i]) {
            case '\\':
                switch (s[++i]) {
                    case 'n':
                        t[j] = '\n';
                        break;
                    case 't':
                        t[j] = '\t';
                        break;
                    default:
                        t[j++] = '\\';
                        t[j] = s[i];
                        break;
                }
                break;
            default:
                t[j] = s[i];
                break;
        }
    }
    t[j] = '\0';
}

入出力結果(Terminal)

$ ./a.out < 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!

escape**********
Ah Love! could you and I with Fate conspire\nTo grasp this sorry Scheme of Things entire,\nWould not we shatter it to bits -- and then\nRe-mould it nearer to the Heart's Desire!\nah love! could you and i with fate conspire\nto grasp this sorry scheme of things entire,\nwould not we shatter it to bits -- and then\nre-mould it nearer to the heart's desire!\n!@#$%Ah Love! could you and I with Fate conspire\n!@#$%To grasp this sorry Scheme of Things entire,\n!@#$%Would not we shatter it to bits -- and then\n!@#$%Re-mould it nearer to the Heart's Desire!\n!@#$%ah love! could you and i with fate conspire\n!@#$%to grasp this sorry scheme of things entire,\n!@#$%would not we shatter it to bits -- and then\n!@#$%re-mould it nearer to the heart's desire!\n
unescape**********
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!

$

0 コメント:

コメントを投稿