forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |