2013年6月25日火曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.9(文字配列)の演習1-16を解いてみる。

その他参考書籍

演習 1-16.

コード

sample.c

#include <stdio.h>
#define MAXLINE 1000

int my_getline(char line[], int maxline);
void copy(char to[], char from[]);

int main()
{
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];
    
    max = 0;
    
    printf("行の長さと行のテキスト\n");
    while ((len = my_getline(line, MAXLINE)) > 0) {
        printf("%2d: %s", len, line);
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    }
    putchar('\n');
    if (max > 0) {
        printf("一番長い行: %s\n", longest);
    }
    return 0;
}

int my_getline(char s[], int lim)
{
    int c, i;
    
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
        s[i] = c;
    }
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

void copy(char to[], char from[])
{
    int i;
    
    i = 0;
    while ((to[i] = from[i]) != '\0') {
        ++i;
    }
}

入出力結果(Terminal)

$ ./a.out < sample.c
行の長さと行のテキスト
19: #include <stdio.h>
21: #define MAXLINE 1000
 1: 
42: int my_getline(char line[], int maxline);
35: void copy(char to[], char from[]);
 1: 
11: int main()
 2: {
13:     int len;
13:     int max;
24:     char line[MAXLINE];
27:     char longest[MAXLINE];
 5:     
13:     max = 0;
 5:     
51:     printf("行の長さと行のテキスト\n");
52:     while ((len = my_getline(line, MAXLINE)) > 0) {
38:         printf("%2d: %s", len, line);
25:         if (len > max) {
23:             max = len;
33:             copy(longest, line);
10:         }
 6:     }
19:     putchar('\n');
19:     if (max > 0) {
50:         printf("一番長い行: %s\n", longest);
 6:     }
14:     return 0;
 2: }
 1: 
34: int my_getline(char s[], int lim)
 2: {
14:     int c, i;
 5:     
75:     for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
18:         s[i] = c;
 6:     }
21:     if (c == '\n') {
18:         s[i] = c;
13:         ++i;
 6:     }
17:     s[i] = '\0';
14:     return i;
 2: }
 1: 
34: void copy(char to[], char from[])
 2: {
11:     int i;
 5:     
11:     i = 0;
40:     while ((to[i] = from[i]) != '\0') {
13:         ++i;
 6:     }
 1: }
一番長い行:     for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {

$ ./a.out < sample.txt
行の長さと行のテキスト
44: Ah Love! could you and I with Fate conspire
45: To grasp this sorry Scheme of Things entire,
44: Would not we shatter it to bits -- and then
42: Re-mould it nearer to the Heart's Desire!
44: ah love! could you and i with fate conspire
45: to grasp this sorry scheme of things entire,
44: would not we shatter it to bits -- and then
42: re-mould it nearer to the heart's desire!
49: !@#$%Ah Love! could you and I with Fate conspire
50: !@#$%To grasp this sorry Scheme of Things entire,
49: !@#$%Would not we shatter it to bits -- and then
47: !@#$%Re-mould it nearer to the Heart's Desire!
49: !@#$%ah love! could you and i with fate conspire
50: !@#$%to grasp this sorry scheme of things entire,
49: !@#$%would not we shatter it to bits -- and then
47: !@#$%re-mould it nearer to the heart's desire!

一番長い行: !@#$%To grasp this sorry Scheme of Things entire,

$

0 コメント:

コメントを投稿