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

📄 work1dlg.cpp

📁 计算机模拟物理作业,用来模拟抛体运动
💻 CPP
字号:
// work1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "work1.h"
#include "work1Dlg.h"

#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
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 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()

/////////////////////////////////////////////////////////////////////////////
// CWork1Dlg dialog

CWork1Dlg::CWork1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWork1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWork1Dlg)
	m_step = 0.0;
	m_xstart = 0.0;
	m_ystart = 0.0;
	m_xend = 0.0;
	m_yend = 0.0;
	m_xshort = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWork1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWork1Dlg)
	DDX_Control(pDX, IDC_PIC, m_pic);
	DDX_Text(pDX, IDC_STEP, m_step);
	DDX_Text(pDX, IDC_XSTART, m_xstart);
	DDV_MinMaxDouble(pDX, m_xstart, 0., 20.);
	DDX_Text(pDX, IDC_YSTART, m_ystart);
	DDV_MinMaxDouble(pDX, m_ystart, 0., 1.);
	DDX_Text(pDX, IDC_XEND, m_xend);
	DDX_Text(pDX, IDC_YEND, m_yend);
	DDX_Text(pDX, IDC_XSHORT, m_xshort);
	DDV_MinMaxUInt(pDX, m_xshort, 1, 10);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWork1Dlg, CDialog)
	//{{AFX_MSG_MAP(CWork1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CLEANSCREEN, OnCleanscreen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWork1Dlg message handlers

BOOL CWork1Dlg::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
	//添加自己的变量
	m_step =0.5;
	m_xstart =2;
	m_ystart = 0;
	m_xshort =1;
	CWnd *p; 
	CString tempstring;
	p=GetDlgItem(IDC_STEP); 
	tempstring.Format("%f", m_step);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_XSTART); 
	tempstring.Format("%f", m_xstart);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_YSTART); 
	tempstring.Format("%f", m_ystart);
    p->SetWindowText(tempstring);

	p=GetDlgItem(IDC_XSHORT); 
	tempstring.Format("%d", m_xshort);
    p->SetWindowText(tempstring);
	//*******************************************************
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWork1Dlg::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 CWork1Dlg::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 CWork1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWork1Dlg::OnOK() 
{
	// TODO: Add extra validation here
	CWork1Dlg::UpdateData(TRUE);
	//OnCleanscreen();
	const double M = 0.01;
	const double AirCondition = 1.0e-4;// 空气阻力的系数,<1
	const double G= 9.8;

	double XStartSpeed; // x轴得初速度,设定为 0-1
	double YstartSpeed; // y轴得初速度,设定为 0-1
	
	double deltaV = 1.0e-7; //精度
	double tempacceX = AirCondition / M;
	double X =0;
	double Y =0;

	double deltaX = 1;
	double deltaY = 1;
	
	double Vxn1 = m_xstart;
	double Vyn1 = m_ystart;
	double step = m_step;        // 步长,>0
	long count = 0;  //用来计算实际的计算时间

	ofstream outx;
	ofstream outy;
	ofstream outvx;
	ofstream outvy;
	outx.open ("c:\\outx.txt");
	outy.open ("c:\\outy.txt");
	outvx.open ("c:\\outvx.txt");
	outvy.open ("c:\\outvy.txt");
	CRect rect;
	m_pic.GetClientRect (&rect);
	CWnd *pWnd;//得到图片框的窗口指针
	pWnd=GetDlgItem(IDC_PIC);
	CDC *dc=pWnd->GetDC();
	dc->MoveTo (0,0);
	//dc->TextOut(0,300,"hello ");
	//dc->LineTo (600,800);
	//dc->LineTo (1200,1800);
	int logicx;
	do 
	{
		deltaX = - tempacceX*Vxn1*Vxn1 * step;
		deltaY =  (G-tempacceX*Vyn1*Vyn1)*step;
		Vxn1 += deltaX;
		Vyn1 += deltaY;	
		//outvx<<Vxn1<<endl;
		//outvy<<Vyn1<<endl;
		X += Vxn1*step-step*step*tempacceX*Vxn1*Vxn1*0.5;
		Y += Vyn1*step+(G-tempacceX*Vyn1*Vyn1)*0.5*step*step;
		outx<<X<<","<<endl;
		outy<<Y<<","<<endl;
		logicx = X/m_xshort;
		dc->LineTo(logicx, (int)Y/500);
		count ++;
	}while((fabs(deltaX)>deltaV)||(fabs(deltaY)>deltaV));

	CWnd *p; 
    p=GetDlgItem(IDC_XEND); 
	CString tempstring;
	tempstring.Format("%f", Vxn1);
    p->SetWindowText(tempstring); 
    p=GetDlgItem(IDC_YEND); 
	tempstring.Format("%f", Vyn1);
    p->SetWindowText(tempstring); 
	p=GetDlgItem(IDC_TIME); 
	tempstring.Format("%f", count*step);
    p->SetWindowText(tempstring); 
	outy.close ();
	outx.close ();
	//	AfxMessageBox("end calc");	
	dc->TextOut(0,rect.Height ()-20,"计算结束");



}

void CWork1Dlg::OnCleanscreen() 
{
	// TODO: Add your control notification handler code here
		CRect rect;
	m_pic.GetClientRect (&rect);
	CWnd *pWnd;//得到图片框的窗口指针
	pWnd=GetDlgItem(IDC_PIC);
	CDC *dc=pWnd->GetDC();
	COLORREF col;
	dc->SetBkColor(RGB(220,230,0)) ;
	col = dc->GetBkColor();
	dc->FillSolidRect (rect.left,rect.top,rect.Width(),rect.Height(),col);

}


void CWork1Dlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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