//Copyright (c) LeafCore
#include <windows.h>
#include <math.h>
#include <time.h>
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void draw(HDC);
char szClassName[ ] = "LeafCore";
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
if (!RegisterClassEx(&wincl))
return 0;
hwnd=CreateWindowEx(
0,
szClassName,
"LeafCore",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
1024,
768,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow(hwnd, nFunsterStil);
while(GetMessage(&messages, NULL, 0, 0)) {
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message) {
case WM_PAINT:
hdc=BeginPaint(hwnd, &ps);
HPEN green_pen=CreatePen(PS_SOLID, 1, RGB(0, 127, 0));
HPEN old_pen=(HPEN) SelectObject(hdc, green_pen);
draw(hdc);
SelectObject(hdc, old_pen);
DeleteObject(green_pen);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
void draw(HDC hdc)
{
//q的數量
const int const_q=1000;
double q[const_q];
double x, y;
//q中的最大數
double greatest;
//q中的最小數
double least;
//初始化隨機數產生器
srand(time(0));
//產生q
for (int i=0; i<const_q; i++) {
q[i]=rand()%300+100;
}
//找出q中的最大數和最小數
greatest=q[0];
least=q[0];
for (int i=1; i<const_q; i++) {
if (greatest<q[i]) {
greatest=q[i];
}
if (least>q[i]) {
least=q[i];
}
}
//畫出代表最大數的直線
MoveToEx(hdc, -100*4+450, (int)-greatest+600, 0);
LineTo(hdc, 100*4+450, (int)-greatest+600);
//畫出代表最小數的直線
MoveToEx(hdc, -100*4+450, (int)-least+600, 0);
LineTo(hdc, 100*4+450, (int)-least+600);
//依次畫出x取-100、-99、……、-1時的點
for (int i=-100; i<0; i++) {
x=i;
y=0;
for (int j=0; j<const_q; j++) {
y+=pow(q[j], x);
}
y/=const_q;
y=pow(y, 1/x);
Ellipse(hdc, (int)x*4+450-2, (int)-y+600-2, (int)x*4+450+2, (int)-y+600+2);
}
//畫出x取0時的點
y=1;
for (int i=0; i<const_q; i++) {
y*=pow(q[i], (double)1/const_q);
}
Ellipse(hdc, (int)450-4, (int)-y+600-4, (int)450+4, (int)-y+600+4);
//依次畫出x取1、2、……、99時的點
for (int i=1; i<100; i++) {
x=i;
y=0;
for (int j=0; j<const_q; j++) {
y+=pow(q[j], x);
}
y/=const_q;
y=pow(y, 1/x);
Ellipse(hdc, (int)x*4+450-2, (int)-y+600-2, (int)x*4+450+2, (int)-y+600+2);
}
}