2014年2月9日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の6章(データ構造と動的メモリ)、プールパズル(p.287)を解いてみる。

その他参考書籍

プールパズル(p.287)

コード(BBEdit, Emacs)

sample287.c

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

typedef struct island{
    char *name;
    char *opens;
    char *closes;
    struct island *next;
} island;

island* create(char *name)
{
    island *i = malloc(sizeof(island));
    i->name = strdup(name);
    i->opens = "09:00";
    i->closes = "17:00";
    i->next = NULL;
    return i;
}

void display(island *start)
{
    island *i = start;
    for (; i != NULL; i = i->next) {
        printf("名前:%s営業時間:%s-%s\n", i->name, i->opens, i->closes);
    }
}

int main(int argc, char *argv[])
{
    island *start = NULL;
    island *i = NULL;
    island *next = NULL;
    char name[80];
    for (; fgets(name, 80, stdin) != NULL; i = next) {
        next = create(name);
        if (start == NULL) {
            start = next;
        }
        if (i != NULL) {
            i->next = next;
        }
    }
    display(start);
    return (0);
}

Makefile

sample287: sample287.c
 cc -g -o sample287 sample287.c

clean:
 rm sample287

入出力結果(Terminal)

$ ./sample287
アトランティス
ティッチマーシュ島
名前:アトランティス
営業時間:09:00-17:00
名前:ティッチマーシュ島
営業時間:09:00-17:00
名前:
営業時間:09:00-17:00
$

0 コメント:

コメントを投稿