📄 newtoninterpdlg.cpp
字号:
// NewtonInterpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NewtonInterp.h"
#include "NewtonInterpDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CNewtonInterpDlg dialog
CNewtonInterpDlg::CNewtonInterpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNewtonInterpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNewtonInterpDlg)
m_Point = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
Data_x=NULL;
Data_y=NULL;
Data_Juncha=NULL;
Data_Row=0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CNewtonInterpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNewtonInterpDlg)
DDX_Control(pDX, IDC_EDIT_Data, m_InputData);
DDX_Text(pDX, IDC_EDIT_Inputpoint, m_Point);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNewtonInterpDlg, CDialog)
//{{AFX_MSG_MAP(CNewtonInterpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_Inputdata, OnBUTTONInputdata)
ON_BN_CLICKED(IDC_BUTTON_Interp, OnBUTTONInterp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNewtonInterpDlg message handlers
BOOL CNewtonInterpDlg::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 CNewtonInterpDlg::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 CNewtonInterpDlg::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 CNewtonInterpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CNewtonInterpDlg::OnBUTTONInputdata()
{
UpdateData();
CString m_szFileName,m_szFilePathName;
CString Data;
CString temp;
CString temp1,temp2;
int line=0;
CFileDialog dlg(true,NULL,NULL,OFN_OVERWRITEPROMPT,"txt(*.txt)|*.txt",NULL);
if(dlg.DoModal()==IDOK)
{
m_szFileName = dlg.GetFileName();
m_szFilePathName = dlg.GetPathName();
}
else
return;
CStdioFile file;
file.Open(m_szFilePathName,CFile::modeRead);
while(file.ReadString(temp))
{
Data+=temp;
Data_Row++;
Data+="\r\n";
}
GetDlgItem(IDC_EDIT_Data)->SetWindowText(Data);
file.Close();
Data_x=new double[Data_Row];
Data_y=new double[Data_Row];
Data_Juncha=new double* [Data_Row];
for(int i=0;i<Data_Row;i++)
{
Data_Juncha[i]=new double[Data_Row];
}
int pos=0;
int leng=0;
char da[20];
char* buffer;
buffer=da;
temp="";
file.Open(m_szFilePathName,CFile::modeRead);
int p=0;
while(file.ReadString(temp))
{
leng=temp.GetLength();
pos=temp.Find(" ");
temp1=temp.Left(pos);
temp2=temp.Right(leng-pos-1);
Data_x[p]=atof(temp1);
Data_y[p]=atof(temp2);
p++;
}
file.Close();
}
void CNewtonInterpDlg::OnBUTTONInterp()
{
UpdateData();
// GetDlgItem(IDC_EDIT_Result)->SetWindowText(result);
if(m_Point=="")
{
MessageBox("请输入要进行插值的点!");
return;
}
InputPoint=atof(m_Point);
CalculateJunCha();
CString result;
result.Format("%.6f",InterpResult);
GetDlgItem(IDC_EDIT_Result)->SetWindowText(result);
double wucha=1.0;
for(int i=0;i<Data_Row;i++)
{
wucha=wucha*(Data_x[i]-InputPoint);
}
wucha=wucha*Data_Juncha[Data_Row-1][Data_Row-1];
if(wucha<0)
{
wucha=wucha*(-1.0);
}
result.Format("%.10f",wucha);
GetDlgItem(IDC_EDIT_Wucha)->SetWindowText(result);
ShowJuncha();
}
void CNewtonInterpDlg::CalculateJunCha()
{
int i;
for(i = 0;i<Data_Row;i++)
Data_Juncha[i][0] = Data_y[i];
for(i = 1;i<Data_Row;i++)
{
for(int j = 1;j<=i;j++)
{
Data_Juncha[i][j] = (Data_Juncha[i][j-1]-Data_Juncha[i-1][j-1])/(Data_x[i]-Data_x[i-j]);
}
}
double temp1 = 1.00;
double temp2 = Data_Juncha[0][0];
for(i = 1;i<Data_Row;i++)
{
temp1 = temp1*(InputPoint-Data_x[i-1]);
temp2 = temp2+Data_Juncha[i][i]*temp1;
}
InterpResult=temp2;
}
void CNewtonInterpDlg::ShowJuncha()
{
CString temp,temp1;
CString Total="";
for(int i = 1;i<Data_Row;i++)
{
temp1="";
temp="";
for(int j = 1;j<=i;j++)
{
temp.Format("%f%s",Data_Juncha[i][j],"\t");
temp1+=temp;
}
Total+=temp1;
Total+="\r\n";
}
GetDlgItem(IDC_EDIT_JunchaData)->SetWindowText(Total);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -