Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- LLVM/Clang (コンパイラ, Xcode - Apple)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の6章(データ構造と動的メモリ: 架け橋を築く)、大きな疑問(p.305)を解いてみる。
その他参考書籍
- 21st Century C: C Tips from the New School
- プログラミング言語C 第2版 ANSI規格準拠(B.W. カーニハンD.M. リッチー(著)、石田 晴久(翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版(Steve Oualline(著)、望月 康司 (監訳)(翻訳)、谷口 功(翻訳)、 オライリージャパン)
大きな疑問(p.305)
コード(BBEdit, Emacs)
spies.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char *question;
struct node *no;
struct node *yes;
} node;
int yes_no(char *question)
{
char answer[3];
printf("%s? (y/n): ", question);
fgets(answer, 3, stdin);
return answer[0] == 'y';
}
node* create(char *question)
{
node *n = malloc(sizeof(node));
n->question = strdup(question);
n->no = NULL;
n->yes = NULL;
return n;
}
void release(node *n)
{
if (n) {
if (n->no)
release(n->no);
if (n->yes)
release(n->yes);
if (n->question)
free(n->question);
free(n);
}
}
int main()
{
char question[100];
char suspect[100];
node *start_node = create("容疑者は髭を生やしているか");
start_node->no = create("ロレッタ・バーンスワース");
start_node->yes = create("ベニー・ザ・スプーン");
node *current;
do {
current = start_node;
while (1) {
if (yes_no(current->question)) {
if (current->yes)
current = current->yes;
else {
printf("容疑者判明\n");
break;
}
} else if (current->no)
current = current->no;
else {
printf("容疑者は誰? ");
fgets(suspect, 100, stdin);
suspect[strlen(suspect) - 1] = '\0';
node *yes_node = create(suspect);
current->yes = yes_node;
node *no_node = create(current->question);
current->no = no_node;
printf("%sには当てはまり、%sには当てはまらない質問は? ",
suspect, current->question);
fgets(question, 100, stdin);
free(current->question);
current->question = strdup(question);
break;
}
}
} while (yes_no("再実行しますか? "));
release(start_node);
}
Ubuntuを使用。
入出力結果(Terminal)
$ make spies cc -g -Wall spies.c -o spies spies.c: In function ‘main’: spies.c:81:1: warning: control reaches end of non-void function [-Wreturn-type] } ^ $ valgrind --leak-check=full ./spies ==4337== Memcheck, a memory error detector ==4337== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==4337== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info ==4337== Command: ./spies ==4337== 容疑者は髭を生やしているか? (y/n): n ロレッタ・バーンスワース? (y/n): n 容疑者は誰? ハイデン・ファントゥッチ ハイデン・ファントゥッチには当てはまり、ロレッタ・バーンスワースには当てはまらない質問は? 顔に傷があるか 再実行しますか? ? (y/n): n ==4337== ==4337== HEAP SUMMARY: ==4337== in use at exit: 37 bytes in 1 blocks ==4337== total heap usage: 11 allocs, 10 frees, 325 bytes allocated ==4337== ==4337== 37 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==4337== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==4337== by 0x4EC02B9: strdup (strdup.c:42) ==4337== by 0x4007FA: create (spies.c:22) ==4337== by 0x4008CD: main (spies.c:47) ==4337== ==4337== LEAK SUMMARY: ==4337== definitely lost: 37 bytes in 1 blocks ==4337== indirectly lost: 0 bytes in 0 blocks ==4337== possibly lost: 0 bytes in 0 blocks ==4337== still reachable: 0 bytes in 0 blocks ==4337== suppressed: 0 bytes in 0 blocks ==4337== ==4337== For counts of detected and suppressed errors, rerun with: -v ==4337== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) $ make spies cc -g -Wall spies.c -o spies spies.c: In function ‘main’: spies.c:82:1: warning: control reaches end of non-void function [-Wreturn-type] } ^ $ valgrind --leak-check=full ./spies ==4357== Memcheck, a memory error detector ==4357== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==4357== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info ==4357== Command: ./spies ==4357== 容疑者は髭を生やしているか? (y/n): n ロレッタ・バーンスワース? (y/n): n 容疑者は誰? ハイデン・ファントゥッチ ハイデン・ファントゥッチには当てはまり、ロレッタ・バーンスワースには当てはまらない質問は? 顔に傷があるか 再実行しますか? ? (y/n): n ==4357== ==4357== HEAP SUMMARY: ==4357== in use at exit: 0 bytes in 0 blocks ==4357== total heap usage: 11 allocs, 11 frees, 325 bytes allocated ==4357== ==4357== All heap blocks were freed -- no leaks are possible ==4357== ==4357== For counts of detected and suppressed errors, rerun with: -v ==4357== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) $
0 コメント:
コメントを投稿