From cd2b94820ff91d435df37927f888d0d72a201218 Mon Sep 17 00:00:00 2001 From: pezy-pc Date: Thu, 18 Dec 2014 20:24:26 +0800 Subject: [PATCH] fixed bug that if value is null --- ch11/README.md | 1 + ch11/ex11_33.cpp | 117 ++++++++++++++--------------------------------- 2 files changed, 36 insertions(+), 82 deletions(-) diff --git a/ch11/README.md b/ch11/README.md index a6c7e795..539934c5 100644 --- a/ch11/README.md +++ b/ch11/README.md @@ -99,3 +99,4 @@ std::pair>::iterator, bool> // return ## [Exercise 11.27 ~ 11.30](ex11_27_28_29_30.cpp) ## [Exercise 11.31](ex11_31.cpp) ## [Exercise 11.32](ex11_32.cpp) +## [Exercise 11.33](ex11_33.cpp) diff --git a/ch11/ex11_33.cpp b/ch11/ex11_33.cpp index f80d1934..7cf78aed 100644 --- a/ch11/ex11_33.cpp +++ b/ch11/ex11_33.cpp @@ -1,95 +1,48 @@ -//! @Alan -//! -//! -//! Exercise 11.33: -//! Implement your own version of the word-transformation program. -// I used one function to for this exercise. It proved that -// less functions are harder to debug. Besides, the comment style shoud be polished. -//! +// +// ex11_33.cpp +// Exercise 11.33 +// +// Created by pezy on 12/18/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Implement your own version of the word-transformation program. -#include -#include #include #include -#include -#include +#include +#include #include -//! -//! \brief wy_word_transformation for ex 11.33 -//! \param rulesFileName const string& -//! \param inputFileName const string& -//! -void -wy_word_transformation(const std::string &rulesFileName, const std::string &inputFileName); -int main() +using std::string; using std::ifstream; + +std::map buildMap(ifstream &map_file) { - wy_word_transformation("rules.txt","original.txt"); - return 0; + std::map trans_map; + for (string key, value; map_file >> key && getline(map_file, value); ) + if (value.size() > 1) trans_map[key] = value.substr(1).substr(0, value.find_last_not_of(' ')); + return trans_map; } - -void -wy_word_transformation(const std::string &rulesFileName, const std::string &inputFileName) +const string & transform(const string &s, const std::map &m) { - //! Build rules map - std::ifstream fin(rulesFileName); - std::string line; - std::map rules_map; - - while(std::getline(fin,line)) - { - std::string key, value; - key = line.substr(0, line.find(' ')); - //! ^^^^^^^^^ - //! @note - //! Suppose the line is - // where r u - //! ^ - //! 012345 - //! the statement becomes - // line.substr(0,5) - //! ^ - //! which means returnning a new string using 5 characters starting at 0. - - value = line.substr(line.find(' ') + 1); - //! @note ^^^ - //! the + 1 here increment the index so from the space to next character. - //! size_t n is the second parameter whose defualt value is npos. If leave - //! it as default, it means "until the end of the string" + auto map_it = m.find(s); + return map_it == m.cend() ? s : map_it->second; +} - //! add the key-value pair into the map. - rules_map[key] = value; +void word_transform(ifstream &map, ifstream &input) +{ + auto trans_map = buildMap(map); + for (string text; getline(input, text); ) { + std::istringstream iss(text); + for (string word; iss >> word; ) + std::cout << transform(word, trans_map) << " "; + std::cout << std::endl; } - fin.close(); - - - //! Read the input file ,transform it and print - fin.open(inputFileName); - std::string text; - - while(std::getline(fin, text)) - { - std::istringstream stream(text); -//! ^^^^^^^^^^^^^^^^^^ - - std::string word; - while(stream >> word) -//! ^^^^^^^^^^^^^^ -//! @attention -//! std::istringstream is quite a handy way to handle spaces in a string, far more convinient -//! than generic algorithm or search a space manually. - - { - //! obtain the iterator using the key given - std::map::const_iterator - it = rules_map.find(word); +} - //! check if the give key is found in the map - std::cout << (it == rules_map.end() ? word +" " : it->second + " "); - //! ^ ^ - //! @attention the parentheses are necessay here. - } - std::cout <<"\n"; - } +int main() +{ + ifstream ifs_map("../data/word_transformation.txt"), ifs_content("../data/given_to_transform.txt"); + if (ifs_map && ifs_content) word_transform(ifs_map, ifs_content); + else std::cerr << "can't find the documents." << std::endl; }