Linguagem C - Divisão, Somatório e Fatorial

main.html
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include "resource.h"

//rdiv = Resultado da divisão
//rsmt = Resultado do somatório
static HWND num1, num2, rdiv, snum1, rsmt, doacao, fat_in, rfat;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void addMenus(HWND);
int fatorial(int);

HMENU MenuPrincipal;

HFONT hFonte;

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 */

    /* 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 = (HBRUSH)(COLOR_3DFACE+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(hI, ID_ICONE);
    wc.hIconSm       = LoadIcon(hI, ID_ICONE);

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

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "WindowClass",
        "Divisão e Somatório",
        WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        500, /* width */
        400, /* 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;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
    switch(msg) {
        case WM_CREATE:
            addMenus(hwnd);
            // --------- Divisão ---------
            CreateWindow(
                TEXT("static"),TEXT("Divisão"), WS_VISIBLE | WS_CHILD,
                30, 20, 480, 20,
                hwnd, NULL, NULL, NULL
            );

            num1 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | DT_CENTER,
                30, 50, 40, 20,
                hwnd, NULL, NULL, NULL
            );

            num2 = CreateWindow(
                TEXT("edit"), TEXT(""), WS_VISIBLE | WS_CHILD | DT_CENTER,
                100, 50, 40, 20,
                hwnd, NULL, NULL, NULL
            );

            CreateWindow(
                TEXT("button"), TEXT("Calcular"), WS_VISIBLE|
                WS_CHILD | WS_BORDER,
                180, 50, 80, 20,
                hwnd, (HMENU) ID_BTN1, NULL, NULL
            );

            rdiv = CreateWindow(
                TEXT("edit"),TEXT(""),WS_VISIBLE|WS_CHILD|WS_BORDER,
                300, 50, 100, 20,
                hwnd, NULL, NULL, NULL
            );
            // --------- Somatório ---------
            CreateWindow(
                TEXT("static"),TEXT("Somatório de"),WS_VISIBLE|WS_CHILD,
                30, 130, 120, 20,
                hwnd, NULL, NULL, NULL
            );
            
            snum1 = CreateWindow(
                TEXT("edit"),TEXT(""),WS_VISIBLE|WS_CHILD|WS_BORDER,
                130, 130, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            
            CreateWindow(
                TEXT("static"), TEXT("="), WS_VISIBLE | WS_CHILD,
                210, 130, 20, 20,
                hwnd, NULL, NULL, NULL
            );
            
            rsmt = CreateWindow(
                TEXT("edit"),TEXT(""),WS_VISIBLE|WS_CHILD|WS_BORDER,
                240, 130, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            
            CreateWindow(
                TEXT("button"), TEXT("Calcular"), WS_VISIBLE |
                WS_CHILD | WS_BORDER,
                320, 130, 80, 20,
                hwnd, (HMENU) ID_BTN2, NULL, NULL
            );
            // --------- Fatorial ---------
            CreateWindow(
                TEXT("static"),TEXT("Fatorial de"),WS_VISIBLE|WS_CHILD,
                30, 180, 120, 20,
                hwnd, NULL, NULL, NULL
            );
            
            fat_in = CreateWindow(
                TEXT("edit"),TEXT(""),WS_VISIBLE|WS_CHILD|WS_BORDER,
                130, 180, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            
            CreateWindow(
                TEXT("static"), TEXT("="), WS_VISIBLE | WS_CHILD,
                210, 180, 20, 20,
                hwnd, NULL, NULL, NULL
            );
            
            rfat = CreateWindow(
                TEXT("edit"),TEXT(""),WS_VISIBLE|WS_CHILD|WS_BORDER,
                240, 180, 60, 20,
                hwnd, NULL, NULL, NULL
            );
            
            CreateWindow(
                TEXT("button"), TEXT("Calcular"), WS_VISIBLE |
                WS_CHILD | WS_BORDER,
                320, 180, 80, 20,
                hwnd, (HMENU) ID_BTN3, NULL, NULL
            );
            
            doacao = CreateWindow(
                TEXT("static"),ID_PIX , WS_VISIBLE | WS_CHILD,
                30, 250, 450, 40,
                hwnd, NULL, NULL, NULL
            );
            hFonte=CreateFont(27,0,0,0,0,FALSE,0,0,0,0,0,0,0,"Arial");
            SendMessage(doacao, WM_SETFONT, (WPARAM) hFonte, TRUE);
            break;

        case WM_COMMAND:
            if(LOWORD(wP)==ID_BTN1) {
                float n1 = GetWindowTextLength(num1)+1;
                float n2 = GetWindowTextLength(num2)+1;

                static char t1[40], t2[40];

                GetWindowText(num1, t1, n1);
                GetWindowText(num2, t2, n2);
                //d de divisão
                float d = atof(t1) / atof(t2);

                static char tresult[50];

                sprintf(tresult,"%s \xf7 %s = %.2f", t1, t2, d);

                SetWindowText(rdiv, tresult);
                return 0;
            }
            if(LOWORD(wP)==ID_BTN2) {
                unsigned long somatorio(int n) {
                    int i, x;
                    for(i=1, x=0; i<=n; i++) {
                        x = x+i;
                    }
                    return(x);
                }
                int z = GetWindowTextLength(snum1)+1;

                static char st1[40];

                GetWindowText(snum1, st1, z);

                int sresult = somatorio(atoi(st1));

                static char stamanho[40];

                itoa(sresult, stamanho, 10);

                SetWindowText(rsmt, stamanho);
            }
            if(LOWORD(wP)==ID_BTN3) {
                int w = GetWindowTextLength(fat_in)+1;

                static char v1[40];

                GetWindowText(fat_in, v1, w);
                
                int r_fatorial = fatorial(atoi(v1));

                static char fresult[50];

                itoa(r_fatorial, fresult, 10);

                SetWindowText(rfat,fresult);
            }
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wP, lP);
    }
    return 0;
}

void addMenus(HWND hwnd) {
    MenuPrincipal = CreateMenu();
    HMENU subMenu = CreateMenu();

    AppendMenu(MenuPrincipal, 16, (UINT_PTR) subMenu, "Arquivo");
    AppendMenu(subMenu, 0, NULL, "Info");
    AppendMenu(subMenu, 0, NULL, "Dados da Tela");
    AppendMenu(subMenu, 0, NULL, "Sair");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Fonte");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Ajuda");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Opções");
    AppendMenu(MenuPrincipal, MF_STRING, NULL, "Hora");

    SetMenu(hwnd, MenuPrincipal);
}

int fatorial(int n) {
    int x;
    if(n==1) {
        return 1;
    }
    x = fatorial(n-1)*n;
    return x;
}

resource.html




/*** File: resource.h ***/

#define ID_BTN1  11
#define ID_BTN2  12
#define ID_BTN3  13

#define ID_ICONE 14

#define ID_PIX "PIX: marcio_dsm@protonmail.com"

resourcerc.html




/*** File: resource.rc ***/

#include <afxres.h>
#include "resource.h"

ID_ICONE ICON "project1.ico"

Comentários