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 (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の2.5章(小さなツールの作成: 1つのことだけをうまくやる)、ピザの一切れ(p.150)を解いてみる。
その他参考書籍
- 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.150)
コード(BBEdit, Emacs)
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
char *delivery = "";
int thick = 0;
char ch;
while ((ch = getopt(argc, argv, "d:t")) != EOF)
if (ch == 'd') delivery = optarg;
else if (ch == 't') thick = 1;
else {
fprintf(stderr, "Unknown option: '%s'\n", optarg);
return 1;
}
argc -= optind;
argv += optind;
if (thick) printf("Thick crust.\n");
if (delivery[0]) printf("To be delivered %s.\n", delivery);
printf("Ingredients:\n");
for (int count = 0; count < argc; count++)
printf("%s\n", argv[count]);
}
π
入出力結果(Terminal)
p$ crun.sh sample150 clang ... Ingredients: $ ./sample150 cheese tomato Ingredients: cheese tomato $ ./sample150 -d '15:00' cheese tomato To be delivered 15:00. Ingredients: cheese tomato $ ./sample150 -d '15:00' -t cheese tomato Thick crust. To be delivered 15:00. Ingredients: cheese tomato $ ./sample150 -td '15:00' cheese tomato Thick crust. To be delivered 15:00. Ingredients: cheese tomato $
0 コメント:
コメントを投稿