一个规范的程序员往往要对自己所编写的软件模块进行比较全面的测试,从某种意义上来说,每一个程序员都应该对自己编写的代码负责。
在测试中,我们必不可少的要使用大量的测试数据。下面这个小程序就是完成这个功能的。你可以在主程序里面指定产生要产生字符串的类型、长度、记录的条数和最终输出的文件名。
// Random.cpp: 产生随机字符串(纯数字、纯字母、或数字字母组合),
// 并将生成的字符串保存到文本文件中
// Date: 2008.05.11
#include <iostream>
#include <vector>
#include <time.h>
#include <list>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
/*
* 功能:产生一个指定范围内的随机数
* 参数:iRange-随机数范围
* 返值:产生的随机数
*/
int GenNum(const int iRange)
{
return (rand() % iRange);
}
/*
* 功能:产生一个指定位数的由纯数字组成的字符串
* 参数:iLength-字符串长度
* 返值:随机字符串
*/
string GenNumOfString(const int iLength)
{
string str = "";
char ch[2];
for (int i=0; i<iLength; i++)
{
_itoa_s(GenNum(10), ch, 2, 10);
str += ch;
}
return str;
}
// RandomDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Random.h"
#include "RandomDlg.h"
#include "GlobalVar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CRandomDlg dialog
CRandomDlg::CRandomDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRandomDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRandomDlg)
m_DistributionString = _T("");
m_DimensionString = _T("");
m_MethodString = _T("");
m_MultiDimInt = 0;
m_ParamSetString = _T("");
m_RandNumInt = 0;
m_LowInt = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// 构造一个新的COpenGL对象
m_pDisplay = new COpenGL;
m_DimensionType = 0;
}
void CRandomDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRandomDlg)
DDX_Control(pDX, IDC_RANDNUM, m_RandNumCtrl);
DDX_Control(pDX, IDC_PARAMSET, m_ParamSetCtrl);
DDX_Control(pDX, IDC_METHOD, m_MethodCtrl);
DDX_Control(pDX, IDC_DISTRIBUTION, m_DistributionCtrl);
DDX_Control(pDX, IDC_DIMENSION, m_DimensionCtrl);
DDX_CBString(pDX, IDC_DISTRIBUTION, m_DistributionString);
DDX_CBString(pDX, IDC_DIMENSION, m_DimensionString);
DDX_CBString(pDX, IDC_METHOD, m_MethodString);
DDX_Text(pDX, IDC_MULTIDIM, m_MultiDimInt);
DDX_Text(pDX, IDC_PARAMSET, m_ParamSetString);
DDX_Text(pDX, IDC_RANDNUM, m_RandNumInt);
DDX_Text(pDX, IDC_LOW, m_LowInt);
DDV_MinMaxInt(pDX, m_LowInt, -1, 10000000);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRandomDlg, CDialog)
//{{AFX_MSG_MAP(CRandomDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_CLOSEUP(IDC_DIMENSION, OnCloseupDimension)
ON_CBN_CLOSEUP(IDC_DISTRIBUTION, OnCloseupDistribution)
ON_CBN_DROPDOWN(IDC_DISTRIBUTION, OnDropdownDistribution)
ON_CBN_CLOSEUP(IDC_METHOD, OnCloseupMethod)
ON_CBN_DROPDOWN(IDC_METHOD, OnDropdownMethod)
ON_EN_CHANGE(IDC_PARAMSET, OnChangeParamset)
ON_EN_CHANGE(IDC_RANDNUM, OnChangeRandnum)
ON_BN_CLICKED(IDC_GENERATE, OnGenerate)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_INSERT, OnInsert)
ON_EN_CHANGE(IDC_LOW, OnChangeLow)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CRandomDlg message handlers
BOOL CRandomDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 定义OpenGL绘图窗口的矩形大小
CRect rect(7,17,300,310);
// 创建COpenGL类对象
m_pDisplay->Create(NULL, // 缺省的窗口
NULL, // 无窗口名称
WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_VISIBLE, // 定义窗口风格
rect, // 窗口的大小
this, // 指定当前对话框为其父窗口指针
0);
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_MULTIDIM);
if(m_DimensionType==3)
pEdit->EnableWindow(TRUE);
else
pEdit->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CRandomDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CRandomDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRandomDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// 添加CRandomDlg类的析构函数
CRandomDlg::~CRandomDlg()
{
if(m_pDisplay)
{
delete m_pDisplay; // 释放m_pDisplay指针
}
}
void CRandomDlg::OnCloseupDimension()
{
// TODO: Add your control notification handler code here
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_DIMENSION);
int Sel;
if((Sel=pCombo->GetCurSel())!=CB_ERR){
pCombo->GetLBText(Sel, m_DimensionString);
if(m_DimensionString.Compare("一维")==0)
m_DimensionType = 1;
else if(m_DimensionString.Compare("二维")==0)
m_DimensionType = 2;
else if(m_DimensionString.Compare("多维")==0)
m_DimensionType = 3;
}
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_MULTIDIM);
if(m_DimensionType==3)
pEdit->EnableWindow(TRUE);
else
pEdit->EnableWindow(FALSE);
}
void CRandomDlg::OnCloseupDistribution()
{
// TODO: Add your control notification handler code here
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_DISTRIBUTION);
int Sel;
if((Sel=pCombo->GetCurSel())!=CB_ERR){
pCombo->GetLBText(Sel, m_DistributionString);
if(m_DistributionString.Compare("正态分布N(a,b)")==0)
m_DistributionType = DIM_NORMALDISTRIBUTION;
else if(m_DistributionString.Compare("指数分布E(0,a)")==0)
m_DistributionType = DIM_EXPONENTIAL;
}
}
void CRandomDlg::OnDropdownDistribution()
{
// TODO: Add your control notification handler code here
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_DISTRIBUTION);
switch(m_DimensionType){
case 1:
{
pCombo->ResetContent();
pCombo->AddString("正态分布N(a,b)");
pCombo->AddString("指数分布E(0,a)");
pCombo->SelectString(-1,m_DistributionString);
}
break;
case 2:
{
pCombo->ResetContent();
pCombo->AddString("正态分布N(a,b)");
pCombo->SelectString(-1,m_DistributionString);
}
break;
default:
{
pCombo->ResetContent();
pCombo->AddString("请输入维数");
}
break;
}
}
void CRandomDlg::OnCloseupMethod()
{
// TODO: Add your control notification handler code here
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_METHOD);
int Sel;
if((Sel=pCombo->GetCurSel())!=CB_ERR){
pCombo->GetLBText(Sel, m_MethodString);
if(m_MethodString.Compare("Box-Muller变换抽样法")==0)
m_MethodType = MET_BOXMULLER;
else if(m_MethodString.Compare("中心极限定理的近似抽样法")==0)
m_MethodType = MET_CENTRALLIMIT;
else if(m_MethodString.Compare("变换抽样法")==0)
m_MethodType = MET_COMMONBH;
}
}
void CRandomDlg::OnDropdownMethod()
{
// TODO: Add your control notification handler code here
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_METHOD);
switch(m_DistributionType){
case DIM_NORMALDISTRIBUTION:
{
pCombo->ResetContent();
pCombo->AddString("中心极限定理的近似抽样法");
pCombo->AddString("Box-Muller变换抽样法");
pCombo->SelectString(-1,m_MethodString);
}
break;
case DIM_EXPONENTIAL:
{
pCombo->ResetContent();
pCombo->AddString("变换抽样法");
pCombo->SelectString(-1,m_MethodString);
}
break;
default:
{
pCombo->ResetContent();
pCombo->AddString("请输入概率分布");
}
break;
}
}
void CRandomDlg::OnChangeParamset()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
int temp = (int)GetDlgItemText(IDC_PARAMSET, m_ParamSetString);
UpdateWindow();
}
void CRandomDlg::OnChangeRandnum()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
int temp = (int)GetDlgItemInt(IDC_RANDNUM);
if(temp>0){
m_RandNumInt = temp;
UpdateWindow();
}
}
void CRandomDlg::generateRandom()
{
loadParam();
switch(m_DimensionType){
case 1:
{
switch(m_DistributionType){
case DIM_NORMALDISTRIBUTION:
{
NormalDistribution();
}
break;
case DIM_EXPONENTIAL:
{
ExponentialDistribution();
}
break;
}
}
break;
case 2:
{
}
break;
case 3:
{
}
break;
default:
{
}
break;
}
showResult();
}
void CRandomDlg::NormalDistribution()
{
int i;
double r1, r2;
double mean, var;
mean = param[0];
var = param[1];
switch(m_MethodType){
case MET_BOXMULLER:
{
srand(time(NULL));
for(i=0; i<M_RANDNUMINT; if(random[i] else{ randmin="random[0];" randmax="random[0]," if(i="=0)" i--; random[i]<m_LowInt) && if(m_LowInt!="-1" var*sqrt(-2*log(r1))*cos(2*PI*r2); + random[i]="mean" } continue; if(r1<="0){" r2="(double)rand()/RAND_MAX;" r1="(double)rand()/RAND_MAX;" i++){>randmax)
randmax = random[i];
if(random[i]<RANDMIN) randmin="random[i];" } m_pDisplay- { 1: case switch(m_DimensionType){ j; i, int generateRandom(); here code handler notification control your Add TODO: CRandomDlg::OnGenerate() void param[i++]="atof(LPCTSTR(p));" paramset="m_ParamSetString;" p='paramset.SpanExcluding(",");' while(!paramset.IsEmpty()){ i="0;" p; CString CRandomDlg::loadParam() break;>m_AxisType = AXIS_2D;
m_pDisplay->randnum = m_RandNumInt;
m_pDisplay->clear();
double span = randmax - randmin;
for(i=0; i<M_RANDNUMINT; if(random[i] i++){ j++){ j<10; for(j="0;">=span/10*j+randmin && random[i]<SPAN m_pDisplay- 10*(j+1)+randmin){>x[j]=0.1*(j+1);
m_pDisplay->y[j]+=1/(double)m_RandNumInt;
break;
}
}
}
}
break;
case 2:
m_pDisplay->m_AxisType = AXIS_3D;
break;
case 3:
break;
default:
m_pDisplay->m_AxisType = AXIS_NULL;
break;
}
m_pDisplay->switchPic();
}
void CRandomDlg::showResult()
{
int i;
CListBox *pList = (CListBox*)GetDlgItem(IDC_RESULT);
CString rd;
pList->ResetContent();
for(i=0; i<M_RANDNUMINT; i++){ pList- random[i]); i+1, %f?, rd.Format(?%d:>AddString(rd);
}
}
void CRandomDlg::OnSave()
{
// TODO: Add your control notification handler code here
char fileBuffer[5010] = {0}, wBuffer[10010] = {0};
char temp[1000];
int i;
for(i=0; i<M_RANDNUMINT; if(random[i] else{ randmin="random[0];" randmax="random[0]," if(i="=0)" i--; random[i]<m_LowInt) && if(m_LowInt!="-1" random[i]="-1/mean*log(r1);" } continue; r1="(double)rand()/RAND_MAX;" i++){ { case int void if(r1="=0){" i<m_RandNumInt; for(i="0;" srand(time(NULL)); MET_COMMONBH: switch(m_MethodType){ mean="param[0];" mean; double r2; r1, i; CRandomDlg::ExponentialDistribution() MB_OK); | MB_ICONSTOP ?失败了?, MessageBox(?保存文件失败?, else MB_ICONEXCLAMATION ?搞定了?, MessageBox(?保存文件成功?, out.Close(); 关闭文件 wBSize); out.Write(wBuffer, 写入文件 wBSize="strlen(wBuffer);" 得到写入当前缓存中内容的长度 在这里把你要写入文件的内容写入缓存,如果写入的内容过长,可以分次写入 CFile::modeCreate)) CFile::modeWrite if(out.Open(outDlg.GetPathName(), 得到文件名,并用创建和写入方式打开 IDOK) if(outDlg.DoModal()="=" 显示对话框 outDlg.m_ofn.nMaxFile="5000;" 设定缓存大小 ; outDlg.m_ofn.lpstrDefExt="txt" 设定保存文件的默认后缀,如果自己有输入后缀,则时候输入的后缀 outDlg.m_ofn.lpstrFile="fileBuffer;" 使用自定义的文件名缓存 outDlg.m_ofn.lpstrTitle='_T("保存自定义文件");' 设定保存对话框标题 NULL); (*.*)|*.*||?), (*.txt)|*.txt|所有文件 _T(?文本文件 NULL, outDlg(FALSE, CFileDialog out; CFile wBSize; ?/r/n?); strcat(wBuffer, temp); sprintf(temp,?%lf?,random[i]);>randmax)
randmax = random[i];
if(random[i]<RANDMIN) randmin="random[i];" } { int here code handler notification control your Add TODO: void i="0;" break; MB_OK); | MB_ICONSTOP ?失败了?, MessageBox(?保存文件失败?, else MB_ICONEXCLAMATION ?搞定了?, MessageBox(?保存文件成功?, 得到文件名,并用创建和写入方式打开 IDOK) 显示对话框 设定缓存大小 ; 设定保存文件的默认后缀,如果自己有输入后缀,则时候输入的后缀 使用自定义的文件名缓存 设定保存对话框标题 NULL); (*.*)|*.*||?), (*.txt)|*.txt|所有文件 _T(?文本文件 NULL, CFileDialog if(temp temp="(int)GetDlgItemInt(IDC_LOW);" mask. the into ORed flag ENM_CHANGE with CRichEditCtrl().SetEventMask() call and function CDialog::OnInitDialog() override you unless this send not will control, RICHEDIT a is If CRandomDlg::OnChangeLow() }else{ fout.close(); fin.close(); 500); fin.getline(temp, strlen(temp)); fout.write(temp, rd); strcat(temp, random[i++]); ?-%f/n?, sprintf(rd, ?); ? while(!fin.eof()){ rd[20]; char if(!fout.fail()){ ios::out); fout.open(name, ?.txt?); strcat(name, ?_RD?); *ext="0;" if(!fin.fail()){ ios::in); fin.open(name, ?.?); ext="strrchr(name," insertDlg.GetPathName()); strcpy(name, *ext; name[200], if(insertDlg.DoModal()="=" insertDlg.m_ofn.nMaxFile="5000;" insertDlg.m_ofn.lpstrDefExt="txt" insertDlg.m_ofn.lpstrFile="fileBuffer;" insertDlg.m_ofn.lpstrTitle='_T("植入自定义文件");' insertDlg(FALSE, fin; ifstream fout; ofstream temp[500]; fileBuffer[5010]="{0}," CRandomDlg::OnInsert()>=0 || temp==-1){
m_LowInt = temp;
UpdateWindow();
}else{
m_LowInt = 0;
UpdateWindow();
}
}