forked from Mooophy/Cpp-Primer
-
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
1 changed file
with
60 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,60 @@ | ||
/*************************************************************************** | ||
* @file main.cpp | ||
* @author Queequeg | ||
* @date 26 Nov 2014 | ||
* @remark This code is for the exercises from C++ Primer 5th Edition | ||
* @note | ||
***************************************************************************/ | ||
//! | ||
//! Exercise 17.21 | ||
//! Rewrite your phone number program from 8.3.2 (p. 323) to use the | ||
//! valid function defined in this section. | ||
|
||
#include<iostream> | ||
using std::cerr; | ||
using std::cout; | ||
using std::cin; | ||
using std::endl; | ||
using std::istream; | ||
using std::ostream; | ||
|
||
#include<fstream> | ||
using std::ifstream; | ||
using std::ofstream; | ||
|
||
#include<sstream> | ||
using std::istringstream; | ||
using std::ostringstream; | ||
|
||
#include<string> | ||
using std::string; | ||
|
||
#include<vector> | ||
using std::vector; | ||
|
||
#include <regex> | ||
using std::regex; | ||
using std::sregex_iterator; | ||
using std::regex_error; | ||
|
||
int main() { | ||
try { | ||
regex reg("(\\d{4})?([-])?(\\d{5})"); | ||
string str; | ||
while (getline(cin, str)) { | ||
for (sregex_iterator b(str.cbegin(), str.cend(), reg), e; b != e; ++b) { | ||
if (!(*b)[1].matched) // if if there is only the last 5 digits of index | ||
cout << b->str(3); // show them | ||
else | ||
cout << b->str(); // show index entirely | ||
cout << '\t'; | ||
} | ||
} | ||
} | ||
|
||
catch (regex_error re) { | ||
cout << re.what() << endl << re.code() << endl; | ||
} | ||
|
||
return 0; | ||
} |