C++ Primer(第五版)|练习题答案与解析(第十七章:标准库特殊设施)

C++ Primer(第五版)|练习题答案与解析(第十七章:标准库特殊设施)

本博客主要记录C++ Primer(第五版)中的练习题答案与解析。
参考:C++ Primer

练习题17.1

定义一个保存三个int值的tuple,并将其分别初始化为10,20和30。

练习题17.2

定义一个tuple,保存一个string、一个vector<string>和一个pair<string, int>

#include <tuple>
#include <string>
#include <vector>
int main()
{
    auto three_ints = std::make_tuple(10, 20, 30);
    using SomeTuple = std::tuple < std::string, std::vector<std::string>, std::pair<std::string, int> > ;
    SomeTuple some_tuple;
    return 0;
}

练习题17.4

编写并测试你自己版本的findBook函数

练习题17.5

重写findBook,令其返回一个pair,包含一个索引和迭代器pair。

练习题17.6

重写findBook,不使用tuple或pair。

练习题17.7

解释你更倾向哪一个版本findBook。

练习题17.8

在本节最后一段代码中,如果将Sales_data()作为第三个参数传给accumulate,会发生什么?

搬运:C++ Primer
SalesData.h

#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
#include <iostream>
class Sales_data
{
    // friends
    friend Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs);
    friend std::ostream&
    operator << (std::ostream& os, const Sales_data& s);
    friend std::istream&
    operator >> (std::istream& is, Sales_data& s);
    friend Sales_data add(const Sales_data&, const Sales_data&);
    friend std::ostream &print(std::ostream&, const Sales_data&);
    friend std::istream &read(std::istream&, Sales_data&);
public:
	// constructors
	Sales_data() = default;
	Sales_data(const std::string &s): bookNo(s) { }
	Sales_data(const std::string &s, unsigned n, double p):
	           bookNo(s), units_sold(n), revenue(p*n) { }
    Sales_data(const Sales_data &s ):
        bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
    { }
    Sales_data(Sales_data&& s):
        bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
    { }
    ~Sales_data(){ }
	Sales_data(std::istream &);
	std::string isbn() const { return bookNo; }
	Sales_data& combine(const Sales_data&);
    // assignments
    Sales_data& operator  =(const Sales_data& rhs);
    Sales_data& operator  =(const std::string&  rhs);
    Sales_data& operator +=(const Sales_data& rhs);
    // conversion
    explicit operator std::string () const { return bookNo; }
    explicit operator double      () const { return revenue; }
	double avg_price() const;
private:
	std::string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};
// overloaded operators added 10.Jan.2014 for ex14.2
inline Sales_data
operator+(const Sales_data& lhs, const Sales_data& rhs)
{
    Sales_data sum = lhs;
    sum += rhs;
    return sum;
}
std::ostream&
operator << (std::ostream& os, const Sales_data& item);
std::istream&
operator >> (std::istream& is, Sales_data& s);
// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istream&, Sales_data&);
// used in future chapters
inline 
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
	return lhs.isbn() < rhs.isbn();
}
#endif

SalesData.cpp

#include <iostream>
using std::istream; using std::ostream;
#include "SalesData.h"
Sales_data::Sales_data(std::istream &is) 
{
	// read will read a transaction from is into this object
	read(is, *this);
}
double 
Sales_data::avg_price() const {
	if (units_sold)
		return revenue/units_sold;
	else
		return 0;
}
// add the value of the given Sales_data into this object
Sales_data& 
Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold; // add the members of rhs into 
	revenue += rhs.revenue;       // the members of ``this'' object
    return *this; // return the object on which the function was called
}
// = Sales_data
Sales_data &Sales_data::operator =(const Sales_data &rhs)
{
    this->bookNo        = rhs.bookNo;
    this->revenue       = rhs.revenue;
    this->units_sold    = rhs.units_sold;
    return *this;
}
// =string
Sales_data &Sales_data::operator =(const std::string &rhs)
{
    *this= Sales_data(rhs);
    return *this;
}
// +=
Sales_data &Sales_data::operator +=(const Sales_data &rhs)
{
    this->revenue += rhs.revenue;
    this->units_sold += rhs.units_sold;
    return *this;
}
Sales_data 
add(const Sales_data &lhs, const Sales_data &rhs)
{
	Sales_data sum = lhs;  // copy data members from lhs into sum
	sum.combine(rhs);      // add data members from rhs into sum
	return sum;
}
// transactions contain ISBN, number of copies sold, and sales price
istream&
read(istream &is, Sales_data &item)
{
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}
ostream&
print(ostream &os, const Sales_data &item)
{
	os << item.isbn() << " " << item.units_sold << " " 
	   << item.revenue << " " << item.avg_price();
	return os;
}
// added 10.Jan 2014
std::ostream &
operator <<(std::ostream &os, const Sales_data &item)
{
    os << item.isbn() << " " << item.units_sold << " "
       << item.revenue << " " << item.avg_price();
    return os;
}
// added 12.Jan 2014
std::istream&
operator >>(std::istream &is, Sales_data &s)
{
    double price;
    // read input
    is >> s.bookNo >> s.units_sold >> price;
    // if successful, write into the object, give the object default state otherwise.
    if(is)
        s.revenue = s.units_sold * price;
    else
        s = Sales_data();
    return is;
}

mian.cpp

#include <iostream>
#include <tuple>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include "SalesData.h"
// for ex17.4
// maches has 3 members:
// an index of a store and iterators into that store's vector
typedef std::tuple<std::vector<Sales_data>::size_type,
                   std::vector<Sales_data>::const_iterator,
                   std::vector<Sales_data>::const_iterator>
                                                            matches;
// for ex17.5
// return a pair that holds an index and a pair of iterators.
typedef std::pair<std::vector<Sales_data>::size_type,
                  std::pair<std::vector<Sales_data>::const_iterator,
                            std::vector<Sales_data>::const_iterator>>
                                                                      matches_pair;
// for ex17.6
// return a struct that holds an index of a store and iterators into that store's vector
struct matches_struct
{
    std::vector<Sales_data>::size_type st;
    std::vector<Sales_data>::const_iterator first;
    std::vector<Sales_data>::const_iterator last;
    matches_struct(std::vector<Sales_data>::size_type s,
                   std::vector<Sales_data>::const_iterator f,
                   std::vector<Sales_data>::const_iterator l) : st(s), first(f), last(l) { }
} ;

// for ex17.4
// return a vector with an entry for each store that sold the given book.
std::vector<matches>
findBook(const std::vector<std::vector<Sales_data>>& files,
         const std::string& book);
// print the result using the given iostream
void reportResults(std::istream& in, std::ostream os,
                   const std::vector<std::vector<Sales_data>>& files);
// for ex17.5
// return a vector with an entry for each store that sold the given book.
std::vector<matches_pair>
findBook_pair(const std::vector<std::vector<Sales_data> > &files,
              const std::string &book);
// for ex17.6
// return a vector with an entry for each store that sold the given book.
std::vector<matches_struct>
findBook_struct(const std::vector<std::vector<Sales_data> > &files,
                const std::string &book);
int main()
{
    return 0;
}
// for ex17.4
// return a vector with an entry for each store that sold the given book.
std::vector<matches>
findBook(const std::vector<std::vector<Sales_data>>& files,
         const std::string& book)
{
    std::vector<matches> ret;

    // for each strore find the range of matching books, if any
    for (auto it = files.cbegin(); it != files.cend(); ++it)
    {
        // find the range of Sales_data tat have the same ISBN
        auto found = std::equal_range(it->cbegin(), it->cend(), book, compareIsbn);
        if(found.first != found.second)
            ret.push_back(std::make_tuple(it - files.cbegin(),
                                          found.first, found.second));
    }
    return ret;
}
// for ex17.4
// print the result using the given iostream
void reportResults(std::istream& in, std::ostream os,
                   const std::vector<std::vector<Sales_data>>& files)
{
    std::string s;
    while(in >> s)
    {
        auto trans = findBook(files, s);
        if(trans.empty()){
            std::cout << s << "not found in any stores" << std::endl;
            continue;
        }
        for(const auto& store :trans)
            os << "store " << std::get<0>(store) << " sales: "
               << std::accumulate(std::get<1>(store), std::get<2>(store),
                                  Sales_data(s))
                  << std::endl;
    }
}
// for ex17.5
// return a vector with an entry for each store that sold the given book
std::vector<matches_pair>
findBook_pair(const std::vector<std::vector<Sales_data> > &files,
              const std::string &book)
{
    std::vector<matches_pair> ret;
    for(auto it = files.cbegin(); it != files.cend(); ++it)
    {
        auto found = std::equal_range(it->cbegin(), it->cend(), book, compareIsbn);
        if(found.first != found.second)
            ret.push_back(std::make_pair(it - files.cbegin(),
                                         std::make_pair(found.first, found.second)));
    }
    return ret;
}
// for ex17.6
// return a vector with an entry for each store that sold the given book.
std::vector<matches_struct>
findBook_struct(const std::vector<std::vector<Sales_data> > &files,
                const std::string &book)
{
    std::vector<matches_struct> ret;
    for(auto it = files.cbegin(); it != files.cend(); ++it)
    {
        auto found = std::equal_range(it->cbegin(), it->cend(), book, compareIsbn);
        if(found.first != found.second)
            ret.push_back(matches_struct(it - files.cbegin(), found.first, found.second));
    }
    return ret;
}

