Skip to content

Commit

Permalink
finished 17.3.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jul 2, 2017
1 parent 6d38263 commit f8d6920
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 82 deletions.
20 changes: 19 additions & 1 deletion ch17/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
[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.
81 changes: 0 additions & 81 deletions ch17/ex17.14.15.16/main.cpp

This file was deleted.

20 changes: 20 additions & 0 deletions ch17/ex17_14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <exception>
#include <iostream>
#include <regex>
#include <string>

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
38 changes: 38 additions & 0 deletions ch17/ex17_15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <exception>
#include <iostream>
#include <regex>
#include <string>

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

0 comments on commit f8d6920

Please sign in to comment.