📄 matrixcalculatordlg.cpp
字号:
// MatrixCalculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MatrixCalculator.h"
#include "MatrixCalculatorDlg.h"
#include "Matrix.h"
#include "Complex.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()
/////////////////////////////////////////////////////////////////////////////
// CMatrixCalculatorDlg dialog
CMatrixCalculatorDlg::CMatrixCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMatrixCalculatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMatrixCalculatorDlg)
m_strMatrix1 = _T("");
m_strMatrix2 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMatrixCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMatrixCalculatorDlg)
DDX_Text(pDX, IDC_EDIT1, m_strMatrix1);
DDX_Text(pDX, IDC_EDIT2, m_strMatrix2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMatrixCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CMatrixCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
ON_BN_CLICKED(IDC_BUTTON15, OnButton15)
ON_BN_CLICKED(IDC_BUTTON16, OnButton16)
ON_BN_CLICKED(IDC_BUTTON17, OnButton17)
ON_BN_CLICKED(IDC_BUTTON18, OnButton18)
ON_BN_CLICKED(IDC_BUTTON19, OnButton19)
ON_BN_CLICKED(IDC_BUTTON20, OnButton20)
ON_BN_CLICKED(IDC_BUTTON21, OnButton21)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMatrixCalculatorDlg message handlers
BOOL CMatrixCalculatorDlg::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 CMatrixCalculatorDlg::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 CMatrixCalculatorDlg::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 CMatrixCalculatorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// 加法
void CMatrixCalculatorDlg::OnButton1()
{
// 构造矩阵
CMatrix mtxA, mtxB, mtxC;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1);
mtxB.FromString(m_strMatrix2);
// 矩阵加法
mtxC = mtxA + mtxB;
// 显示数据
m_strMatrix3 = mtxC.ToString(" ");
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
// 减法
void CMatrixCalculatorDlg::OnButton2()
{
// 构造矩阵
CMatrix mtxA, mtxB, mtxC;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1);
mtxB.FromString(m_strMatrix2);
// 矩阵减法
mtxC = mtxA - mtxB;
// 显示数据
m_strMatrix3 = mtxC.ToString(" ");
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
// 转置
void CMatrixCalculatorDlg::OnButton3()
{
// 构造矩阵
CMatrix mtxA, mtxC;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1);
// 矩阵转置
mtxC = mtxA.Transpose();
// 显示数据
m_strMatrix3 = mtxC.ToString(" ");
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
1 3 -2 0 4
-2 -1 5 -7 2
0 8 4 1 -5
3 -3 2 -4 1
4 5 -1
2 -2 6
7 8 1
0 3 -5
9 8 -6
*/
// 乘法
void CMatrixCalculatorDlg::OnButton4()
{
// 构造矩阵
CMatrix mtxA, mtxB, mtxC;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1);
mtxB.FromString(m_strMatrix2);
// 矩阵乘法
mtxC = mtxA * mtxB;
// 显示数据
m_strMatrix3 = mtxC.ToString(" ");
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
0.2368,0.2471,0.2568,1.2671
1.1161,0.1254,0.1397,0.1490
0.1582,1.1675,0.1768,0.1871
0.1968,0.2071,1.2168,0.2271
*/
// 实矩阵求逆的全选主元高斯-约当法
void CMatrixCalculatorDlg::OnButton5()
{
// 构造矩阵
CMatrix mtxA;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1, ",");
// 实矩阵求逆的全选主元高斯-约当法
if (mtxA.InvertGaussJordan())
m_strMatrix3 = mtxA.ToString(" ");
else
m_strMatrix3 = "求解失败";
// 显示数据
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
0.2368,0.2471,0.2568,1.2671
1.1161,0.1254,0.1397,0.1490
0.1582,1.1675,0.1768,0.1871
0.1968,0.2071,1.2168,0.2271
0.1345,0.1678,0.1875,1.1161
1.2671,0.2017,0.7024,0.2721
-0.2836,-1.1967,0.3558,-0.2078
0.3576,-1.2345,2.1185,0.4773
*/
// 复矩阵求逆的全选主元高斯-约当法
void CMatrixCalculatorDlg::OnButton6()
{
// 构造矩阵
CMatrix mtxA, mtxB;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1, ",");
mtxB.FromString(m_strMatrix2, ",");
// 复矩阵求逆的全选主元高斯-约当法
if (mtxA.InvertGaussJordan(mtxB))
{
m_strMatrix3 = "实部矩阵的逆矩阵为:\n" + mtxA.ToString(" ");
m_strMatrix3 += "\n\n虚部矩阵的逆矩阵为:\n"+mtxB.ToString(" ");
}
else
m_strMatrix3 = "求解失败";
// 显示数据
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
5.0,7.0,6.0,5.0
7.0,10.0,8.0,7.0
6.0,8.0,10.0,9.0
5.0,7.0,9.0,10.0
*/
// 对称正定矩阵的求逆
void CMatrixCalculatorDlg::OnButton7()
{
// 构造矩阵
CMatrix mtxA;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1, ",");
// 对称正定矩阵的求逆
if (mtxA.InvertSsgj())
m_strMatrix3 = mtxA.ToString(" ");
else
m_strMatrix3 = "求解失败";
// 显示数据
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
10, 5, 4, 3, 2, 1
-1,10, 5, 4, 3, 2
-2,-1,10, 5, 4, 3
-3,-2,-1,10, 5, 4
-4,-3,-2,-1,10, 5
-5,-4,-3,-2,-1,10
*/
// 托伯利兹矩阵求逆的埃兰特方法
void CMatrixCalculatorDlg::OnButton8()
{
// 构造矩阵
CMatrix mtxA;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1, ",");
// 托伯利兹矩阵求逆的埃兰特方法
if (mtxA.InvertTrench())
m_strMatrix3 = mtxA.ToString(" ");
else
m_strMatrix3 = "求解失败";
// 显示数据
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
1.0,2.0,3.0,4.0
5.0,6.0,7.0,8.0
9.0,10.0,11.0,12.0
13.0,14.0,15.0,16.0
3.0,-3.0,-2.0,4.0
5.0,-5.0,1.0,8.0
11.0,8.0,5.0,-7.0
5.0,-1.0,-3.0,-1.0
*/
// 求行列式值的全选主元高斯消去法
void CMatrixCalculatorDlg::OnButton9()
{
// 构造矩阵
CMatrix mtxA;
// 获取数据
UpdateData();
mtxA.FromString(m_strMatrix1, ",");
// 求行列式值的全选主元高斯消去法
double dblValue = mtxA.DetGauss();
m_strMatrix3.Format("%f", dblValue);
// 显示数据
AfxMessageBox(m_strMatrix3, MB_OK|MB_ICONINFORMATION);
}
/*
1.0,2.0,3.0,4.0
5.0,6.0,7.0,8.0
9.0,10.0,11.0,12.0
13.0,14.0,15.0,16.0
17.0,18.0,19.0,20.0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -