#include <windows.h> 
#include <string.h> 
#include <stdlib.h> 
#include <math.h> 
  
LPSTR NazwaKlasy = "KlasaOkna"; 
HWND hLiczbaX, hLiczbaY, hLiczbaW, hText; 
  
// obs-uga zdarze1, wywo-ana przes patla komunikat!w 
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) 
{ 
    switch( msg ) 
    { 
    case WM_COMMAND:  //obs-uga przycisku 
        DWORD dlug, X, Y, W;  
        LPSTR BuforX,BuforY,BuforW; 
		int Blad,liczba,przes;
		Blad=0;
 		char znak[20];
        for (int i=0;i<20;i++) znak[i]=' ';
		switch( wParam ) 
        { 
        case 101:  //dodawanie
			X=0;
			Y=0;
			W=0;
			dlug = GetWindowTextLength( hLiczbaX )+1; 
            BuforX =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaX, BuforX, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforX[i]<'0')||(BuforX[i]>'9')) Blad++;
				else {
					X=X*10+BuforX[i]-48;
				}  
			}
			dlug = GetWindowTextLength( hLiczbaY )+1; 
            BuforY =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaY, BuforY, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforY[i]<'0')||(BuforY[i]>'9')) Blad++; 
				else {
					Y=Y*10+BuforY[i]-48;
				}  
			}
			X=X+Y;
			W=X;
			dlug=0;
			do {
				dlug++;
				X=floor(X/10);
				}while (X>0);
            dlug--;
			BuforW =( LPSTR ) GlobalAlloc( GPTR, dlug );
			for (int i=0; i<=dlug; i++) {
				liczba=W-10*floor(W/10);
				W=floor(W/10);
				znak[dlug-i]= (char)(((int)'0')+liczba);
			}
			strcpy(BuforW,znak);           
			if (Blad>0) SetWindowText( hLiczbaW, "ERROR" );
			else SetWindowText( hLiczbaW, BuforW );
            SetWindowText(hText, "X + Y =");
			GlobalFree( BuforW ); 
            GlobalFree( BuforX ); 
            GlobalFree( BuforY ); 
            break; 
        case 102:  //odejmowanie 
			X=0;
			Y=0;
			W=0;
			dlug = GetWindowTextLength( hLiczbaX )+1; 
            BuforX =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaX, BuforX, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforX[i]<'0')||(BuforX[i]>'9')) Blad++;
				else {
					X=X*10+BuforX[i]-48;
				}  
			}
			dlug = GetWindowTextLength( hLiczbaY )+1; 
            BuforY =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaY, BuforY, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforY[i]<'0')||(BuforY[i]>'9')) Blad++; 
				else {
					Y=Y*10+BuforY[i]-48;
				}  
			}
			dlug=0;
			if (X>=Y) {
				X=X-Y;
				przes=0;	
			} else{
				X=Y-X;
				przes=1;
				znak[0]='-';
			}
			W=X;
			dlug--;
			do {
				dlug++;
				X=floor(X/10);
				}while (X>0);
			BuforW =( LPSTR ) GlobalAlloc( GPTR, dlug+przes );
			for (int i=0; i<=dlug; i++) {
				liczba=W-10*floor(W/10);
				W=(int)floor(W/10);
				znak[przes+dlug-i]= (char)(((int)'0')+liczba);
			}
			strcpy(BuforW,znak);           
			if (Blad>0) SetWindowText( hLiczbaW, "ERROR" );
			else SetWindowText( hLiczbaW, BuforW );
            SetWindowText(hText, "X - Y =");
			GlobalFree( BuforW ); 
            GlobalFree( BuforX ); 
            GlobalFree( BuforY ); 
            break; 
        case 103:  //mnożenie
			X=0;
			Y=0;
			W=0;
			dlug = GetWindowTextLength( hLiczbaX )+1; 
            BuforX =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaX, BuforX, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforX[i]<'0')||(BuforX[i]>'9')) Blad++;
				else {
					X=X*10+BuforX[i]-48;
				}  
			}
			dlug = GetWindowTextLength( hLiczbaY )+1; 
            BuforY =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaY, BuforY, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforY[i]<'0')||(BuforY[i]>'9')) Blad++; 
				else {
					Y=Y*10+BuforY[i]-48;
				}  
			}
			X=X*Y;
			W=X;
			dlug=0;
			do {
				dlug++;
				X=floor(X/10);
				}while (X>0);
            dlug--;
			BuforW =( LPSTR ) GlobalAlloc( GPTR, dlug );
			for (int i=0; i<=dlug; i++) {
				liczba=W-10*floor(W/10);
				W=floor(W/10);
				znak[dlug-i]= (char)(((int)'0')+liczba);
			}
			strcpy(BuforW,znak);           
			if (Blad>0) SetWindowText( hLiczbaW, "ERROR" );
			else SetWindowText( hLiczbaW, BuforW );
            SetWindowText(hText, "X * Y =");
			GlobalFree( BuforW ); 
            GlobalFree( BuforX ); 
            GlobalFree( BuforY ); 
            break; 
        case 104:  //dzielenie
			X=0;
			Y=0;
			W=0;
			dlug = GetWindowTextLength( hLiczbaX )+1; 
            BuforX =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaX, BuforX, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforX[i]<'0')||(BuforX[i]>'9')) Blad++;
				else {
					X=X*10+BuforX[i]-48;
				}  
			}
			dlug = GetWindowTextLength( hLiczbaY )+1; 
            BuforY =( LPSTR ) GlobalAlloc( GPTR, dlug ); 
            GetWindowText( hLiczbaY, BuforY, dlug ); 
			for (int i=0; i<=dlug-2; i++) {
				if ((BuforY[i]<'0')||(BuforY[i]>'9')) Blad++; 
				else {
					Y=Y*10+BuforY[i]-48;
				}  
			}
			if (Y!=0)
				X=floor(X/Y);
			else {
				X=0;
				Blad++;
			}
			W=X;
			dlug=0;
			do {
				dlug++;
				X=floor(X/10);
				}while (X>0);
            dlug--;
			BuforW =( LPSTR ) GlobalAlloc( GPTR, dlug );
			for (int i=0; i<=dlug; i++) {
				liczba=W-10*floor(W/10);
				W=floor(W/10);
				znak[dlug-i]= (char)(((int)'0')+liczba);
			}
			strcpy(BuforW,znak);           
			if (Blad>0) SetWindowText( hLiczbaW, "ERROR" );
			else SetWindowText( hLiczbaW, BuforW );
            SetWindowText(hText, "X / Y = ~");
			GlobalFree( BuforW ); 
            GlobalFree( BuforX ); 
            GlobalFree( BuforY ); 
            break; 
        case 105:  //symboliczny numer/identyfikator przycisku 
            SetWindowText(hText, "WYNIK");
            SetWindowText( hLiczbaX, "0" ); 
            SetWindowText( hLiczbaY, "0" ); 
            SetWindowText( hLiczbaW, "0" ); 
            break; 
        } 
        break; 
    case WM_CLOSE:   
        DestroyWindow( hwnd ); 
        break; 
    case WM_DESTROY: 
        PostQuitMessage( 0 ); 
        break; 
    default:    
        return DefWindowProc( hwnd, msg, wParam, lParam ); 
    } 
      
    return 0; 
} 
  
  
// g-!wna funkcja programu 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) 
{ 
    WNDCLASSEX wc;   
    HWND hwnd; 
    MSG Komunikat; 
  
   // wype-nienie struktury okna 
    wc.cbSize = sizeof( WNDCLASSEX ); 
    wc.style = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); 
    wc.hCursor = LoadCursor( NULL, IDC_ARROW ); 
    wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 ); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = NazwaKlasy; 
    wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); 
      
    //rejestracja klasy okna 
    if( !RegisterClassEx( & wc ) ) 
    { 
        MessageBox( NULL, "Odmawia rejestracji okna!", "Problem...", 
        MB_ICONEXCLAMATION | MB_OK ); 
        return 1; 
    } 
    // utworzenie okna 
    hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, NazwaKlasy, "KALKULATOR - Bartosz Jabłonecki.", WS_OVERLAPPEDWINDOW, 
    CW_USEDEFAULT, CW_USEDEFAULT, 400, 260, NULL, NULL, hInstance, NULL ); 
    if( hwnd == NULL ) 
    { 
        MessageBox( NULL, "Odmowa utworzenia okna!", "B-Üd...", MB_ICONEXCLAMATION ); 
        return 1; 
    } 
    ShowWindow( hwnd, nCmdShow ); 
    UpdateWindow( hwnd ); 
      
  
    //  Tutaj znajduje sia g-!wna cza? programu, 
    //  gdzie znajdujÜ sia wszystkie funkcje wej?cia i przesy-ane sÜ do WinProc. 
          
    CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", " KALKULATOR - Bartosz Jabłonecki", WS_CHILD | WS_VISIBLE ,  
        40, 40, 280, 20, hwnd, NULL, hInstance, NULL ); 
    CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "X", WS_CHILD | WS_VISIBLE ,  
        40, 80, 80, 20, hwnd, NULL, hInstance, NULL ); 
    CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "Y", WS_CHILD | WS_VISIBLE ,  
        140, 80, 80, 20, hwnd, NULL, hInstance, NULL ); 
    hText = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "WYNIK", WS_CHILD | WS_VISIBLE ,  
        240, 80, 80, 20, hwnd, NULL, hInstance, NULL ); 
    hLiczbaX = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "0", WS_CHILD | WS_VISIBLE | WS_BORDER,  
        40, 100, 80, 30, hwnd, NULL, hInstance, NULL ); 
    hLiczbaY = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "0", WS_CHILD | WS_VISIBLE | WS_BORDER,  
        140, 100, 80, 30, hwnd, NULL, hInstance, NULL ); 
    hLiczbaW = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "0", WS_CHILD | WS_VISIBLE | WS_BORDER,  
        240, 100, 80, 30, hwnd, NULL, hInstance, NULL ); 
    HWND hPrzycisk1 = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " + ", WS_CHILD | WS_VISIBLE,  
        40, 150, 40, 30, hwnd, (HMENU)101, hInstance, NULL ); 
    HWND hPrzycisk2 = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " - ", WS_CHILD | WS_VISIBLE,  
        90, 150, 40, 30, hwnd, (HMENU)102, hInstance, NULL ); 
    HWND hPrzycisk3 = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " * ", WS_CHILD | WS_VISIBLE,  
        140, 150, 40, 30, hwnd, (HMENU)103, hInstance, NULL ); 
    HWND hPrzycisk4 = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " / ", WS_CHILD | WS_VISIBLE,  
        190, 150, 40, 30, hwnd, (HMENU)104, hInstance, NULL ); 
    HWND hPrzyciskC = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " C ", WS_CHILD | WS_VISIBLE,  
        240, 150, 80, 30, hwnd, (HMENU)105, hInstance, NULL ); 
          
    //patla komunikat!w 
    while( GetMessage( &Komunikat, NULL, 0, 0 ) >0 ) 
    { 
        TranslateMessage( &Komunikat );  
        DispatchMessage( &Komunikat );   //przes-anie komunikat!w do WinProc 
    } 
    return Komunikat.wParam; 
} 

