⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 random_variable_generationdlg.cpp

📁 随机遍量的产生程序说明: 1 功能:该程序目的是要能够产生包括均匀分布
💻 CPP
字号:
            // Random_Variable_GenerationDlg.cpp : implementation file
//11.7,2003
//修改于11.9,2003

#include "stdafx.h"
#include "Random_Variable_Generation.h"
#include "Random_Variable_GenerationDlg.h"

#include "Variable_Generate.h"
#include "Random_Vari_Statistic.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()

/////////////////////////////////////////////////////////////////////////////
// CRandom_Variable_GenerationDlg dialog

CRandom_Variable_GenerationDlg::CRandom_Variable_GenerationDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRandom_Variable_GenerationDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRandom_Variable_GenerationDlg)
	m_Number = 1000;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
//****************************************
	the_mean = 0;
	the_variance = 0;
	Cursel_type = 5;
	draw_interval = 300/DRAW_DOT_NUMBER;
//****************************************
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRandom_Variable_GenerationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRandom_Variable_GenerationDlg)
	DDX_Control(pDX, IDC_COMBO_TYPE, m_comboBox);
	DDX_Text(pDX, IDC_EDIT_NUMBER, m_Number);
	DDV_MinMaxInt(pDX, m_Number, 500, MAX_NUMBER);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRandom_Variable_GenerationDlg, CDialog)
	//{{AFX_MSG_MAP(CRandom_Variable_GenerationDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_EXPORT, OnExport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRandom_Variable_GenerationDlg message handlers

BOOL CRandom_Variable_GenerationDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRandom_Variable_GenerationDlg::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 CRandom_Variable_GenerationDlg::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();
//*******************************************************
		CDC* dc;
		dc=GetDC();
		CBrush b(RGB(220,220,220));
		CRect r(10,10,400,300);
		dc->FillRect(&r,&b);
		CBrush c(RGB(0,0,0));
		dc->FrameRect(&r,&c);
	    //dc->MoveTo(200,290);
		//dc->LineTo(200,20);
		dc->MoveTo(20,280);
		dc->LineTo(390,280);
//修改于11.9,2003
		CString str("Null");
	    int temp=0;
	    dc->SetBkColor(RGB(220,220,220));
	
	    switch(Cursel_type)
		{
	        case 0: { dc->TextOut(30,20,"均匀分布");break;}
        	case 1: { dc->TextOut(30,20,"瑞利分布");break;}
            case 2: { dc->TextOut(30,20,"正态分布");break;}
            case 3: { dc->TextOut(30,20,"指数分布");break;}
	        default: return;
		}
	   	str.Format("Mean: %f",the_mean);   
	    dc->TextOut(100,20,str);
	    str.Format("Var: %f",the_variance);
	    dc->TextOut(250,20,str);
        if(Cursel_type < 4 && Cursel_type >= 0) 
		{
			temp = (int)draw_array[0];
        	dc->MoveTo(50,280-temp);
	        for(int i = 1;i < DRAW_DOT_NUMBER;i++)
			{
				temp = (int)draw_array[i];
		       	dc->LineTo(i*draw_interval+50,280-temp);
			}
		}

//修改end
	
//*******************************************************
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRandom_Variable_GenerationDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//************************************************************************
void CRandom_Variable_GenerationDlg::OnExport() 
{
	// TODO: Add your control notification handler code here
	if(Cursel_type > 3 ||Cursel_type < 0) 
	{
		MessageBox("Error parameters or no data!");
		return;
	}
	CString filename;
	CString szFilter="文本文件(*.txt)|*.txt|All Files (*.*)|*.*||";
	CFileDialog fileopenbox(FALSE,"txt",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);
//	fileopenbox.m_ofn.lpstrTitle="Browse";
	if(fileopenbox.DoModal()==IDOK)
	{
		filename=fileopenbox.GetPathName();
		//在这里加入保存文件的代码
        FILE *fp;
		fp = fopen(filename,"w+");

		char* type_str;
		switch(Cursel_type)
		{
		   case 0: type_str = "Uninfom distribution";break;
		   case 1: type_str = "rili distribution";break;
		   case 2: type_str = "normal distribution";break;
		   case 3: type_str = "exp distribution";break;
		   default: return;
		}
		
		fprintf(fp,"%s(Sorted from small to great)",type_str);
		for(int i = 0;i < m_Number;i++)
		{
			if(i%5 == 0)  fprintf(fp,"\nDot  %d~%d:",i,i+5);
			fprintf(fp," %10f ,",pf[i]);
		}

		fclose(fp);
	}
}


void CRandom_Variable_GenerationDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	Cursel_type = m_comboBox.GetCurSel();

	if(Cursel_type > 3 ||Cursel_type < 0) 
	{
		MessageBox("Please select a type!");
		return;
	}
	
	UINT s;
	s = (unsigned)time( NULL );//get the seed value

	switch(Cursel_type)
	{
	case 0: { unif_distr(s,m_Number,pf);break;}
	case 1: { rili_distr(s,m_Number,pf);break;}
    case 2: { normal_distr(s,m_Number,pf);break;}
    case 3: { exp_distr(s,m_Number,pf);break;}
	default: break;
	}
	//调用统计函数float mean_of(float* pf,int num),进行均值的计算
	the_mean = mean_of(pf,m_Number);
    //调用统计函数float variance_of(float* pf,int num),进行方差的计算
	the_variance = variance_of(pf,m_Number);
	//调用统计函数int density_function(float* pf,int num,
	///////////int maxdrawnum, float* draw_array,int type),计算大体概率密度。
	//而后绘制统计曲线
    density_function(pf,m_Number,DRAW_DOT_NUMBER,draw_array,Cursel_type);
	for(int i = 0;i < DRAW_DOT_NUMBER;i++)
	{
        switch(Cursel_type)
		{
		   // 将绘图函数乘以100,使概率密度函数图像看起来清楚
		   case 0: draw_array[i] = draw_array[i] * MAX_NUMBER;break;  
		   case 2: draw_array[i] = draw_array[i] * MAX_NUMBER/2;break;
		   case 3: draw_array[i] = draw_array[i] * MAX_NUMBER/(float)2.5;break;
		   default: draw_array[i] = draw_array[i] * MAX_NUMBER/2;break;
		}
	}
	
	OnPaint();

	//CDialog::OnOK();
}

//**************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -