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

📄 zerchfadlg.cpp

📁 一个最小二乘法的拟合数据的小程序
💻 CPP
字号:
// ZerchfaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Zerchfa.h"
#include "ZerchfaDlg.h"
#include "InputData.h"
#include "alculate.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()

/////////////////////////////////////////////////////////////////////////////
// CZerchfaDlg dialog

CZerchfaDlg::CZerchfaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CZerchfaDlg::IDD, pParent)
{




	//{{AFX_DATA_INIT(CZerchfaDlg)
	m_degree = 1;
	m_nodenum = 5;
	//m_result = _T("");
	//	m_erro = _T("");
	
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CZerchfaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CZerchfaDlg)
	DDX_Text(pDX, IDC_DEGREE, m_degree);
	DDV_MinMaxInt(pDX, m_degree, 1, 4);
	DDX_Text(pDX, IDC_NODENUM, m_nodenum);
	DDV_MinMaxInt(pDX, m_nodenum, 5, 8);
	DDX_Text(pDX, IDC_REZULT, m_result);
	DDX_Text(pDX, IDC_ERRO, m_erro);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CZerchfaDlg, CDialog)
	//{{AFX_MSG_MAP(CZerchfaDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ASSURE, OnAssureCalculate)
	ON_BN_CLICKED(IDC_DRAW, OnDraw)
	ON_BN_CLICKED(IDC_INPUTDATA, OnIndata)
	ON_BN_CLICKED(IDC_FULLSCREEN, OnFullscreen)
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZerchfaDlg message handlers

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

void CZerchfaDlg::OnAssureCalculate() 
{
	
	
	UpdateData(TRUE);
	
	calculate.SetDegree(m_degree);
	
	calculate.SetNodenum(m_nodenum);
	
	calculate.SetXY(m_nodenum);
	
	for(int i=0;i<m_nodenum;i++)
	{
		calculate.X.SetElement(i,0,px[i]);		
	}
	
	for( i=0;i<m_nodenum;i++)
	{
		calculate.Y.SetElement(i,0,py[i]);
		
		//TRACE("\ncalulate.x:%f  calulate.y:%f\n",calculate.X.GetElement(i,0),
		//			calculate.Y.GetElement(i,0));
		
	}
	
	calculate.computing();
	
	for( i=0;i<calculate.a.GetNumRows();i++)
		  {
		TRACE("\na:%f\n",calculate.a.GetElement(i,0));
		  }

	  //////输出结果:
		  
	if(m_degree==3)
		m_result.Format("g(x) = (%f) + (%f)*x + (%f)*x^2 + (%f)*x^3",calculate.a.GetElement(0,0),
		calculate.a.GetElement(1,0),calculate.a.GetElement(2,0),calculate.a.GetElement(3,0));
	else if(m_degree==4)
		m_result.Format("g(x) = (%f) + (%f)*x + (%f)*x^2 + (%f)*x^3 + (%f)*x^4",
		calculate.a.GetElement(0,0),calculate.a.GetElement(1,0),calculate.a.GetElement(2,0),
		calculate.a.GetElement(3,0),calculate.a.GetElement(4,0));
	else if(m_degree==2)
		m_result.Format("g(x) = (%f) + (%f)*x + (%f)*x^2",calculate.a.GetElement(0,0),
		calculate.a.GetElement(1,0),calculate.a.GetElement(2,0));
	else
		m_result.Format("g(x)= (%f) + (%f)*x",calculate.a.GetElement(0,0),calculate.a.GetElement(1,0));
		  
	m_erro.Format("δ*δ=%f",calculate.errosquare);
		  
	UpdateData(FALSE);
		  
		  
}

void CZerchfaDlg::OnIndata() 
{
	if(inputdata.DoModal()==IDOK)
	{
/*
				px.Add(inputdata.m_x1);
				px.Add(inputdata.m_x2);
				px.Add(inputdata.m_x3);
				px.Add(inputdata.m_x4);
				px.Add(inputdata.m_x5);
				px.Add(inputdata.m_x6);
				px.Add(inputdata.m_x7);
				px.Add(inputdata.m_x8);
				
				py.Add(inputdata.m_y1);
				py.Add(inputdata.m_y2);
				py.Add(inputdata.m_y3);
				py.Add(inputdata.m_y4);
				py.Add(inputdata.m_y5);
				py.Add(inputdata.m_y6);
				py.Add(inputdata.m_y7);
				py.Add(inputdata.m_y8);*/
		px=new double[8];
		py=new double[8];
		px[0]=inputdata.m_x1;
		px[1]=inputdata.m_x2;
		px[2]=inputdata.m_x3;
		px[3]=inputdata.m_x4;
		px[4]=inputdata.m_x5;
		px[5]=inputdata.m_x6;
		px[6]=inputdata.m_x7;
		px[7]=inputdata.m_x8;

		py[0]=inputdata.m_y1;
		py[1]=inputdata.m_y2;
		py[2]=inputdata.m_y3;
		py[3]=inputdata.m_y4;
		py[4]=inputdata.m_y5;
		py[5]=inputdata.m_y6;
		py[6]=inputdata.m_y7;
		py[7]=inputdata.m_y8;
		
		
	}
		
}

