Practice 6: %F コマンドの実装(検索)

今回から,プログラミング演習2に入ります.レポートの様式も変更となります.改めて,課題解説書の確認も忘れずに.

プログラミング演習1の内容が理解できていれば,検索 %F の実装は,比較的簡単なはず・・・?

解説「探索」を読んで,実装を考えてみましょう.解答例(前半後半) を見るのは,慎重に.

練習6-1 プロジェクトの再開

プログラミング演習2の課題に取り組むため,以下の準備をせよ.

  • 課題プログラムの確認

  • レポートの様式の確認と執筆準備

    • レポートテンプレートを手に入れる

      • ファイル名は,例えば,eop2_09B02999.texのように,学生番号を含む名前に変えておく

    • 課題説明資料を読み,「章立て」の構成となるよう,texファイルを書き換える

    • 正しい章立てで,pdfファイルが作成できていることを確認する

練習6-2 struct profile 構造体の各メンバに対する比較方法の検討

解説「探索」を読み,文字列wordstruct profile構造体の各メンバに対する比較方法を検討せよ.

初期検討においては,以下のテスト用コードを利用するとよい.コメントとして書かれている(1)~(5)を順に書き換え,コンパイルと実行を試してみるとよい.

(クリックするとテスト用コードを開きます)
Listing 18 cmdF-ex1.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/** cmdF-ex1.c
 * Practice code to help consideration for the command '%F'
 * see also: Answer of Exercise 3-3
 */
#define main nouse_main
/* (1) Change the filename to your source code. */
#include "./my-command.c"
#undef main

/* You can use the definitions and functions at your original source. */

int main(void) {
    int i;
    struct profile *p, test_profiles[] = {
        { 5100046, "", { 1845, 11, 2 }, "", "SEN Unit 2.0 Open"},
        { 5100127, "", { 1908, 1, 19 }, "", "01955 641225 Primary 25 2.6 Open"},
        /* { 1955, "", { 1955, 6, 4 }, "", "Did you check this profile data?"}, */
    };

    char *word, line[MAX_LINE_LEN+1];
    
    /* (5) Change here to obtain the 'word' by your get_line() function */
    strcpy(line, "01955 641225 Primary 25 2.6 Open");
    word = line;

    for(i=0; i<sizeof(test_profiles)/sizeof(test_profiles[0]); i++) {
        printf("i = %d\n", i);
        p = &test_profiles[i];

        /* Test id */
        printf("Word:'%s' equals Id:'%d'? --> ", word, p->id);
        if( 0 ) {   /* (2) How to compare char* vs int? */
            printf("Yes\n");
        } else {
            printf("No\n");
        }

        /* Test comment */
        printf("Word:'%s' equals Comment:'%s'? --> ", word, p->comment);
        if( 0 ) {   /* (3) How to compare char* vs char*? */
            printf("Yes\n");
        } else {
            printf("No\n");
        }

        /* Test birthday */
        /* (4) How to test it? */

    }

    printf("Finish.\n");
    return 0;
}

練習6-3 %Fコマンドの実装

これまで仮実装していたcmd_find()を完成させよ.

練習6-4 %Fコマンドのテスト

探索結果が正しいことを確認するためのテストを実施したい.自分の作成したコマンドがsample.csvから,正しく探索できているか確認する方法を示して,結果を確認せよ.

参考 Emacs, gcc, gdb を使いこなそう

参考資料です.時間に余裕がある人は,以下を読んで環境を作っておくと,いろいろ便利かもしれません.