您的位置:首页 > 其它

17.4节练习

2016-07-07 20:26 288 查看
练习17.28 编写函数,每次调用生成并返回一个均匀分布的随机unsigned int。

#include <iostream>
#include <random>
using namespace std;
unsigned rand_static()
{
static default_random_engine e;
static uniform_int_distribution<unsigned> u;
return u(e);
}
unsigned rand_()
{
default_random_engine e;
uniform_int_distribution<unsigned> u;
return u(e);
}

int main()
{
for (int i = 0; i < 10; ++i) {
cout << rand_static() << endl;
cout << rand_() << endl;
cout << endl;
}
}


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

#include <iostream>
#include <random>
using namespace std;
unsigned rand_static(unsigned num)
{
static default_random_engine e(num);
//static uniform_int_distribution<unsigned> u;
return e();
}
unsigned rand_(unsigned num)
{
default_random_engine e(num);
//uniform_int_distribution<unsigned> u;
return e();
}

int main()
{
unsigned num;
cin >> num;
for (int i = 0; i < 10; ++i) {

cout << rand_static(num) << endl;
cout << rand_(num) << endl;
cout << endl;
}

}


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

#include <iostream>
#include <random>
using namespace std;
unsigned rand_static(unsigned num)
{
static default_random_engine e(num);
//static uniform_int_distribution<unsigned> u;
cout << "max min:" << e.max() << " " << e.min() << endl;
return e();
}
unsigned rand_(unsigned num)
{
default_random_engine e(num);
//uniform_int_distribution<unsigned> u;
cout << "max min:" << e.max() << " " << e.min() << endl;
return e();
}

int main()
{
unsigned num;
cin >> num;
for (int i = 0; i < 10; ++i) {

cout << rand_static(num) << endl;
cout << rand_(num) << endl;
cout << endl;
}

}


练习17.31 对于本节中的游戏编程,如果在do循环体内定义be和e,会发生什么?

每步循环都会创建一个新引擎,从而每步循环都会生成相同的值。

练习17.32 如果我们在循环内定义resp,会发生什么?

作为循环的条件,不能定义在循环体呢,否则编译不通过。

练习17.33  修改11.3.6节(392页)中的单词转换程序,允许对一个给定单词有多种转换方式,每次随机选择一个进行实际转换。

#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <random>
std::map<std::string, std::string> buildmap(std::ifstream &ifile)
{
std::map<std::string, std::string> temp;
std::string str, f, s;
while (getline(ifile, str))
{
std::istringstream sst(str);
sst >> f >> s;
temp[f] = s;
}
return temp;
}
std::s
4000
tring trans(const std::string &s, std::map<std::string, std::string> &m)//转换
{
auto temp = m.find(s);
if (temp != m.end())
return m[s];
else
return s;
}
std::string trans_random(const std::string &s, std::map<std::string, std::string> &m)//转换
{
auto temp = m.find(s);
if (temp != m.end())
return (m[s]+"!");
else
return s;
}
int main(int argc, char **argv)
{
using namespace std;
ifstream ifpw(argv[1]), iftxt(argv[2]);
if (!ifpw&&!iftxt)
{
cerr << "open file error!\n";
exit(1);
}
map<string, string> pw(buildmap(ifpw));
string temp, f;
default_random_engine e;
bernoulli_distribution b;
bool t;
while (getline(iftxt, temp))
{
istringstream iss(temp);
while (iss >> f)
{
t = b(e);
if(t)
cout << trans_random(f, pw) << " ";
else
cout << trans(f, pw) << " ";
}
cout << endl;
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: