diff --git a/ch17/README.md b/ch17/README.md index 568ebaf9..fbea6d8d 100644 --- a/ch17/README.md +++ b/ch17/README.md @@ -90,4 +90,22 @@ Because `std::accumulate`'s third parameter is the initial value of the sum. It' > Write an integral object that contains the correct answers for the true/false quiz. Use it to generate grades on the quiz for the data structure from the previous two exercises. -[Test `QuizResponses` class](ex17_11_quiz_responses_test.cpp) \ No newline at end of file +[Test `QuizResponses` class](ex17_11_quiz_responses_test.cpp) + +## Exercise 17.14 + +> Write several regular expressions designed to trigger various errors. Run your program to see what output your compiler generates for each error. + +[Try error code 1](ex17_14.cpp) + +## Exercise 17.15 + +> Write a program using the pattern that finds words that violate the “i before e except after c” rule. Have your program prompt the user to supply a word and indicate whether the word is okay or not. Test your program with words that do and do not violate the rule. + +[I before E except after C rule](ex17_15.cpp) + +## Exercise 17.16 + +> What would happen if your regex object in the previous program were initialized with "[^c]ei"? Test your program using that pattern to see whether your expectations were correct. + +`[^c]ei` says we want any such letter that is followed by the letters 'ei', This pattern describes strings containing exactly **three** characters. The test words in [ex17_15](ex17_15.cpp) will all fail. \ No newline at end of file diff --git a/ch17/ex17.14.15.16/main.cpp b/ch17/ex17.14.15.16/main.cpp deleted file mode 100644 index ebc80d94..00000000 --- a/ch17/ex17.14.15.16/main.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Queequeg - * @date 19 Nov 2014 - * @remark This code is for the exercises from C++ Primer 5th Edition - * @note There are some bugs in gcc(include the latest version 4.9.2) - * to handle regular expression.To compile this program, please - * turn to other compilers such as msvs2013 and clang. - ***************************************************************************/ -//! -//! Exercise 17.14 -//! Write several regular expressions designed to trigger various errors. -//! Run your program to see what output your compiler generates for each error. - -//! Exercise 17.15 -//! Write a program using the pattern that finds word that violate the -//! "i before e except after c" rule. Have your program prompt the user to -//! supply a word and indicate whether the word is okay or not. Test your -//! program with words that do and do not violate the rule. - -//! Exercise 17.16 -//! What would happen if your regex object in the previous program were -//! initialized with "[^c]ei"? Test your program using that pattern to see -//! whether your expectations were correct. - - -#include -using std::cout; -using std::cin; -using std::endl; - -#include -using std::string; - -#include -using std::regex; -using std::regex_error; - -int main() -{ - //! for ex17.14 - //! error_brack - try{ - regex r("[[:alnum:]+\\.(cpp|cxx|cc)$", regex::icase); - } - catch(regex_error e) - { - cout << e.what() << " code: " << e.code() << endl; - } - - //! for ex17.15 - regex r("[[:alpha:]]*[^c]ei[[:alpha:]]*", regex::icase); - string s; - cout << "Please input a word! Input 'q' to quit!" << endl; - while(cin >> s && s != "q") - { - if(std::regex_match(s, r)) - cout << "Input word " << s << " is okay!" << endl; - else - cout << "Input word " << s << " is not okay!" <> s && s != "q") - { - if(std::regex_match(s, r)) - cout << "Input word " << s << " is okay!" << endl; - else - cout << "Input word " << s << " is not okay!" < +#include +#include +#include + +using std::regex; + +int main() +{ + try { + regex r("[[:alnum]+\\.(cpp|cxx|cc)$", regex::icase); + } + catch (std::regex_error e) { + std::cout << e.what() << "\ncode:" << e.code() << std::endl; + } +} + +// output +// Unexpected end of character class. +// code:1 \ No newline at end of file diff --git a/ch17/ex17_15.cpp b/ch17/ex17_15.cpp new file mode 100644 index 00000000..dc5a243b --- /dev/null +++ b/ch17/ex17_15.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +using std::regex; + +//! @check https://en.wikipedia.org/wiki/I_before_E_except_after_C +int main() +{ + try { + regex r("[[:alpha:]]*(cie|[^c]ei)[[:alpha:]]*"); + std::string word; + while (std::cout << "Enter a word, or q to quit: ", std::cin >> word) { + if (word == "q") break; + std::smatch result; + if (std::regex_search(word, result, r)) + std::cout << "Violate the rule: 'i before e except after c'" + << std::endl; + else + std::cout << word + " is okay." << std::endl; + } + } + catch (std::regex_error e) { + std::cout << e.what() << "\ncode:" << e.code() << std::endl; + } +} + +//! @test +// Enter a word, or q to quit: believe +// believe is okay. +// Enter a word, or q to quit: receive +// receive is okay. +// Enter a word, or q to quit: species +// Violate the rule: 'i before e except after c' +// Enter a word, or q to quit: seize +// Violate the rule: 'i before e except after c' +// Enter a word, or q to quit: q \ No newline at end of file