用mfc写一个求解一元二次方程的程序
时间: 2023-05-25 22:06:25 浏览: 153
求解一元二次方程的程序
以下是使用MFC编写的求解一元二次方程的程序,实现了输入系数和求解根的功能:
// QuadraticEquationDlg.cpp : implementation file
//
#include "stdafx.h"
#include "QuadraticEquation.h"
#include "QuadraticEquationDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CQuadraticEquationDlg dialog
CQuadraticEquationDlg::CQuadraticEquationDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_QUADRATICEQUATION_DIALOG, pParent)
, m_stra(_T(""))
, m_strb(_T(""))
, m_strc(_T(""))
, m_strRoot1(_T(""))
, m_strRoot2(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CQuadraticEquationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_A, m_stra);
DDV_MaxChars(pDX, m_stra, 10);
DDX_Text(pDX, IDC_EDIT_B, m_strb);
DDV_MaxChars(pDX, m_strb, 10);
DDX_Text(pDX, IDC_EDIT_C, m_strc);
DDV_MaxChars(pDX, m_strc, 10);
DDX_Text(pDX, IDC_EDIT_ROOT1, m_strRoot1);
DDX_Text(pDX, IDC_EDIT_ROOT2, m_strRoot2);
}
BEGIN_MESSAGE_MAP(CQuadraticEquationDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CALC, &CQuadraticEquationDlg::OnBnClickedButtonCalc)
END_MESSAGE_MAP()
// CQuadraticEquationDlg message handlers
BOOL CQuadraticEquationDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 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 CQuadraticEquationDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CQuadraticEquationDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CQuadraticEquationDlg::OnBnClickedButtonCalc()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); // get input values
double a = _wtof(m_stra);
double b = _wtof(m_strb);
double c = _wtof(m_strc);
double delta = b * b - 4 * a * c; // calculate delta
if (delta >= 0) { // if the roots are real numbers
double root1 = (-b + sqrt(delta)) / (2 * a);
double root2 = (-b - sqrt(delta)) / (2 * a);
// convert roots to formatted strings
m_strRoot1.Format(_T("%.2f"), root1);
m_strRoot2.Format(_T("%.2f"), root2);
}
else { // if the roots are complex numbers
double realPart = -b / (2 * a);
double imagPart = sqrt(-delta) / (2 * a);
CString strRealPart, strImagPart;
strRealPart.Format(_T("%.2f"), realPart);
strImagPart.Format(_T("%.2fi"), imagPart);
// combine real and imaginary parts into roots
m_strRoot1 = strRealPart + _T("+") + strImagPart;
m_strRoot2 = strRealPart + _T("-") + strImagPart;
}
UpdateData(FALSE); // update output values
}
阅读全文