练习题17.9

解释下列每个bitset对象所包含的位模式。

(a)(a) bitset<64> bitvec(32);

0000000000000000000000000000000000000000000000000000000000100000
//                                                       ^

(b)bitset<32> bv(1010101);输出:00000000000011110110100110110101
(c)string bstr; cin >> bstr; bitset<8>bv(bstr);取决于输入了什么。

练习题17.10

使用序列1、2、3、5、8、13、21初始化一个bitset,将这些位置位。对另一个bitset进行默认初始化,并编写程序将其恰当的位置位。

#include <iostream>
#include <bitset>
#include <vector>
int main()
{
    std::vector<int> v = { 1, 2, 3, 5, 8, 13, 21 };
    std::bitset<32> bset;
    for (auto i : v)    bset.set(i);
    std::bitset<32> bset2;
    for (unsigned i = 0; i != 32; ++i)
        bset2[i] = bset[i];
    std::cout <<bset <<std::endl;
    std::cout <<bset2<<std::endl;
}

测试

00000000001000000010000100101110
00000000001000000010000100101110

练习题17.11

定义一个数据结构,包含一个整型对象,记录一个包含10个问题的真/假测验的解答,如果测验包含100道题,你需要对数据结构做出什么改变?

练习题17.12

使用前一题的数据结构,编写一个函数,它接受一个问题的编号和一个表示真/假解答的值,函数根据这两个参数更新测验的解答。

练习题17.13

编写一个整型对象,包含真/假测验的正确答案。使用它来为前两题中的数据结构生成测验成绩。

#ifndef QUIZ
#define QUIZ
#include <iostream>
#include <bitset>
#include <utility>
#include <string>
#include <iostream>
//class Quiz
template<std::size_t N>
class Quiz
{
public:
    //constructors
    Quiz() = default;
    Quiz(std::string& s) :bitquiz(s){ }
    //generate grade
    template<std::size_t M>
    friend std::size_t grade(Quiz<M> const&, Quiz<M> const&);
    //print
    template<std::size_t M>
    friend std::ostream& operator<<(std::ostream&, Quiz<M> const&);
    //update bitset
    void update(std::pair<std::size_t, bool>);
private:
    std::bitset<N> bitquiz;
};
#endif
template<std::size_t N>
void Quiz<N>::update(std::pair<std::size_t, bool> pair)
{
    bitquiz.set(pair.first, pair.second);
}
template<std::size_t M>
std::ostream& operator<<(std::ostream& os, Quiz<M> const& quiz)
{
    os << quiz.bitquiz;
    return os;
}
template<std::size_t M>
std::size_t grade(Quiz<M> const& corAns, Quiz<M> const& stuAns)
{
    auto result = stuAns.bitquiz ^ corAns.bitquiz;
    result.flip();
    return result.count();
}
int main()
{
    std::cout << "练习题17.11" << std::endl;
    std::string s = "1010101";
    Quiz<10> quiz(s);
    std::cout << quiz << std::endl;
    std::cout << "练习题17.12" << std::endl;
    quiz.update(std::make_pair(1, true));
    std::cout << quiz << std::endl;
    std::cout << "练习题17.13" << std::endl;
    std::string answer = "10011";
    std::string stu_answer = "11001";
    Quiz<5> ans(answer), stu_ans(stu_answer);
    std::cout << grade(ans, stu_ans) << std::endl;
    return 0;
}

