程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 均值不等式的簡單探究

均值不等式的簡單探究

編輯:關於C語言

\

 

 

 

 

 

 

 

 

 

 

 

 

 

\

 

 

 

 

 

 

 

 

 

 

 

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved