2014年5月27日火曜日

開発環境

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

その他参考書籍

エクササイズ(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_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");
}

void find(int (*match)(char*))
{
  int i;
  
  puts("検索結果:");
  puts("------------------------------------");

  for (i = 0; i < NUM_ADS; ++i)
    if (match(ADS[i]))
      puts(ADS[i]);
      
  puts("------------------------------------");
}

int main(int argc, char *argv[])
{
  int i;
  /* 関数ポインタの配列を作成出来るか試してみる */
  int (*matches[3])(char*) =
    {sports_or_workout, ns_theater, arts_theater_or_dining};

  for (i = 0; i < 3; ++i)
    find(matches[i]);
  
  puts("人物一覧");
  for (i = 0; i < NUM_ADS; ++i)
    puts(ADS[i]);
  
  puts("スポーツかトレーニング好きな人");
  find(sports_or_workout);
  
  puts("演劇の好きな煙草を吸わない人");
  find(ns_theater);

  puts("芸術、演劇、または食べることが好きな人");
  find(arts_theater_or_dining);
  
  return (0);
}

Makefile

CC=cc
CFLAGS = -g -Wall
SRC=sample321.c
OBJ=sample321.o

all: sample321

sample321: $(OBJ)
 $(CC) $(CFLAGS) -o sample321 $(OBJ)

sample321.o: sample321.c
 $(CC) $(CFLAGS) -c sample321.c

clean:
 rm -rf sample321 sample321.o

入出力結果(Terminal)

$ make && ./sample321
cc -g -Wall -c sample321.c
cc -g -Wall -o sample321 sample321.o
検索結果:
------------------------------------
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
Josh: SJM likes sports, movies and theater
Jed: DBM likes theater, books and dining
------------------------------------
検索結果:
------------------------------------
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
------------------------------------
人物一覧
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
スポーツかトレーニング好きな人
検索結果:
------------------------------------
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
Josh: SJM likes sports, movies and theater
Jed: DBM likes theater, books and dining
------------------------------------
演劇の好きな煙草を吸わない人
検索結果:
------------------------------------
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 コメント:

コメントを投稿