2014年4月29日火曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅣ部(高度なプログラミング概念)の21章(高度なクラス)、21.8(プログラミング実習)、実習 21-2.を解いてみる。

その他参考書籍

実習 21-2.

コード(BBEdit, Emacs)

string_matcher.cpp

#include <iostream>
#include <cstring>

class string_matcher {
private:
  char data[100];
public:
  string_matcher () {
    std::strcpy(data, "");
  }
  string_matcher (const char *s) {
    std::strcpy(data, s);
  }
  bool match(const char *s);
};

class word_num_empty_matcher : public string_matcher {
public:
  word_num_empty_matcher (const char *s) : string_matcher(s) {}
  bool match_word(const char *s);
  bool match_num(int num);
  bool match_empty();
};

inline bool string_matcher::match(const char *s)
{
  return std::strcmp(data, s) == 0;
}

inline bool word_num_empty_matcher::match_word(const char *s)
{
  return match(s);
}

inline bool word_num_empty_matcher::match_num(int num)
{
  char s[2];

  s[0] = '0' + num;
  s[1] = '\0';
  
  return match(s);
}

inline bool word_num_empty_matcher::match_empty()
{
  return match("");
}

int main(int argc, char *argv[])
{
  word_num_empty_matcher s1("cpp");
  word_num_empty_matcher s2("5");
  word_num_empty_matcher s3("");
  word_num_empty_matcher ary[] = {s1, s2, s3};
  int i;

  for (i = 0; i < 3; ++i) {
    std::cout << ary[i].match_word("cpp") << ", " << ary[i].match_num(5) <<
      ", " << ary[i].match_empty() << std::endl;
  }
    
  return (0);
}

Makefile

CC=g++
CFLAGS=-g -Wall
all: string_matcher

string_matcher: string_matcher.cpp
 ${CC} ${CFLAGS} -o string_matcher string_matcher.cpp

clean:
 rm string_matcher

入出力結果(Terminal)

$ make && ./string_matcher
g++ -g -Wall -o string_matcher string_matcher.cpp
1, 0, 0
0, 1, 0
0, 0, 1
$

0 コメント:

コメントを投稿