Skip to content

Commit

Permalink
fixed bug that if value is null
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 18, 2014
1 parent fcfc3d9 commit cd2b948
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 82 deletions.
1 change: 1 addition & 0 deletions ch11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ std::pair<std::map<std::string, std::vector<int>>::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)
117 changes: 35 additions & 82 deletions ch11/ex11_33.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <fstream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>

//!
//! \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<string, string> buildMap(ifstream &map_file)
{
wy_word_transformation("rules.txt","original.txt");
return 0;
std::map<string, string> 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<string, string> &m)
{
//! Build rules map
std::ifstream fin(rulesFileName);
std::string line;
std::map<std::string, std::string> 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<std::string, std::string>::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;
}

0 comments on commit cd2b948

Please sign in to comment.