2014年5月15日木曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅤ部(言語のその他の機能)の25章(STL(Standard Template Library))、25.8(プログラミング実習)、実習 25-4.を解いてみる。

その他参考書籍

実習 25-4.

コード(BBEdit, Emacs)

count_words.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <set>
#include <map>

static std::map<std::string, int> word_counts;
static std::set<std::string> words;

static void p(std::string word)
{
  std::map<std::string, int>::const_iterator word_loc;
  word_loc = word_counts.find(word);
  std::cout << std::setiosflags(std::ios::left) << std::setw(50) << word;
  std::cout << std::resetiosflags(std::ios::left);
  std::cout << std::setiosflags(std::ios::right) << std::setw(4) <<
    word_loc->second << '\n';
  std::cout << std::resetiosflags(std::ios::right);
}

int main(int argc, char *argv[])
{
  std::string word;
  int count;
  std::ifstream in_file;
  std::map<std::string, int>::const_iterator word_loc;

  if (argc != 2) {
    std::cerr << "Usage: <filename>\n";
    exit (8);
  }

  in_file.open(argv[1]);
  if (in_file.fail()) {
    std::cerr << "Unable to open " << argv[1] << '\n';
    exit (8);
  }

  while (!in_file.eof()) {
    in_file >> word;
    word_loc = word_counts.find(word);
    if (word_loc == word_counts.end()) {
      words.insert(word);
      word_counts.insert(std::pair<std::string, int>(word, 1));
    }
    else {
      count = word_loc->second + 1;
      word_counts.erase(word);
      word_counts.insert(std::pair<std::string, int>(word, count));
    }
  }

  std::for_each(words.begin(), words.end(), p);
  
  return (0);
}

Makefile

CC=g++
CFLAGS=-g -Wall
SRC=count_words.cpp
OBJ=count_words.o

all: count_words

count_words: $(OBJ)
 ${CC} ${CFLAGS} -o count_words $(OBJ)

count_words.o: count_words.cpp
 ${CC} $(CFLAGS) -c count_words.cpp

clean:
 rm count_words count_words.o

入出力結果(Terminal)

$ make
g++ -g -Wall -c count_words.cpp
g++ -g -Wall -o count_words count_words.o
$ ./count_words count_words.cpp
!=                                                   1
"                                                    1
"Unable                                              1
"Usage:                                              1
#include                                             7
'\n';                                                2
(!in_file.eof())                                     1
(0);                                                 1
(8);                                                 2
(argc                                                1
(in_file.fail())                                     1
(word_loc                                            1
*argv[])                                             1
+                                                    1
1));                                                 1
1;                                                   1
2)                                                   1
<<                                                  13
<cstdlib>                                            1
<filename>\n";                                       1
<fstream>                                            1
<iomanip>                                            1
<iostream>                                           1
<map>                                                1
<set>                                                1
<string>                                             1
=                                                    3
==                                                   1
>>                                                   1
argc,                                                1
argv[1]                                              1
char                                                 1
count                                                1
count));                                             1
count;                                               1
else                                                 1
exit                                                 2
if                                                   3
in_file                                              1
in_file.open(argv[1]);                               1
in_file;                                             1
int                                                  2
int>                                                 1
int>(word,                                           2
int>::const_iterator                                 2
main(int                                             1
open                                                 1
p(std::string                                        1
p);                                                  1
return                                               1
static                                               3
std::cerr                                            2
std::cout                                            4
std::for_each(words.begin(),                         1
std::ifstream                                        1
std::map<std::string,                                3
std::resetiosflags(std::ios::left);                  1
std::resetiosflags(std::ios::right);                 1
std::set<std::string>                                1
std::setiosflags(std::ios::left)                     1
std::setiosflags(std::ios::right)                    1
std::setw(4)                                         1
std::setw(50)                                        1
std::string                                          1
to                                                   1
void                                                 1
while                                                1
word)                                                1
word;                                                3
word_counts.end())                                   1
word_counts.erase(word);                             1
word_counts.find(word);                              2
word_counts.insert(std::pair<std::string,            2
word_counts;                                         1
word_loc                                             2
word_loc->second                                     2
word_loc;                                            2
words.end(),                                         1
words.insert(word);                                  1
words;                                               1
{                                                    7
}                                                    8
$ 

0 コメント:

コメントを投稿