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 (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数)、エクササイズ(p.321)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
エクササイズ(p.321)
コード(BBEdit, Emacs)
sample321.c
#include <stdio.h>
#include <string.h>
int NUM_ADS = 7;
char *ADS[] = {
"William: SBM GSHO likes sports, TV, dining",
"Matt: SUM NS likes art, movies, theater",
"Luis: SLM ND likes books, theater, art",
"Mike: DWM DS likes trucks, sports and bieber",
"Peter: SAM likes chess, working out and art",
"Josh: SJM likes sports, movies and theater",
"Jed: DBM likes theater, books and dining"
};
int sports_no_bieber(char *s)
{
return strstr(s, "sports") && !strstr(s, "bieber");
}
int sports_or_workout(char *s)
{
return strstr(s, "sports") || strstr(s, "working out");
}
int ns_theater(char *s)
{
return strstr(s, "NS") && strstr(s, "theater");
}
int arts_theater_or_dining(char *s)
{
return strstr(s, "art") || strstr(s, "theater") || strstr(s, "dining");
}
void find(int (*match)(char*))
{
int i;
puts("検索結果:");
puts("------------------------------------");
for (i = 0; i < NUM_ADS; i++) {
if (match(ADS[i])) {
printf("%s\n", ADS[i]);
}
}
puts("------------------------------------");
}
int main(int argc, char *argv[])
{
printf("sports_no_bieber\n");
find(sports_no_bieber);
printf("sports_or_workout\n");
find(sports_or_workout);
printf("ns_theater\n");
find(ns_theater);
printf("arts_theater_or_dining\n");
find(arts_theater_or_dining);
return (0);
}
Makefile
sample321: sample321.c cc -g -o sample321 sample321.c clean: rm sample321
入出力結果(Terminal)
$ make && ./sample321 cc -g -o sample321 sample321.c sports_no_bieber 検索結果: ------------------------------------ William: SBM GSHO likes sports, TV, dining Josh: SJM likes sports, movies and theater ------------------------------------ sports_or_workout 検索結果: ------------------------------------ William: SBM GSHO likes sports, TV, dining Mike: DWM DS likes trucks, sports and bieber Peter: SAM likes chess, working out and art Josh: SJM likes sports, movies and theater ------------------------------------ ns_theater 検索結果: ------------------------------------ Matt: SUM NS likes art, movies, theater ------------------------------------ arts_theater_or_dining 検索結果: ------------------------------------ William: SBM GSHO likes sports, TV, dining Matt: SUM NS likes art, movies, theater Luis: SLM ND likes books, theater, art Peter: SAM likes chess, working out and art Josh: SJM likes sports, movies and theater Jed: DBM likes theater, books and dining ------------------------------------ $
0 コメント:
コメントを投稿