📄 dicedlg.cpp
字号:
// DiceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Dice.h"
#include "DiceDlg.h"
#include "NewWindow.h"
#include "iostream.h"
#include "fstream"
#include "string"
#include "sstream"
#include "time.h"
#include <CTime>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class Experiment{
private:
int diceNum;
int expNum;
int** result;
int* count;
public:
Experiment(int dicen,int expn)
{
diceNum = dicen;
expNum = expn;
result = new int*[expn];
for(int i = 0;i<expn;i++)
result[i] = new int[diceNum+1];
count = new int[6*diceNum+1];
}
void reInit(int dicen,int expn)
{
delete result;
Experiment(dicen,expn);
}
void simulate()
{
for(int i = 0;i<6*diceNum+1;i++)
count[i] = 0;
//以上初始化概率分布计数器
srand((unsigned int)time(NULL));
for(i =0;i<expNum;i++)
{
int sum = 0;
for(int j = 0;j<diceNum;j++)
{
result[i][j] = rand()%6+1;
sum+=result[i][j];
}
result[i][j] = sum;
count[sum] ++;
}
}
void writeFile(string filename)
{
ofstream out(filename.c_str());
out<<"Dice number:"<<diceNum<<"\t"
<<"Flip number:"<<expNum<<endl;
for(int i = 0;i<expNum;i++)
{
out<<"[Flip"<<i<<"]\t";
for(int j = 0;j<diceNum;j++)
{
out<<result[i][j]<<"\t";
}
out<<"Total:"<<result[i][diceNum]<<endl;
}
out.close();
}
void writeText(CDialog* dlg,int ID)
{
CString toshow;
string cs;
stringstream tran;
for(int i = 0;i<expNum;i++)
{
tran<<"[ Flip "<<i<<" ]\t\t";
for(int j = 0;j<diceNum;j++)
{
tran<<result[i][j]<<"\t";
}
tran<<"Total:"<<result[i][diceNum]<<"\r\n";
}
cs = tran.str();//将stringstram里的内容转成string
CWnd* pwd;
pwd = dlg->GetDlgItem(ID);
toshow.Format("%s",cs.c_str());//将string 转为CString,format函数功能很强大
pwd->SetWindowText(toshow);
}
void drawPic(CDialog *dlg,int DRAWON,CRect& rt);
};
class CAboutDlg : public CDialog
{
private:
Experiment* exp;
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()
/////////////////////////////////////////////////////////////////////////////
// CDiceDlg dialog
CDiceDlg::CDiceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDiceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDiceDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDiceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDiceDlg)
DDX_Control(pDX, IDC_EDIT1, m_text1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDiceDlg, CDialog)
//{{AFX_MSG_MAP(CDiceDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_COMMAND(ID_MENUITEM11, OnMenuitem11)
ON_COMMAND(ID_MENUITEM12, OnMenuitem12)
ON_COMMAND(ID_MENUITEM21, OnMenuitem21)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDiceDlg message handlers
BOOL CDiceDlg::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 CDiceDlg::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 CDiceDlg::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 CDiceDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDiceDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CDiceDlg::OnOK()
{
}
void CDiceDlg::OnButton1()
{
CString str;
for(int i = 0;i<10;i++)
str+= "string\r\n";
NewWindow newwin;
if(newwin.DoModal()==IDOK)
{
// this->Invalidate();
int expnum = newwin.m_expnum;
int dicenum = newwin.m_dicenum;
Experiment exp(dicenum,expnum);
exp.simulate();
exp.writeFile("result.txt");
exp.writeText(this,IDC_EDIT1);
//下面画图
CRect rt;
this->GetClientRect(rt);
rt.top = rt.top +20;
rt.bottom = rt.bottom*0.58;
rt.right = rt.right-20;
rt.left = rt.left+20;
exp.drawPic(this,IDC_PIC,rt);
// TODO: Add your control notification handler code here
// TODO: Add extra validation here
}
// TODO: Add your control notification handler code here
}
void CDiceDlg::OnButton2()
{
CString path;
CString szFilter = CString("Text Files(*.txt)|*.txt||");
CFileDialog fileDlg(FALSE,NULL,NULL,OFN_HIDEREADONLY,szFilter);//false另存为, true打开
if(fileDlg.DoModal()==IDOK)
{
path= fileDlg.GetPathName();
CString content;
this->m_text1.GetWindowText(content);
string str(content.GetBuffer(content.GetLength()));
ofstream out(path);
out<<str;
out.close();
}
}
//以下是Experiment的draw函数
void Experiment::drawPic(CDialog *dlg,int DRAWON,CRect& rt)
{
CWnd* pWnd;
CDC* pDc;
CPen White,Blue;
HBRUSH hr;
hr = CreateSolidBrush(RGB(100,100,211));
White.CreatePen(PS_SOLID,1,RGB(255,255,255));
Blue.CreatePen(PS_SOLID,10,RGB(50,50,50));
pWnd = dlg->GetDlgItem(DRAWON);
pWnd->MoveWindow(rt);
pDc = pWnd->GetDC();
pDc->SelectObject(White);
int width = rt.Width();
int height = rt.Height();
pDc->MoveTo(0,0);
pDc->Rectangle(0,0,width,height);
//pDc->SelectObject(Blue);
int colNum = 5*diceNum+1;
float colwid,gapwid;
gapwid = (float)width/(3*colNum+1);
colwid = 2*gapwid;
float pos = gapwid;
float* value;
value = new float[colNum];
int max = 0;
for(int j = 0;j<colNum;j++)
if(count[diceNum+j]>max)
max = count[diceNum+j];
for(j = 0;j<colNum;j++)
value[j] = (float)count[diceNum+j]/max*(height-40);
/*
stringstream st;
string to;
st<<max;
st>>to;
dlg->MessageBox(to.c_str());
*/
pDc->SelectObject(hr);
stringstream temp;
string buf;
for(int i = 0;i<colNum;i++)
{
temp.clear();
temp<<(float)count[i+diceNum]/expNum;
temp>>buf;
pDc->TextOut(pos,height-40-value[i],buf.c_str());
pDc->Rectangle(pos,height-20-value[i],pos+colwid,height-20);
temp.clear();
temp<<i+diceNum;
temp>>buf;
pDc->TextOut(pos,height-20,buf.c_str());
pos = pos+colwid+gapwid;
}
string str1,str2;
temp.clear();
temp<<diceNum;
temp>>str1;
str1 = "骰子数:"+str1;
temp.clear();
temp<<expNum;
temp>>str2;
str2 = "实验数:"+str2;
pDc->TextOut(20,20,str1.c_str());
pDc->TextOut(20,40,str2.c_str());
dlg->ReleaseDC(pDc);
}
void CDiceDlg::OnMenuitem11()
{
OnButton1();
// TODO: Add your command handler code here
}
void CDiceDlg::OnMenuitem12()
{
CDialog::OnCancel();
// TODO: Add your command handler code here
}
void CDiceDlg::OnMenuitem21()
{
CAboutDlg adlg;
adlg.DoModal();
// TODO: Add your command handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -