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.
1 parent
55ddacb
commit 0764f1d
Showing
1 changed file
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*************************************************************************** | ||
* @file main.cpp | ||
* @author Queequeg | ||
* @date 19 Nov 2014 | ||
* @remark This code is for the exercises from C++ Primer 5th Edition | ||
* @note | ||
***************************************************************************/ | ||
//! | ||
//! 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 <iostream> | ||
using std::cout; | ||
using std::cin; | ||
using std::endl; | ||
|
||
#include<string> | ||
using std::string; | ||
|
||
#include <regex> | ||
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:]]*e[[:alpha:]]*i[[:alpha:]]*c[[: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; | ||
} | ||
return 0; | ||
} |