测试:

练习题17.11
0001010101
练习题17.12
0001010111
练习题17.13
3

练习题17.14

编写几个正则表达式,分别触发不同错误。运行你的程序,观察编译器对每个错误的输出。

练习题17.15

编写程序,使用模式查找“i在e之前,除非在e之后”规则的单词。你的程序应该提示用户输入一个单词,然后指出此单词是否符合要求。用一些违反和违反规则的单词测试你的程序。

练习题17.16

如果前一题程序中的regex对象用“[^c]ei”进行初始化,会发生什么?测试你的程序。

#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
    std::cout<<"练习题17.14:"<<std::endl;
    try{
        regex r("[[:alnum:]+\\.(cpp|cxx|cc)$", regex::icase);
    }
    catch(regex_error e)
    {
        cout << e.what() << " code: " << e.code() << endl;
    }
    // for ex17.15
    std::cout<<"练习题17.15:"<<std::endl;
    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!" <<endl;
        cout << "Please input a word! Input 'q' to quit!" << endl;
    }
    cout << endl;
    // for ex17.16
    std::cout<<"练习题17.16:"<<std::endl;
    r.assign("[^c]ei", regex::icase);
    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!" <<endl;
        cout << "Please input a word! Input 'q' to quit!" << endl;
    }
    return 0;
}

测试:

练习题17.14:
regex_error code: 4
练习题17.15:
Please input a word! Input 'q' to quit!
input
Input word input is not okay!
Please input a word! Input 'q' to quit!
eixt
Input word eixt is not okay!
Please input a word! Input 'q' to quit!
cied
Input word cied is not okay!
Please input a word! Input 'q' to quit!
ie
Input word ie is not okay!
Please input a word! Input 'q' to quit!
ei
Input word ei is not okay!
Please input a word! Input 'q' to quit!
q

练习题17.16:
Please input a word! Input 'q' to quit!
exit
Input word exit is not okay!
Please input a word! Input 'q' to quit!
ei
Input word ei is not okay!
Please input a word! Input 'q' to quit!
q

练习题17.17

更新你的程序,令他查找输入序列中所有违反“ei”语法规则的单词。

练习题17.18

修改你的程序,忽略包含“ei”但并非拼写错误的单词,如“albeit”和“neighbor”。

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <regex>
using std::regex;
using std::sregex_iterator;
int main()
{
	string s;
	cout << "Please input a sequence of words:" << endl;
	getline(cin, s);
	cout << endl;
	cout << "Word(s) that violiate the \"ei\" grammar rule:" << endl;
	string pattern("[^c]ei");
	pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
	regex r(pattern, regex::icase);
	for (sregex_iterator it(s.begin(), s.end(), r), end_it; it != end_it; ++it)
		cout << it->str() << endl;
	return 0;
}

测试:

Please input a sequence of words:
albeit

Word(s) that violiate the "ei" grammar rule:
albeit

练习题17.19

为什么不先检查m[4]是否匹配了就直接调用m[4].str()?

我们期望m[4]和m[6]中的两个分隔符是相同的。如果m[4](或m[6])不匹配,则m[4].str()(或m[6].str()分别)返回一个空字符串,该字符串也可以与其他分隔符进行比较。

练习题17.20

编写你自己版本的验证电话号码程序。

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <regex>
using std::regex;
using std::sregex_iterator;
using std::smatch;
bool valid(const smatch& m);
int main()
{
	string phone = "(\\()?(\\d{ 3 })(\\))?([-. ])?(\\d{ 3 })([-. ]?)(\\d{ 4 })";
	regex r(phone);
	smatch m;
	string s;
	bool valid_record;
	// read each record from the input file
	while (getline(cin, s))
	{
		valid_record = false;
		// for each matching phone number
		for (sregex_iterator it(s.begin(), s.end(), r), end_it; it != end_it; ++it)
		{
			valid_record = true;
			// check whether the number's formatting is valid
			if (valid(*it))
				cout << "valid phone number: " << it->str() << endl;
			else
				cout << "invalid phone number: " << it->str() << endl;
		}
		if (!valid_record)
			cout << "invalid record!" << endl;
	}
	return 0;
}
bool valid(const smatch& m)`在这里插入代码片`
{
	// if there is an open parenthesis before the area code
	if (m[1].matched)
		// the area code must be followed by a close parenthesis
		// and followed immediately by the rest of the number or a space
		return m[3].matched && (m[4].matched == 0 || m[4].str() == " ");
	else
		// then there can't be a close after the area code
		// the delimiters between the other two components must match
		return !m[3].matched && m[4].str() == m[6].str();
}

