2018年4月13日金曜日

開発環境

Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数 - 関数を最大限に活用する)、コードマグネット(p. 313)を取り組んでみる。

コードマグネット(p. 313)

Makefile

CC = cc

all: run

sample: sample.c
 $(CC) sample.c -o sample

run: sample
 ./sample
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int NUM_ADS = 7;
char *ADS[] = {
  "William: SBM GSOH likes sports, TV, dining",
  "Matt: SWM 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"
};

void find() {
  puts("検索結果:");
  puts("--------------------------------------------------");
  for (int i = 0; i < NUM_ADS; i++) {
    char *AD = ADS[i];
    if(strstr(AD, "sports") && !strstr(AD, "bieber")) {
      puts(AD);
    }
  }
  puts("--------------------------------------------------");
}

int main() {
  find();
}

入出力結果(Terminal)

$ make
cc sample.c -o sample
./sample
検索結果:
--------------------------------------------------
William: SBM GSOH likes sports, TV, dining
Josh: SJM likes sports, movies and theater
--------------------------------------------------
$

0 コメント:

コメントを投稿