Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Yosemite - Apple, Ubuntu (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang/LLVM (コンパイラ, Xcode - Apple)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信: お話は楽しい)、長いエクササイズ(p.460)を解いてみる。
その他参考書籍
- 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.460)
コード(BBEdit, Emacs)
sample460.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <errno.h> #include <signal.h> #include <catch_signal.h> #include <error.h> int score = 0; void end_game(int sig) { printf("\n最終得点:%i\n", score); exit(0); } void times_up(int sig) { puts("\n時間切れ!"); raise(SIGINT); } int main(int argc, char *argv[]) { if (catch_signal(SIGALRM, times_up) == -1) error("SIGALARMに設定できません。"); if (catch_signal(SIGINT, end_game) == -1) error("SIGINTにハンドラを設定できません。"); srandom (time (0)); while (1) { int a= random() % 11; int b = random() % 11; char txt[4]; alarm(5); printf("\n%iかける%iはいくつですか? ", a, b); fgets(txt, 4, stdin); int answer = atoi(txt); if (answer == a * b) score++; else printf("\n間違いです!得点:%i\n", score); } return 0; }
Makefile
P=sample460 CC=cc CFLAGS=-g -Wall # -O3 SRC= OBJ=~/root/object_files/*.o sample460.o LDLIBS= $(P): $(OBJ) $(CC) $(CFLAGS) $(LDLIBS) $(OBJ) -o $@ sample460.o: sample460.c $(CC) $(CFLAGS) -c sample460.c -o $@
入出力結果(Terminal)
$ make cc -g -Wall -c sample460.c -o sample460.o cc -g -Wall ~/root/object_files/*.o sample460.o -o sample460$ ./sample460 4かける6はいくつですか?24 1かける8はいくつですか?8 7かける1はいくつですか? 時間切れ! 最終得点:2 $ ./sample460 7かける4はいくつですか? 時間切れ! 最終得点:0 $ ./sample460 7かける3はいくつですか? C-c C-c 最終得点:0 $ ./sample460 0かける7はいくつですか?10 間違いです!得点:0 8かける7はいくつですか? 時間切れ! 最終得点:0 $
0 コメント:
コメントを投稿