Calcular a Média em C/C++

main.html
#include <windows.h>
#include <stdio.h>

#define BTN_MEDIA 1

void Controles(HWND);

static HWND num1, num2, num3, num4, r;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
    switch(msg) {
        case WM_CREATE:
            Controles(hwnd);
            return 0;
        case WM_COMMAND:
            if(LOWORD(wP)==BTN_MEDIA) {
                int n1 = GetWindowTextLength(num1)+1;
                int n2 = GetWindowTextLength(num2)+1;
                int n3 = GetWindowTextLength(num3)+1;
                int n4 = GetWindowTextLength(num4)+1;
				
                static char t1[50], t2[50], t3[50], t4[50];
				
                GetWindowText(num1, t1, n1);
                GetWindowText(num2, t2, n2);
                GetWindowText(num3, t3, n3);
                GetWindowText(num4, t4, n4);

                float res = (atof(t1)+atof(t2)+atof(t3)+atof(t4))/4;
				
                char media[50];

                sprintf(media, "A Média é %.2f", res);

                SetWindowText(r, media);

                FILE *arq;
                arq = fopen("arquivo.txt", "w+");
                fwrite(media, sizeof(arq),10,arq);
                fclose(arq);
            }
            break;
        /* Upon destruction, tell the main thread to stop */
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }
		
        default:
            return DefWindowProc(hwnd, msg, wP, lP);
    }
    return 0;
}

void Controles(HWND hwnd) {
    CreateWindowW(
        L"static", L"1 Nota: ", WS_VISIBLE | WS_CHILD,
        10, 20, 60, 20,
        hwnd, NULL, NULL, NULL
    );
    num1 = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
        120, 20, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindowW(
        L"static", L"2 Nota: ", WS_VISIBLE | WS_CHILD,
        10, 60, 60, 20,
        hwnd, NULL, NULL, NULL
    );
    num2 = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
        120, 60, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindowW(
        L"static", L"3 Nota: ", WS_VISIBLE | WS_CHILD,
        10, 100, 60, 20,
        hwnd, NULL, NULL, NULL
    );
    num3 = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
        120, 100, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindowW(
        L"static", L"4 Nota: ", WS_VISIBLE | WS_CHILD,
        10, 140, 60, 20,
        hwnd, NULL, NULL, NULL
    );
    num4 = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER,
        120, 140, 100, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindow(
        TEXT("button"),TEXT("Calcular"),WS_VISIBLE|WS_CHILD|WS_BORDER,
        10, 180, 80, 20,
        hwnd, (HMENU) BTN_MEDIA, NULL, NULL
    );
    r = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_DISABLED,
        100, 180, 120, 20,
        hwnd, NULL, NULL, NULL
    );
}

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR szCL, int iCS) {
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG msg; /* A temporary location for all messages */

    /* zero out the struct and set the stuff we want to modify */
    memset(&wc,0,sizeof(wc));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hI;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetSysColorBrush(COLOR_MENUHILIGHT);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc)) {
        MessageBox(
            NULL,
            "Window Registration Failed!",
            "Error!",
            MB_ICONEXCLAMATION | MB_OK
        );
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE, "WindowClass", "Média",
        WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        300, /* width */
        250, /* height */
        NULL, NULL, hI, NULL
    );

    if(hwnd == NULL) {
        MessageBox(
            NULL,
            "Window Creation Failed!",
            "Error!",
            MB_ICONEXCLAMATION | MB_OK
        );
        return 0;
    }

    while(GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

Comentários