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

📄 matrixdlg.cpp

📁 用于解实数范围内的超定方程组。附带有详尽的程序说明文档。具体操作步骤可以参照说明。
💻 CPP
字号:
// MatrixDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Matrix.h"
#include "MatrixDlg.h"
#include <fstream>
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const int Max=10000;
bool control=0;
//int m;   //矩阵行数
//int n;   //矩阵列数
float **a;//系数矩阵
float **b;//常数项矩阵
float **at;//a的转置
float **ata;//at与a的乘积
float **atb;//at与b的乘积
float **u;
//float x[Max];

float* fOneDPointer(int num)
{
	register int i;

	float* p = new float[num];

	for(i=0; i<num; i++){
		 p[i]=0.0f;
	  }
	return(p);
}

void Free_fOneDPointer(float *p)
{
	delete [] p;
}

float** fTwoDPointer(int raw,int col)
{
	register int i,j;

	float* arr = new float[raw*col];
	float** p = new float*[raw];
	for(i=0; i<raw; i++){
	  p[i] = arr + i*col;
	  for(j=0; j<col; j++)
		 p[i][j]=(float)0.0;
	  }
	return(p);
}

void Free_fTwoDPointer(float **p)
{
	delete [] *p;
	delete [] p;
}

void CMatrixDlg::InitMatrix(int m,int n)//对各个数组进行初始化
{
	UpdateData(true);
	if(control==0){
	  m=m_h;
	  n=m_l;
      a=fTwoDPointer(m,n);
	  b=fTwoDPointer(m,1);
      at=fTwoDPointer(n,m);
      ata=fTwoDPointer(n,n);
      atb=fTwoDPointer(n,1);
      u=fTwoDPointer(n,n+1);
	  control=1;
	}

}

void CMatrixDlg::FreeMatrix()//释放各个数组的空间
{
	Free_fTwoDPointer(a);
	Free_fTwoDPointer(b);
	Free_fTwoDPointer(at);
	Free_fTwoDPointer(ata);
	Free_fTwoDPointer(atb);
	Free_fTwoDPointer(u);

}

void CMatrixDlg::MatrixInver(float **a,int m,int n)
{
	UpdateData(true);
	int i,j;
	m=m_h;
	n=m_l;
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
		{
		at[j][i]=a[i][j];           
		}
	
}

void CMatrixDlg::MatrixMultiple(float **at,float **a,int m,int n)
{
	UpdateData(true);
	int i,j,k;
	m=m_h;
	n=m_l;
	for(i=0;i<n;i++)
	for(j=0;j<n;j++){
		for(k=0;k<m;k++)
		ata[i][j]+=at[i][k]*a[k][j];
	}
}


void CMatrixDlg::MatrixMultipleA(float **at,float **b,int m,int n)
{
	UpdateData(true);
	int i,j,k;
	m=m_h;
	n=m_l;
	for(i=0;i<n;i++)
	for(j=0;j<1;j++){
		for(k=0;k<m;k++)
		atb[i][j]+=at[i][k]*b[k][j];
	}
}

void CMatrixDlg::adding(float **ata,float **atb,int m,int n)
{   
	UpdateData(true);
	int i,j;
	m=m_h;
	n=m_l;
	for(i=0;i<n;i++)	
	{
		for(j=0;j<n;j++)
		{	
			u[i][j]=ata[i][j];
		}
		u[i][n]=atb[i][0];
	}

}


float* CMatrixDlg::lu(float **u,int m,int n)
{
	float *x;
	x=fOneDPointer(Max);
	UpdateData(true);
	int i,r,k;
	m=m_h;
	n=m_l;

	for(r=0;r<n;r++)
	{
		for(i=r;i<=n;i++)
			for(k=0;k<r;k++)
				u[r][i]-=u[r][k]*u[k][i];
			for(i=r+1;i<n;i++)
			{
				for(k=0;k<r;k++)
					u[i][r]-=u[i][k]*u[k][r];
				u[i][r]/=u[r][r];
			}
	}
	for(i=n-1;i>=0;i--)
	{
		for(r=n-1;r>=i+1;r--)
			u[i][n]-=u[i][r]*x[r];
		x[i]=u[i][n]/u[i][i];
	}
	
	return x;

}

float * CMatrixDlg::Calculate(float **a, float **b, int m, int n)
{
	float *x;
	x=fOneDPointer(Max);
	UpdateData(true);
	m=m_h;
	n=m_l;
	MatrixInver(a,m,n);//求系数矩阵a的转置矩阵at 
    MatrixMultiple(at,a,m,n);//求at和a的乘积
    MatrixMultipleA(at,b,m,n);//求at和b的乘积
    adding(ata,atb,m,n);
	x=lu(u,m,n);
	//Free_fOneDPointer(x);
	return x;
}

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMatrixDlg dialog

CMatrixDlg::CMatrixDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMatrixDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMatrixDlg)
	m_h = 0;
	m_l = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMatrixDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMatrixDlg)
	DDX_Text(pDX, IDC_EDIT_H, m_h);
	DDX_Text(pDX, IDC_EDIT_L, m_l);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMatrixDlg, CDialog)
	//{{AFX_MSG_MAP(CMatrixDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_READ_CONST, OnReadConst)
	ON_BN_CLICKED(IDC_CALCULATE, OnCalculate)
	ON_BN_CLICKED(IDC_READ_MATRIX, OnReadMatrix)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatrixDlg message handlers

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

void CMatrixDlg::OnReadMatrix() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
    //m=m_h;
	//n=m_l;
	if(m_h==0||m_l==0)
	{
		AfxMessageBox("请输入矩阵的行列数(行列数为非零正整数)");
	}
	else
	{
    
    InitMatrix(m_h,m_l);
	CString FileName="a.txt";
	ifstream in(FileName);
	if(!in){
		MessageBox(FileName+"文件打开失败");
	}
	else
	{  
		char Num[Max];				
		for(int i=0;i<m_h;i++)
			for(int j=0;j<m_l;j++)
		{
		in>>Num;
		a[i][j]=(float)atof(Num);		
		}   
	    MessageBox(FileName+"文件读入成功");
	}
	}
}

void CMatrixDlg::OnReadConst() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
    //m=m_h;
	//n=m_l;
	if(m_h==0||m_l==0)
	{
		AfxMessageBox("请输入矩阵的行列数(行列数为非零正整数)");
	}
	else
	{
	InitMatrix(m_h,m_l);
    CString FileName="b.txt";
    ifstream in(FileName);
	if(!in){
		MessageBox(FileName+"文件打开失败");
	}
	else
	{
		char Num[Max];
		for(int i=0;i<m_h;i++)
		{
		in>>Num;
        b[i][0]=(float)atof(Num);
		}   
	  MessageBox(FileName+"文件读入成功");

	}
	}
}

void CMatrixDlg::OnCalculate() 
{
	// TODO: Add your control notification handler code here
	float *x;
	x=fOneDPointer(Max);
    UpdateData(true);
	int m,n;
	m=m_h;
	n=m_l;
	control=0;
	x=Calculate(a,b,m,n);
	CString text;
	CString str[Max];	
	for(int i=0;i<n;i++)
	{
		str[i].Format("%f",x[i]);
        text=text+str[i]+"    ";
	}
	GetDlgItem(IDC_EDIT_ANSWER)->SetWindowText(text);
	Free_fOneDPointer(x);
    FreeMatrix();
}
















⌨️ 快捷键说明

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