Equação do Segundo Grau em C/C++ - Post reescrito em 04/10/2023

main.html
/*
    Equação do Segundo Grau
*/
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define BTN_BUSCA 1

void Controles(HWND);

static HWND c1, c2, c3, r;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
    float x1, x2, Delta;
    switch(msg) {
        case WM_CREATE:
            Controles(hwnd);
            return 0;
        case WM_COMMAND:
            if(LOWORD(wP)==BTN_BUSCA) {
                int n1 = GetWindowTextLength(c1)+1;
                int n2 = GetWindowTextLength(c2)+1;
                int n3 = GetWindowTextLength(c3)+1;
            	static char a[50],b[50],c[50],resultado[50];
                GetWindowText(c1, a, n1);
                GetWindowText(c2, b, n2);
                GetWindowText(c3, c, n3);
                Delta = (atoi(b)*atoi(b))-4*atoi(a)*atoi(c);
                if(Delta<0) {
                    MessageBox(
                        hwnd,
                        "Não existem raizes reais!",
                        "Delta < 0",
                        MB_OK
                    );
                    exit(0);
                }
                x1 = (-atoi(b)+sqrt(Delta)) / (2*atoi(a));
                x2 = (-atoi(b)-sqrt(Delta)) / (2*atoi(a));
                sprintf(resultado,"x1 = %.2f x2 = %.2f",x1,x2);
                SetWindowText(r, resultado);
            }
            return 0;
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }
		
        default:
            return DefWindowProc(hwnd, msg, wP, lP);
    }
    return 0;
}

void Controles(HWND hwnd) {
    c1 = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD,
        10, 70, 40, 20,
        hwnd, NULL, NULL, NULL
    );
    c2 = CreateWindow(
        TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD,
        70, 70, 40, 20,
        hwnd, NULL, NULL, NULL
    );
    c3 = CreateWindow(
        TEXT("edit"), TEXT(""),WS_VISIBLE | WS_CHILD,
        130, 70, 40, 20,
        hwnd, NULL, NULL, NULL
    );
    CreateWindow(
        TEXT("button"),TEXT("Calcular"),
        WS_VISIBLE | WS_CHILD | WS_BORDER,
        190, 70, 100, 20,
        hwnd, (HMENU) BTN_BUSCA, NULL, NULL
    );
    r = CreateWindow(
        TEXT("edit"), TEXT(""),WS_VISIBLE|WS_CHILD|WS_DISABLED,
        310, 70, 150, 20,
        hwnd, NULL, NULL, NULL
    );
}

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR lpCmdLine, 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 */

    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", "Programação para Windows",
        WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        500, /* width */
        200, /* 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); /* Send it to WndProc */
    }
    return msg.wParam;
}

Comentários