练习题17.21

使用本节定义的valid函数重写8.3.2节中的电话号码程序。

#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::smatch;
struct PersonInfo
{
    string name;
    vector<string> phones;
};
bool valid(const smatch& m);
bool read_record(istream& is, vector<PersonInfo>& people);
void format_record(ostream& os, const vector<PersonInfo>& people);
// fake function that makes the program compile
string format(const string &num) { return num; }
int main()
{
    vector<PersonInfo> people;
    string filename;
    cout << "Please input a record file name: ";
    cin >> filename;
    cout << endl;
    ifstream fin(filename);

    if (read_record(fin, people))
    {
        ofstream fout("data\\result.txt", ofstream::trunc);
        format_record(fout, people);
    }
    else
    {
        cout << "Fail to open file " << filename << endl;
    }
    return 0;
}
bool valid(const smatch& m)
{
    // if there is an open parenthesis before the area code
    if (m[1].matched)
        // the area code must be followed by a close parenthesis
        // and followed immediately by the rest of the number or a space
        return m[3].matched && (m[4].matched == 0 || m[4].str() == " ");
    else
        // then there can't be a close after the area code
        // the delimiters between the other two components must match
        return !m[3].matched && m[4].str() == m[6].str();
}
bool read_record(istream& is, vector<PersonInfo>& people)
{
    if (is)
    {
        string line, word; // will hold a line and word from input, respectively
                           // read the input a line at a time until cin hits end-of-file (or another error)
        while (getline(is, line))
        {
            PersonInfo info; // create an object to hold this record's data
            istringstream record(line); // bind record to the line we just read
            record >> info.name; // read the name
            while (record >> word) // read the phone numbers
                info.phones.push_back(word); // and store them
            people.push_back(info); // append this record to people
        }
        return true;
    }
    else
        return false;
}
void format_record(ostream& os, const vector<PersonInfo>& people)
{
    string phone = "(\\()?(\\d{ 3 })(\\))?([-. ])?(\\d{ 3 })([-. ]?)(\\d{ 4 })";
    regex r(phone);
    smatch m;
    for (const auto &entry : people)
    {
        // for each entry in people
        ostringstream formatted, badNums; // objects created on each loop
        for (const auto &nums : entry.phones)
        {
            for (sregex_iterator it(nums.begin(), nums.end(), r), end_it; it != end_it; ++it)
            {
                // for each number
                // check whether the number's formatting is valid
                if (!valid(*it))
                    // string in badNums
                    badNums << " " << nums;
                else
                    // "writes" to formatted's string
                    formatted << " " << format(nums);
            }
        }
        if (badNums.str().empty()) // there were no bad numbers
            os << entry.name << " " // print the name
            << formatted.str() << endl; // and reformatted numbers
        else // otherwise, print the name and bad numbers
            cerr << "input error: " << entry.name
            << " invalid number(s) " << badNums.str() << endl;
    }
}

练习题17.23

编写查找邮政编码的正则表达式。一个美国的邮政编码可以由五位数或九位数字组成。前五位数字和后四位数字之间可以用一个短横线分隔。

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
#include <regex>
using std::regex;
using std::sregex_iterator;
using std::smatch;
bool valid(const smatch& m);
int main()
{
	string zipcode =
		"(\\d{5})([-])?(\\d{4})?\\b";
	regex r(zipcode);
	smatch m;
	string s;
	while (getline(cin, s))
	{
		//! for each matching zipcode number
		for (sregex_iterator it(s.begin(), s.end(), r), end_it;
			it != end_it; ++it)
		{
			//! check whether the number's formatting is valid
			if (valid(*it))
				cout << "valid zipcode number: " << it->str() << endl;
			else
				cout << "invalid zipcode number: " << s << endl;
		}
	}
	return 0;
}
bool valid(const smatch& m)
{
	if ((m[2].matched)&&(!m[3].matched))
		return false;
	else
		return true;
}

