📄 numericalanalysis.cpp
字号:
// NumericalAnalysis.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "Newton.h"
#include "StrCheck.h"
#include "Lagrange.h"
#include "Cubicspline.h"
#include "Leastsquares.h"
#include "Trapzoid.h"
#include "Simpson.h"
#define MAX_LOADSTRING 100
#define MAX_NUM 999999
// Global Variables:
HWND hwndList ;
HWND hwndStatic1;
HWND hwndStatic2;
HWND hwndStatic3;
HWND hwndStatic4;
HWND hwndStatic5;
HWND hwndStatic6;
HWND hwndEdit_x;
HWND hwndEdit_y;
HWND hwndEdit_der_y1;
HWND hwndEdit_der_y2;
HWND hwndEdit_var;
HWND hwndEdit_weight;
HWND hwndButton_add;
HWND hwndButton_del;
HWND hwndButton_clear;
HWND hwndButton_zoomin;
HWND hwndButton_zoomout;
HWND hwndButton_cal;
HWND hwndOutput;
static double DATA[100][2],x=1,y=1,weight[100];
static int LEN=0;
static int METHOD;
static TCHAR THE_STATUS[50];
static RECT rect;
static double DRAW_DATA[100][2];
TCHAR str[10];
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//////////////// 初始化,重新设定数据 //////////////////
void Initialize_data(int i)
{int j;
if(i==-1)
{for(j=0;j<100;j++)
{DATA[j][0]=DATA[j][1]=MAX_NUM;}
LEN=0;
}
else
{for(j=i;DATA[j+1][0]!=MAX_NUM;j++)
{DATA[j][0]=DATA[j+1][0];DATA[j][1]=DATA[j+1][1];}
DATA[j][0]=DATA[j][1]=MAX_NUM;
LEN=LEN-1;
}
}
///////////// 浮点数转化为字符串 /////////////////
void floattostring(double data)
{TCHAR dtr[10],ktr[10],mtr[10];
int i=0,j=10,k=0;
for(i=0;i<10;i++)
{str[i]='\0';
}
i=0;
if(data<0)
{str[i]='-';i=i+1;}
while(1)
{if(int(data)*10/j!=0)
{itoa(abs(int(data)%j),mtr,10);dtr[k++]=mtr[0];j=j*10;}
else
{break;}
}
for(;k>0;k--)
{str[i]=dtr[k-1];i++;}
for(j=0;j<5;j++)
{data=data*10.0;
itoa(abs(int(data)%10),mtr,10);ktr[j]=mtr[0];
}
if(i==0||(str[0]=='-'&&i==1))
{str[i++]='0';}
if(i<10)
{str[i++]='.';}
for(k=0;k<5&&i<10;k++)
{str[i++]=ktr[k];}
}
//////////////////绘图函数///////////////////////
void display(HWND hWnd)
{
int i;
double max_x=-999999,min_x=999999,max_y=-999999,min_y=999999;
HDC hdc;
PAINTSTRUCT ps;
static HBRUSH myBrush;
static HPEN myPen;
TCHAR temp[10];
//230,60,630,290
hdc=BeginPaint(hWnd,&ps);
myBrush=CreateSolidBrush(RGB(0,0,0));
SelectObject(hdc,myBrush);
Rectangle(hdc,230,60,630,290);
myPen=CreatePen(PS_DASH,2,RGB(255,255,255));
SelectObject(hdc,GetStockObject(WHITE_PEN));
MoveToEx(hdc,240,175,NULL); //X轴
LineTo(hdc,620,175);
MoveToEx(hdc,430,70,NULL); //y轴
LineTo(hdc,430,280);
//y轴箭头
MoveToEx(hdc,430,70,NULL);
LineTo(hdc,425,80);
MoveToEx(hdc,430,70,NULL);
LineTo(hdc,435,80);
//x轴箭头
MoveToEx(hdc,620,175,NULL);
LineTo(hdc,610,170);
MoveToEx(hdc,620,175,NULL);
LineTo(hdc,610,180);
//画尺度
/*for(i=0;DRAW_DATA[i][0]>-999999&&DATA[i][0]<999999;i++)
{if(DATA[i][0]>max_x)
{max_x=DATA[i][0];}
if(DATA[i][0]<min_x)
{min_x=DATA[i][0];}
if(DATA[i][1]>max_y)
{max_y=DATA[i][1];}
if(DATA[i][1]<min_y)
{min_y=DATA[i][1];}
}
if(max_x>-min_x)
{x=max_x/5;}
else
{x=min_x/5;}
if(max_y>-min_y)
{y=max_y/3;}
else
{y=min_y/3;}*/
//画标尺
SetBkColor(hdc,RGB(0,0,0));
SetTextColor(hdc,RGB(255,255,255));
floattostring(x);
wsprintf(temp,"%s",str);
TextOut(hdc,460,180,temp,strlen(temp));
floattostring(y);
wsprintf(temp,"-%s",str);
TextOut(hdc,440,200,temp,strlen(temp));
for(i=1;i<6;i++)
{
MoveToEx(hdc,430-30*i,170,NULL);
LineTo(hdc,430-30*i,175);
MoveToEx(hdc,430+30*i,170,NULL);
LineTo(hdc,430+30*i,175);
}
for(i=0;i<4;i++)
{
MoveToEx(hdc,430,175+30*i,NULL);
LineTo(hdc,435,175+30*i);
MoveToEx(hdc,430,175-30*i,NULL);
LineTo(hdc,435,175-30*i);
}
//画函数图象
//230,60,630,290
for(i=0;i<99&&DRAW_DATA[i][0]>-999999&&DRAW_DATA[i][0]<999999;i++)
{//if(int(DRAW_DATA[i][0]*30/x)+430>=230&&int(DRAW_DATA[i][0]*30/x)+430<=630&&int(DRAW_DATA[i][1]*30/y)+175>=60&&int(DRAW_DATA[i][1]*30/y)+175<=290&&int(DRAW_DATA[i+1][0]*30/x)+430>=230&&int(DRAW_DATA[i+1][0]*30/x)+430<=630&&int(DRAW_DATA[i+1][1]*30/y)+175>=60&&int(DRAW_DATA[i+1][1]*30/y)+175<=290)
MoveToEx(hdc,int(DRAW_DATA[i][0]*30/x)+430,-int(DRAW_DATA[i][1]*30/y)+175,NULL);
LineTo(hdc,int(DRAW_DATA[i+1][0]*30/x)+430,-int(DRAW_DATA[i+1][1]*30/y)+175);
}
for(i=0;DATA[i][0]>-999999&&DATA[i][0]<999999;i++)
{//if(int(DRAW_DATA[i][0]*30/x)+430>=230&&int(DRAW_DATA[i][0]*30/x)+430<=630&&int(DRAW_DATA[i][1]*30/y)+175>=60&&int(DRAW_DATA[i][1]*30/y)+175<=290&&int(DRAW_DATA[i+1][0]*30/x)+430>=230&&int(DRAW_DATA[i+1][0]*30/x)+430<=630&&int(DRAW_DATA[i+1][1]*30/y)+175>=60&&int(DRAW_DATA[i+1][1]*30/y)+175<=290)
Ellipse(hdc,int(DATA[i][0]*30/x)+429,-int(DATA[i][1]*30/y)+174,int(DATA[i][0]*30/x)+431,-int(DATA[i][1]*30/y)+176);
//SetPixel(hdc,int(DATA[i][0]*30/x)+430,-int(DATA[i][1]*30/y)+175,RGB(255,255,0));
}
EndPaint(hWnd,&ps);
}
////////////////////////////////////////////
////////////////////////////////////////////
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_NUMERICALANALYSIS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_NUMERICALANALYSIS);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_NUMERICALANALYSIS);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_NUMERICALANALYSIS;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 640, 480, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i,j,wmId, wmEvent,lbIndex;
double var,data_quo[100][100],der_y1,der_y2;
lagrange cal;
Newton cal2;
Cubicspline cal3;
Leastsquares cal4;
Trapzoid cal5;
Simpson cal6;
COLORREF bkColor ;
TCHAR buffer[10],buffer_x[10],buffer_y[10],temp[10];
floatcheck check;
HMENU hMenu;
switch (message)
{
case WM_CREATE:
bkColor = (COLORREF) GetSysColor(COLOR_BTNFACE);
SetClassLong(hWnd, GCL_HBRBACKGROUND, (long)CreateSolidBrush(bkColor));
wsprintf(THE_STATUS,"The Method: Lagrange ");
//创建数据列表
hwndList = CreateWindow(TEXT("LISTBOX"), NULL, WS_CHILD | WS_VISIBLE |
LBS_NOTIFY |WS_VSCROLL | WS_BORDER ,
0, 0, 0, 0,
hWnd,(HMENU)IDC_DATALIST,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建静态文本“x=”
hwndStatic1 = CreateWindow(TEXT("STATIC"), "x=", WS_VISIBLE | WS_CHILD | SS_LEFT,
31, 0, 20, 20,
hWnd, (HMENU)IDC_STATIC1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建静态文本“f(x)=”
hwndStatic2 = CreateWindow(TEXT("STATIC"), "f(x)=", WS_VISIBLE | WS_CHILD | SS_LEFT,
20, 25, 30, 30,
hWnd, (HMENU)IDC_STATIC1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建静态文本“f'(x)=”
hwndStatic3 = CreateWindow(TEXT("STATIC"), "f'(x)=", WS_VISIBLE | WS_CHILD | SS_LEFT,
17, 50, 30, 30,
hWnd, (HMENU)IDC_STATIC1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建静态文本“Weight=”
hwndStatic6 = CreateWindow(TEXT("STATIC"), "Weight of x=", WS_VISIBLE | WS_CHILD | SS_LEFT,
17, 80, 100, 30,
hWnd, (HMENU)IDC_STATIC1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建静态文本“Input X value, x=”
hwndStatic4 = CreateWindow(TEXT("STATIC"), "Input X value x=", WS_VISIBLE | WS_CHILD | SS_LEFT,
250, 30, 150, 20,
hWnd, (HMENU)IDC_STATIC3,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建静态文本“Method =”
hwndStatic5 = CreateWindow(TEXT("STATIC"), THE_STATUS, WS_VISIBLE | WS_CHILD | SS_LEFT,
280,0, 250, 30,
hWnd, (HMENU)IDC_STATIC1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建编辑框,输入x的值
hwndEdit_x = CreateWindow(TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER| \
ES_WANTRETURN ,
0, 0, 0, 0, hWnd, (HMENU) IDC_EDIT_X,
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//创建编辑框,输入f(x)的值
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -