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

📄 least_squaredlg.cpp

📁 用最小二乘法来进行函数模拟。对输入的数据构造一个多项式。
💻 CPP
字号:
// least_squareDlg.cpp : implementation file
//

#include "stdafx.h"
#include "least_square.h"
#include "least_squareDlg.h"
#include "math.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()

/////////////////////////////////////////////////////////////////////////////
// CLeast_squareDlg dialog

CLeast_squareDlg::CLeast_squareDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLeast_squareDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLeast_squareDlg)
	m_pointnumber = 0;
	m_jieshu = 0;
	m_x1 = 0.0f;
	m_y1 = 0.0f;
	m_x2 = 0.0f;
	m_y2 = 0.0f;
	m_x3 = 0.0f;
	m_y3 = 0.0f;
	m_x4 = 0.0f;
	m_y4 = 0.0f;
	m_x5 = 0.0f;
	m_y5 = 0.0f;
	m_x6 = 0.0f;
	m_y6 = 0.0f;
	m_x7 = 0.0f;
	m_y7 = 0.0f;
	m_x8 = 0.0f;
	m_y8 = 0.0f;
	m_x9 = 0.0f;
	m_y9 = 0.0f;
	m_x10 = 0.0f;
	m_y10 = 0.0f;
	m_x11 = 0.0f;
	m_y11 = 0.0f;
	m_x12 = 0.0f;
	m_y12 = 0.0f;
	m_x13 = 0.0f;
	m_y13 = 0.0f;
	m_x14 = 0.0f;
	m_y14 = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLeast_squareDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLeast_squareDlg)
	DDX_Text(pDX, IDC_EDIT1, m_pointnumber);
	DDX_Text(pDX, IDC_EDIT2, m_jieshu);
	DDX_Text(pDX, IDC_EDIT3, m_x1);
	DDX_Text(pDX, IDC_EDIT4, m_y1);
	DDX_Text(pDX, IDC_EDIT5, m_x2);
	DDX_Text(pDX, IDC_EDIT6, m_y2);
	DDX_Text(pDX, IDC_EDIT7, m_x3);
	DDX_Text(pDX, IDC_EDIT8, m_y3);
	DDX_Text(pDX, IDC_EDIT9, m_x4);
	DDX_Text(pDX, IDC_EDIT10, m_y4);
	DDX_Text(pDX, IDC_EDIT11, m_x5);
	DDX_Text(pDX, IDC_EDIT12, m_y5);
	DDX_Text(pDX, IDC_EDIT13, m_x6);
	DDX_Text(pDX, IDC_EDIT14, m_y6);
	DDX_Text(pDX, IDC_EDIT15, m_x7);
	DDX_Text(pDX, IDC_EDIT16, m_y7);
	DDX_Text(pDX, IDC_EDIT17, m_x8);
	DDX_Text(pDX, IDC_EDIT18, m_y8);
	DDX_Text(pDX, IDC_EDIT19, m_x9);
	DDX_Text(pDX, IDC_EDIT20, m_y9);
	DDX_Text(pDX, IDC_EDIT21, m_x10);
	DDX_Text(pDX, IDC_EDIT22, m_y10);
	DDX_Text(pDX, IDC_EDIT23, m_x11);
	DDX_Text(pDX, IDC_EDIT24, m_y11);
	DDX_Text(pDX, IDC_EDIT25, m_x12);
	DDX_Text(pDX, IDC_EDIT26, m_y12);
	DDX_Text(pDX, IDC_EDIT27, m_x13);
	DDX_Text(pDX, IDC_EDIT28, m_y13);
	DDX_Text(pDX, IDC_EDIT29, m_x14);
	DDX_Text(pDX, IDC_EDIT30, m_y14);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLeast_squareDlg, CDialog)
	//{{AFX_MSG_MAP(CLeast_squareDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, Onjisuan)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeast_squareDlg message handlers

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

float* CLeast_squareDlg::Approx(float *x, float *y, int m, int n)
{
	float* c,*a;
	int i,j,t;
	c=(float*)malloc((n+1)*(n+2)*sizeof(float));
	for(i=0;i<=n;i++)
	{
		for(j=0;j<=n;j++)
		{
			*(c+i*(n+2)+j)=0.0;
			for(t=0;t<=m-1;t++)
				*(c+i*(n+2)+j)+=power(i+j,x[t]);
		}
		*(c+i*(n+2)+n+1)=0.0;
		for(j=0;j<=m-1;j++)
			*(c+i*(n+2)+n+1)+=y[j]*power(i,x[j]);
	}
	a=ColPivot((float*)c,n+1);
	return a;
}

float* CLeast_squareDlg::ColPivot(float *a, int n)
{
	int i,j,t,k;
	float *x,*c,p;
	x=(float*)malloc(n*sizeof(float));
	c=(float*)malloc(n*(n+1)*sizeof(float));
	for(i=0;i<=n-1;i++)
		for(j=0;j<=n;j++)
			*(c+i*(n+1)+j)=(*(a+i*(n+1)+j));
		for(i=0;i<=n-2;i++)
		{
			k=i;
			for(j=i+1;j<=n-1;j++)
				if(fabs(*(c+j*(n+1)+i))>(fabs(*(c+k*(n+1)+i))))k=j;
				if(k!=i)
					for(j=i;j<=n;j++)
					{
						p=*(c+i*(n+1)+j);
						*(c+i*(n+1)+j)=*(c+k*(n+1)+j);
						*(c+k*(n+1)+j)=p;
					}
					for(j=i+1;j<=n-1;j++)
					{
						p=(*(c+j*(n+1)+i))/(*(c+i*(n+1)+i));
						for(t=i;t<=n-1;t++)
							*(c+j*(n+1)+t)=*(c+j*(n+1)+t)-p*(*(c+i*(n+1)+t));
						*(c+j*(n+1)+n)-=*(c+i*(n+1)+n)*p;
					}
		}
		for(i=n-1;i>=0;i--)
		{
			for(j=n-1;j>=i+1;j--)
				(*(c+i*(n+1)+n))-=x[j]*(*(c+i*(n+1)+j));
			x[i]=*(c+i*(n+1)+n)/(*(c+i*(n+1)+i));
		}
		free(c);
		return x;
}


float CLeast_squareDlg::power(int i, float v)
{
	float a=1.0;
	while(i--)a*=v;
	return a;

}

void CLeast_squareDlg::Onjisuan() 
{
	// TODO: Add your control notification handler code here
    UpdateData();
	float x[14],y[14];
	x[0]=m_x1;y[0]=m_y1;
	x[1]=m_x2;y[1]=m_y2;
	x[2]=m_x3;y[2]=m_y3;
	x[3]=m_x4;y[3]=m_y4;
	x[4]=m_x5;y[4]=m_y5;
	x[5]=m_x6;y[5]=m_y6;
	x[6]=m_x7;y[6]=m_y7;
	x[7]=m_x8;y[7]=m_y8;
	x[8]=m_x9;y[8]=m_y9;
	x[9]=m_x10;y[9]=m_y10;
	x[10]=m_x11;y[10]=m_y11;
	x[11]=m_x12;y[11]=m_y12;
	x[12]=m_x13;y[12]=m_y13;
	x[13]=m_x14;y[13]=m_y14;
	//float y[16]={4.00,6.40,8.00,8.80,9.22,9.50,9.70,9.86,10.00,10.20,10.32,10.42,10.50,10.55,10.58,10.60};
	float *a;
	a=Approx(x,y,m_pointnumber,m_jieshu);
	char tempstr[300];
	int k=0;
	k=sprintf(tempstr,"拟合曲线方程:f(x)=\n%f\n",a[0]);
	for(int i=1;i<=m_jieshu;i++)
	{
		if(a[i]>0)k+=sprintf(tempstr+k,"+%fx^%d\n",a[i],i);
		else if(a[i]<0)k+=sprintf(tempstr+k,"-%fx^%d\n",fabs(a[i]),i);
		else;
	}
	MessageBox(tempstr);

}

⌨️ 快捷键说明

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