测试:

55555-5555
valid zipcode number: 55555-5555

练习题17.24

编写你自己版本的重排电话号码格式的程序。

#include <iostream>
#include <regex>
#include <string>
using namespace std;
string pattern = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
string format = "$2.$5.$7";
regex r(pattern);
string s;
int main()
{
    while(getline(cin,s))
    {
        cout<<regex_replace(s,r,format)<<endl;
    }
    return 0;
}

测试:

drew (973)555.0130
drew 973.555.0130
morgan (201) 555-2368 862-555-0123
morgan 201.555.2368 862.555.0123
lee (609) 555-0132 2015550175 800.555-0000
lee 609.555.0132 201.555.0175 800.555.0000

练习题17.25

重写你的电话号码程序,使之只输出每个人的第一个电话号码。

#include <iostream>
#include <regex>
#include <string>
using namespace std;
string pattern = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
string fmt = "$2.$5.$7";
regex r(pattern);
string s;
int main()
{
    while(getline(cin,s))
    {
        smatch result;
        regex_search(s,result,r);
        if(!result.empty())
        {
        cout<<result.prefix()<<result.format(fmt)<<endl;
        }
        else
        {
            cout<<"Sorry, No match."<<endl;
        }
    }
    return 0;
}

测试:

morgan (201) 555-2368 862-555-0123
morgan 201.555.2368

练习题17.27

编写程序,将九位数字邮政编码转换为ddddd-dddd

#include <iostream>
#include <regex>
#include <string>
using namespace std;
string pattern = "(\\d{5})([.- ])?(\\d{4})";
string fmt = "$1-$3";
regex r(pattern);
string s;
int main()
{
    while(getline(cin,s))
    {
        smatch result;
        regex_search(s,result, r);

        if(!result.empty())
        {
            cout<<result.format(fmt)<<endl;
        }
        else
        {
            cout<<"Sorry, No match."<<endl;
        }
    }
    return 0;
}

测试:

941551234
94155-1234
555784612
55578-4612

练习题17.28

编写函数,每次调用生成并返回一个均匀分布的随机unsigned int。

练习题17.29

修改上一题中编写的函数,允许用户提供一个这种作为可选参数。

练习题17.30

再次修改,此次再增加两个参数,表示函数允许返回的最小值和最大值。

#include <iostream>
#include <random>
#include<string>
// default version
unsigned random_gen();
// with seed spicified
unsigned random_gen(unsigned seed);
// with seed and range spicified
unsigned random_gen(unsigned seed, unsigned min, unsigned max);
int main()
{
    std::string temp;
    while(std::cin >> temp)
    std::cout << std::hex << random_gen(19, 1, 10) << std::endl;
    return 0;
}
unsigned random_gen()
{
    static std::default_random_engine e;
    static std::uniform_int_distribution<unsigned> ud;
    return ud(e);
}
unsigned random_gen(unsigned seed)
{
    static std::default_random_engine e(seed);
    static std::uniform_int_distribution<unsigned> ud;
    return ud(e);
}
unsigned random_gen(unsigned seed, unsigned min, unsigned max)
{
    static std::default_random_engine e(seed);
    static std::uniform_int_distribution<unsigned> ud(min, max);
    return ud(e);
}
```+-
## 练习题17.33
>修改11.3.6节(P392)中的单词转换程序,允许对一个给定单词有多种转换方式,每次修改随机选择一种进行实际转换。

```cpp
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <random>
using std::default_random_engine;
using std::uniform_int_distribution;
#include <ctime>
using std::time;
#include <algorithm>
using std::sort;
using std::find_if;
#include <utility>
using std::pair;
int main() {
	typedef pair<string, string> ps;
	ifstream i("d.txt");
	vector<ps> dict;
	string str1, str2;
	// read wirds from dictionary
	while (i >> str1 >> str2) {
		dict.emplace_back(str1, str2);
	}
	i.close();
	// sort words in vector
	sort(dict.begin(), dict.end(), [](const ps &_ps1, const ps &_ps2){ return _ps1.first < _ps2.first; });
	i.open("i.txt");
	default_random_engine e(unsigned int(time(0)));
	// read words from text
	while (i >> str1) {
	  // find word in dictionary
		vector<ps>::const_iterator it = find_if(dict.cbegin(), dict.cend(),
		  [&str1](const ps &_ps){ return _ps.first == str1; });
		// if word doesn't exist in dictionary
		if (it == dict.cend()) {
		  // write it itself
			cout << str1 << ' ';
		}
		else {
		  // get random meaning of word
			uniform_int_distribution<unsigned> u (0, find_if(dict.cbegin(), dict.cend(),
			 [&str1](const ps &_ps){ return _ps.first > str1; }) - it - 1);
			// write random meaning
			cout << (it + u(e))->second << ' ';
		}
	}
	return 0;
}