void CZerchfaDlg::OnDraw() 
{    ///将求解的多项式系数一维列向量转化成一维数组


	double * ca;
	ca=new double[m_degree+1];
	for(int i=0;i<m_degree+1;i++)
	{
      ca[i]=calculate.a.GetElement(i,0);
	}
	CDrafting drafting=new CDrafting(px,py,ca,m_degree,m_nodenum);
	                                /*double* point_x,double* point_y,double* funcof,int degree,int nodenum*/
	for(	i=0;i<drafting.nnodenum;i++)
	{
		TRACE("\npx:%f  py:%f\n",drafting.p_x[i],drafting.p_y[i]);
	}
	for( i=0; i<drafting.ddegree + 1; i++)
	{
		TRACE("\nf_c: %f\n",drafting.f_c[i]);
	}
	drafting.DoModal();

	// delete[] ca;
	
}

void CZerchfaDlg::OnFullscreen() 
{
	GetWindowPlacement(&m_OldWndPlacement);
	CRect WindowRect;
	GetWindowRect(&WindowRect);
	CRect ClientRect;
	RepositionBars(0,0xffff,AFX_IDW_PANE_FIRST,reposQuery,&ClientRect);
	ClientToScreen(&ClientRect);

	//获取屏幕分辨率
	int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
	int nFullHeight=GetSystemMetrics(SM_CYSCREEN);

	//将除控制条外的客户区全屏显示到从(0,0)到(nFullWidth,nFullHeight)区域,
	//将(0,0)和(nFullWidth,nFullHeight)两个点外扩充原窗口和除控制条之外的客户区位置间差值
	//就得到全屏显示的窗口位置
	m_FullScreenRect.left=WindowRect.left-ClientRect.left;
	m_FullScreenRect.top=WindowRect.top-ClientRect.top;
	m_FullScreenRect.right=WindowRect.right-ClientRect.right+nFullWidth;
	m_FullScreenRect.bottom=WindowRect.bottom-ClientRect.bottom+nFullHeight;
	m_bFullScreen=TRUE;
	
	
	//进入全屏显示状态
	WINDOWPLACEMENT wndpl;
	wndpl.length=sizeof(WINDOWPLACEMENT);
	wndpl.flags=0;
	wndpl.showCmd=SW_SHOWNORMAL;
	wndpl.rcNormalPosition=m_FullScreenRect;
	SetWindowPlacement(&wndpl);

}
//////////////////////////////////////////////////////////////////////////
//重载OnGetMinMaxInfo()函数,在全屏显示时提供全屏显示的位置信息
void CZerchfaDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{

	if (m_bFullScreen)
	{
		lpMMI->ptMaxSize.x=m_FullScreenRect.Width();
		lpMMI->ptMaxSize.y=m_FullScreenRect.Height();
		lpMMI->ptMaxPosition.x=m_FullScreenRect.Width();
		lpMMI->ptMaxPosition.y=m_FullScreenRect.Height();

		//最大Track尺寸也要改变
		lpMMI->ptMaxTrackSize.x=m_FullScreenRect.Width();
		lpMMI->ptMaxTrackSize.y=m_FullScreenRect.Height();
	}
	CZerchfaDlg::OnGetMinMaxInfo(lpMMI);
	m_bFullScreen=FALSE;
}



void CZerchfaDlg::EndFullScreen()
{
	if (!m_bFullScreen) 
	{
		ShowWindow(SW_HIDE);
		SetWindowPlacement(&m_OldWndPlacement);
	}
}

void CZerchfaDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar==VK_ESCAPE)//如果按的键为ESC键
	{

		//获取主框架的指针

		CZerchfaDlg* czerchadlg=(CZerchfaDlg*)AfxGetApp()->m_pMainWnd;
		//调用主窗口类的自定义函数EndFullScreen(),便退出全屏显示状态
		czerchadlg->EndFullScreen();
	}
	
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

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