2015年4月27日月曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数: 関数を最大限に活用する)、エクササイズ(p.321)を解いてみる。

その他参考書籍

エクササイズ(p.321)

コード(BBEdit, Emacs)

#include <stdio.h>
#include <string.h>

#include "sample321.h"

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");
}
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");
}
typedef int (*a_fn_type)(char *);
void find(a_fn_type match) {
  printf("検索結果:\n"
         "---------------------------------------------------------------------"
         "-----------\n");
  for (int i = 0; i < NUM_ADS; i++)
    if (match(ADS[i]))
      printf("%s\n", ADS[i]);
  printf("---------------------------------------------------------------------"
         "-----------\n");
}
int main() {
  a_fn_type matches[] = {sports_no_bieber, sports_or_workout, ns_theater,
                         arts_theater_or_dining};

  for (int i = 0; i < 4; i++)
    find(matches[i]);
}

入出力結果(Terminal)

p
$ crun.sh sample321
clang ...
検索結果:
--------------------------------------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Josh: SJM likes sports, movies and theater
--------------------------------------------------------------------------------
検索結果:
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
検索結果:
--------------------------------------------------------------------------------
Matt: SUM NS likes art, movies, theater
--------------------------------------------------------------------------------
検索結果:
--------------------------------------------------------------------------------
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 コメント:

コメントを投稿