2014年4月2日水曜日

開発環境

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

その他参考書籍

自分で考えてみよう(p.289)

コード(BBEdit, Emacs)

sample273.c

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

void release(island *start)
{
  island *i = start;
  island *next = NULL;

  for (; i != NULL; i = next) {
    next = i->next;
    free(i->name);
    free(i);
  }
}

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) {
    name[strlen(name) - 1] = '\0';
    next = create(name);
    if (start == NULL) {
      start = next;
    }
    if (i != NULL) {
      i->next = next;
    }
  }
  display(start);
  release(start);
  return (0);
}

sample287header.h

typedef struct island {
  char *name;
  char *opens;
  char *closes;
  struct island *next;
} island;
void display(island *start);
island* create(char *name);

Makefile

all: sample289

sample289: sample289.o sample287header.o
 cc -g -o sample289 sample289.o sample287header.o

sample289.o: sample289.c sample287header.h
 cc -c sample289.c

sample287header.o: sample287header.c sample287header.h
 cc -c sample287header.c

入出力結果(Terminal)

$ make && ./sample289
cc -c sample289.c
cc -c sample287header.c
cc -g -o sample289 sample289.o sample287header.o
アトランティス
ティッチマーシュ島
名前:アトランティス
営業時間:09:00-17:00
名前:ティッチマーシュ島
営業時間:09:00-17:00
$

0 コメント:

コメントを投稿