新博客地址:http://gorthon.sinaapp.com/
Python的模块实在是好用,可是在C++里面却经常找不到相应的功能,于是想着进行简单的模拟。
(模拟的不好,但是基本能用,欢迎拍砖)
首先从random模块开始:
对于choice和sample函数只实现了string的模拟。其中sample跟Python的有点差异,我这里实现的sample的采样大小可以大于原sequence。并且choice和sample都支持中文。
// file:function.h
// author: shuji
// bh2#qq.com
#pragma once
#include <cstdlib>
#include <ctime>
#include <climits>
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
namespace __rand__
{
// 已经实现的功能,线程安全未测试
double random();
int randint(const int start, const int stop);
int randint(const int stop);
double uniform(double start, double stop);
string choice(const string sequence);
string sample(const string sequence, const size_t num);
// 以下功能尚未实现
template <typename T>
T choice(T sequence);
template <typename T>
T sample(T sequence);
template <typename T>
void shuffle(T sequence);
// 下面为static函数,为本文件专用,外部文件不要使用
static bool has_seeded = false; // has_seeded 用来保证只运行一次srand
static void _initSrand_(); // 用static声明,则此文件外部一般不可引用此函数
template <typename T>
static void _swap_(T &t1, T &t2);
static string _getCurrentRandStr_(string &ascii, string &unicode);
static void _allocate_(const string &sequence, string &ascii, string &unicode);
}// end namespace __rand__
// function.cpp
// file:function.cpp
// author: shuji
// bh2#qq.com
#include "function.h"
template <typename T>
static void __rand__::_swap_(T &t1, T &t2)
{
T tmp = t1;
t1 = t2;
t2 = tmp;
}
static void __rand__::_initSrand_()
{
/**初始化种子*/
if (!has_seeded)
{
has_seeded = true;
srand((unsigned)time(NULL));
}
rand(); // 0 <= rand() < RAND_MAX(32767=0x7fff)
}
static string __rand__::_getCurrentRandStr_(string &ascii, string &unicode)
{
/**此函数供choice和sample使用,返回ascii或unicode中的一个随机字符*/
string result("");
int index(0);
// randint(1) = 0返回中文
// randint(1) = 1返回英文
if (randint(1))
{
index = randint(ascii.size() - 1);
result = ascii[index];
}
else // 返回中文
{
index = randint(unicode.size() - 1);
index = (index&0x1)? index - 1 : index; // 不要取到中文的第二个字节,index&0x1 = 1表示index为奇数,应将其变为偶数(注意下标从0开始,故中文的第一个字节下标为偶数)
result = unicode.substr(index, 2);
}
return result;
}
static void __rand__::_allocate_(const string &sequence, string &ascii, string &unicode)
{
ascii.empty();
unicode.empty();
/**将sequence分为中文和英文*/
for (size_t index(0); index<sequence.size(); index++)
{
if ((sequence[index]&0x80)&&(unsigned)sequence[index]>=0)
unicode += sequence.substr(index++, 2);
else
ascii += sequence[index];
}
}
double __rand__::random()
{
/**返回闭区间[0, 1]上的一个浮点数*/
_initSrand_();
return rand() / (double)(RAND_MAX - 1);
}
int __rand__::randint(const int start, const int stop)
{
/**返回闭区间[start, stop]或[stop, start]上的一个整数*/
int istart = start;
int istop = stop;
if(istart > istop)
_swap_(istart, istop);
//_initSrand_();
return int(random() * (istop + 1 - istart)) + istart;
}
int __rand__::randint(const int stop)
{
/**返回闭区间[0, stop]或[stop, 0]上的一个整数*/
return randint(0, stop);
}
double __rand__::uniform(double start, double stop)
{
/**返回闭区间[start, stop]或[stop, start]上的一个浮点数*/
return random() * (stop - start) + start;
}
string __rand__::choice(const string sequence)
{
/**返回sequence中的一个随机字符*/
string ascii(""); // 存储所有英文
string unicode(""); // 存储所有中文
_allocate_(sequence, ascii, unicode);
return _getCurrentRandStr_(ascii, unicode);
}
string __rand__::sample(const string sequence, const size_t num)
{
/**返回sequence中的一个采样序列,采样长度不限*/
string ascii(""); // 存储所有英文
string unicode(""); // 存储所有中文
string result(""); // 返回值
_allocate_(sequence, ascii, unicode);
for(size_t i=0; i < num; i++)
result += _getCurrentRandStr_(ascii, unicode);
return result;
}
测试文件(main.cpp):
// file:main.cpp
// author: shuji
// bh2#qq.com
#include <iostream>
#include <string>
#include "function.h"
using namespace std;
template <typename T>
void show(T t)
{
cout << t << endl;
}
int main()
{
show(__rand__::random());
show(__rand__::randint(3));
show(__rand__::randint(3, 0));
show(__rand__::uniform(5.2, 4.8));
string str("^和9谐a中b华c人d民e共f和g国{1}quot;);
show(__rand__::choice(str));
//for(int i=0;i<10;i++)
show(__rand__::sample(str, 155));
return EXIT_SUCCESS;
}