Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成)、ピザパズル(ピザの一切れ(p.150))を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
ピザパズル(ピザの一切れ(p.150))
コード(BBEdit, Emacs)
order_pizza.c
#include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { char *delivery = ""; int thick = 0; int count = 0; char ch; while ((ch = getopt(argc, argv, "d:t")) != EOF) switch (ch) { case 'd': delivery = optarg; break; case 't': thick = 1; break; default: fprintf(stderr, "Unknown option: '%s'\n", optarg); return 1; } argc -= optind; argv += optind; if (thick) puts("Thick crust."); if (delivery[0]) printf("to be delivered %s.\n", delivery); puts("ingredients:"); for (count = 0; count < argc; ++count) puts(argv[count]); return (0); }
Makefile
all: order_pizza order_pizza: order_pizza.c cc -g -o order_pizza order_pizza.c clean: rm order_pizza
入出力結果(Terminal)
$ make && ./order_pizza cc -g -o order_pizza order_pizza.c ingredients: $ make && ./order_pizza cc -g -o order_pizza order_pizza.c ingredients: $ ./order_pizza cheese ingredients: cheese $ ./order_pizza cheese tomato ingredients: cheese tomato $ ./order_pizza -d ./order_pizza: option requires an argument -- d Unknown option: '(null)' $ ./order_pizza -d now to be delivered now. ingredients: $ ./order_pizza -d now cheese to be delivered now. ingredients: cheese $ ./order_pizza -d now cheese tomato to be delivered now. ingredients: cheese tomato $ ./order_pizza t ingredients: t $ ./order_pizza -t Thick crust. ingredients: $ ./order_pizza -t cheese Thick crust. ingredients: cheese $ ./order_pizza -t cheese tomato Thick crust. ingredients: cheese tomato $ ./order_pizza -d now -t Thick crust. to be delivered now. ingredients: $ ./order_pizza -d now -t cheese Thick crust. to be delivered now. ingredients: cheese $ ./order_pizza -d now -t cheese tomato Thick crust. to be delivered now. ingredients: cheese tomato $
0 コメント:
コメントを投稿