開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Xcode - Apple
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(データ構造と動的メモリ - 架け橋を築く)、自分で考えてみよう(p. 289)を取り組んでみる。
自分で考えてみよう(p. 289)
//
// main.c
// sample1
//
// Created by kamimura on 2018/02/11.
// Copyright © 2018 kamimura. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Island {
char *name;
char *opens;
char *closes;
struct Island *next;
} Island;
Island *Island_new(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 Island_free(Island *start) {
Island *i = start;
Island *next = NULL;
for (; i != NULL; i = next) {
next = i->next;
free(i->name);
free(i);
}
}
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 *start = NULL;
Island *i = NULL;
Island *next = NULL;
char name[80];
for (;fgets(name, 80, stdin) != NULL; i = next) {
next = Island_new(name);
if (start == NULL) {
start = next;
}
if (i != NULL) {
i->next = next;
}
}
IslandDisplay(start);
Island_free(start);
return 0;
}
入出力結果(Terminal)
ab cd 名前: ab 営業時間: 09:00-17:00 名前: cd 営業時間: 09:00-17:00 Program ended with exit code: 0
0 コメント:
コメントを投稿