练习题17.35

修改P670页中的程序,打印2的平方根,但这次打印十六进制数字的大写形式。

练习题17.36

修改上一题中的程序,打印不同的浮点数,使它们排成一列。

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
	std::cout << "练习题17.35:" << std::endl;
	cout << "default format: " << 100 * sqrt(2.0) << '\n'
		<< "scientific: " << scientific << 100 * sqrt(2.0) << '\n'
		<< "fixed decimal: " << fixed << 100 * sqrt(2.0) << '\n'
		<< "hexidecimal: " << uppercase << hexfloat << 100 * sqrt(2.0) << '\n'
		<< "use defaults: " << defaultfloat << 100 * sqrt(2.0)
		<< "\n\n";
	std::cout << "练习题17.36:" << std::endl;
	cout << left << setw(15) << "default format:" << setw(25) << right << 100 * sqrt(2.0) << '\n'
		<< left << setw(15) << "scientific:" << scientific << setw(25) << right << 100 * sqrt(2.0) << '\n'
		<< left << setw(15) << "fixed decimal:" << setw(25) << fixed << right << 100 * sqrt(2.0) << '\n'
		<< left << setw(15) << "hexidecimal:" << setw(25) << uppercase << hexfloat << right << 100 * sqrt(2.0) << '\n'
		<< left << setw(15) << "use defaults:" << setw(25) << defaultfloat << right << 100 * sqrt(2.0)
		<< "\n\n";
}

测试:

练习题17.35default format: 141.421
scientific: 1.414214e+02
fixed decimal: 141.421356
hexidecimal: 0X1.1AD7BCP+7
use defaults: 141.421

练习题17.36default format:                  141.421
scientific:                 1.414214E+02
fixed decimal:                141.421356
hexidecimal:               0X1.1AD7BCP+7
use defaults:                    141.421

练习题17.37

用为格式化版本的getline逐行读取一个文件。测试你的程序,给它一个文件,既包含空行又包含长度超过你传递给getline的字符数组大小的行。

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main () {
    ifstream myfile("test.txt");
    if (myfile) cout << 1 << endl;
    char sink [250];

    while(myfile.getline(sink,250))
    {
      cout << sink << endl;
    }
    return 0;
}

测试:

1
I'm expressin' with my full capabilities,
And now I'm livin' in correctional facilities,
Cause some don't agree with how I do this.
I get straight, meditate like a Buddhist
I'm droppin' flava, my behaviour is heriditery,
But my technique is very necessary.
Blame it on Ice Cube... Because he says it gets funky
When you got a subject and a predacit.


Add it on a dope beat
And that'll make you think.
Some suckaz just tickle me pink
To my stomache. 'Cause they don't flow like this one.
You know what? I won't hesitate to dis one
Or two before I'm through.
So don't try to sing this!
Some drop science
While I'm droppin' English.
Even if Yella
Makes it a-capella
I still express, yo, I don't smoke weed or a sess.
Cause its known to give a brother brain damage.
And brain damage on the mic don't manage
Nuthin'
But makin' a sucker and you equal.
Don't be another sequel...

这节是过了一遍,待用上的时候仔细看。

发布了76 篇原创文章 · 获赞 44 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_24739717/article/details/104680427