第十七章 17.3.1节练习
2014-10-07 20:04
155 查看
练习17.14
编写几个正则表达式,分别触发不同错误。运行你的程序,观察编译器对每个错误的输出。
解答:
这个就不一一列举出来了。如果正则表达式写错,在运行时程序会在编译表达式的时候直接崩溃。
练习17.15
编写程序,使用模式查找违反“i在e之前,除非在c之后”规则的单词。你的程序应该提示用户输入一个单词,然后掷出此单词是够符合要求。用一些违反和未违反规则的单词测试你的程序。
解答:
练习17.16
如果前一题程序中的regex对象用“[^c]ei"进行初始化,将会发生什么?用词模式测试你的程序,检查你的答案是否正确。
解答:
编写几个正则表达式,分别触发不同错误。运行你的程序,观察编译器对每个错误的输出。
解答:
这个就不一一列举出来了。如果正则表达式写错,在运行时程序会在编译表达式的时候直接崩溃。
练习17.15
编写程序,使用模式查找违反“i在e之前,除非在c之后”规则的单词。你的程序应该提示用户输入一个单词,然后掷出此单词是够符合要求。用一些违反和未违反规则的单词测试你的程序。
解答:
#include <iostream> #include <regex> #include <string> using namespace std; int main(){ string pattern("[^c]ei"); pattern = "[[:alpha:]]+" + pattern + "[[:alpha:]]*"; regex r(pattern); smatch results; string test_str; while (cout << "\nPlease enter a word for test:" << endl, cin >> test_str){ if (regex_search(test_str, results, r)) cout << "this word fit the requirements." << endl; else cout << "this word don't fit the requirements." << endl; } }
练习17.16
如果前一题程序中的regex对象用“[^c]ei"进行初始化,将会发生什么?用词模式测试你的程序,检查你的答案是否正确。
解答:
#include <iostream> #include <regex> #include <string> using namespace std; int main(){ string pattern("[^c]ei"); pattern = "[[:alpha:]]+" + pattern + "[[:alpha:]]*"; //regex r(pattern); regex r("[^c]ei"); smatch results; string test_str; while (cout << "\nPlease enter a word for test:" << endl, cin >> test_str){ if (regex_search(test_str, results, r)) cout << "this word fit the requirements." << endl; else cout << "this word don't fit the requirements." << endl; } }在VS2013中并没有什么影响。
相关文章推荐
- 第十七章 17.3.2节练习
- 第十七章 17.3.3节练习
- C primer plus(第五版)编程练习第十七章
- 《C++ Primer Plus(第六版)》(42)(第十七章 输入、输出和文件 编程练习和答案2)
- 第十七章,txt文件的写入和读取数据结合练习(C++)
- 第十七章 17.3.4节练习
- C ++ Primer Plus 第六版 第十七章编程练习答案
- 第十七章 17.4.1节练习
- 现代软件工程 第十七章 【人、绩效和职业道德】 练习与讨论
- Android编程权威指南-第十七章挑战练习
- 第十七章 17.4.2节练习
- 第十七章 17.1.1节练习
- 《C++ Primer Plus(第六版)》(41)(第十七章 输入、输出和文件 编程练习和答案1)
- C Primer Plus (第五版) 第十七章 高级数据显示 编程练习
- 第十七章 17.1.2节练习
- 第十七章 17.2.1节练习 &17.2.2节练习
- 第十七章 17.5.1节练习
- 现代软件工程 第十七章 【人、绩效和职业道德】 练习与讨论
- C++ Primer Plus第六版 第十七章 编程练习答案(完结篇...木有18章)
- 第十七章 17.5.2节练习 & 17.5.3节练习