2018年2月11日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(データ構造と動的メモリ - 架け橋を築く)、コードマグネット(p. 273)を取り組んでみる。

コードマグネット(p. 273)

//
//  main.c
//  sample1
//
//  Created by kamimura on 2018/02/11.
//  Copyright © 2018 kamimura. All rights reserved.
//

#include <stdio.h>

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

void IslandDisplay(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, const char * argv[]) {
    Island aminty = {.name="アミティ", .opens="09:00", .closes="17:00", .next=NULL};
    Island craggy = {.name="クラッギー", .opens="09:00", .closes="17:00", .next=NULL};
    Island isla_nublar = {.name="イスラヌブラル", .opens="09:00", .closes="17:00", .next=NULL};
    Island shutter = {.name="シャッター", .opens="09:00", .closes="17:00", .next=NULL};
    aminty.next = &craggy;
    craggy.next = &isla_nublar;
    isla_nublar.next = &shutter;
    
    IslandDisplay(&aminty);
    puts("");
    Island skull = {.name="スカル", .opens="09:00", .closes="17:00", .next=NULL};
    isla_nublar.next = &skull;
    skull.next = &shutter;
    
    IslandDisplay(&aminty);
    return 0;
}

入出力結果(Terminal)

名前: アミティ 営業時間: 09:00-17:00
名前: クラッギー 営業時間: 09:00-17:00
名前: イスラヌブラル 営業時間: 09:00-17:00
名前: シャッター 営業時間: 09:00-17:00

名前: アミティ 営業時間: 09:00-17:00
名前: クラッギー 営業時間: 09:00-17:00
名前: イスラヌブラル 営業時間: 09:00-17:00
名前: スカル 営業時間: 09:00-17:00
名前: シャッター 営業時間: 09:00-17:00
Program ended with exit code: 0

0 コメント:

コメントを投稿