均值不等式的简单探究

from:点击打开链接



//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);
    }
}


posted @ 2011-07-05 12:46  Podevor  阅读(206)  评论(0编辑  收藏  举报