Advertisement
peterzig

[PIU] Kalkulator

Nov 7th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.53 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <sstream>
  8. #include <Strsafe.h>
  9. #include <vector>
  10. MSG Komunikat;
  11.  
  12. WCHAR buffor[11];
  13. float liczba1;
  14. int zmienna = 0;
  15.  
  16. HWND hText;
  17. HWND hprzycisk1;
  18. HWND hprzycisk2;
  19. HWND hprzycisk3;
  20. HWND hprzycisk4;
  21. HWND hprzycisk5;
  22. HWND hprzycisk6;
  23. HWND hprzycisk7;
  24. HWND hprzycisk8;
  25. HWND hprzycisk9;
  26. HWND hprzycisk0;
  27. HWND hprzyciskdodac;
  28. HWND hprzyciskodjac;
  29. HWND hprzyciskrazy;
  30. HWND hprzyciskpodzielic;
  31. HWND hprzyciskpomnozyc;
  32. HWND hprzyciskwynik;
  33. HWND hprzyciskkropka;
  34. HWND hprzyciskkasuj;
  35.  
  36. float bufor()
  37. {
  38.     GetWindowText(hText, buffor, GetWindowTextLength(hText) + 1);
  39.     float schowek = _wtof(buffor);
  40.     return schowek;
  41. }
  42. void pobierz()
  43. {
  44.     GetWindowText(hText, buffor, GetWindowTextLength(hText) + 1);
  45.     liczba1 = _wtof(buffor);
  46.     SetWindowText(hText, L"");
  47. }
  48.  
  49. void wstaw(float liczba)    // Wrzucenie zmienonej liczby do WCHARA;
  50. {
  51.     std::wostringstream woss;
  52.     woss << liczba;
  53.     std::wstring ws = woss.str();
  54.     const wchar_t* cwc = ws.c_str();
  55.     std::vector<wchar_t> buf(cwc, cwc + (ws.size() + 1));
  56.     wchar_t* liczba2 = &buf[0];
  57.     SetWindowText(hText, liczba2);
  58. }
  59. void dodaj()
  60. {
  61.  
  62.     GetWindowText(hText, buffor, GetWindowTextLength(hText) + 1);
  63.  
  64.     // Wyciągnięcie liczby z WCHARA
  65.  
  66.     float wynik = liczba1 + _wtof(buffor);
  67.     if (wynik<1000000)
  68.         wstaw(wynik);
  69.     else SetWindowText(hText, L"Za duzo");
  70. }
  71.  
  72. void odejmij()
  73. {
  74.     GetWindowText(hText, buffor, GetWindowTextLength(hText) + 1);
  75.  
  76.     // Wyciągnięcie liczby z WCHARA
  77.  
  78.     float wynik = liczba1 - _wtof(buffor);
  79.  
  80.  
  81.     wstaw(wynik);
  82.  
  83.  
  84. }
  85.  
  86. void podziel()
  87. {
  88.  
  89.     GetWindowText(hText, buffor, GetWindowTextLength(hText) + 1);
  90.  
  91.     if (_wtof(buffor) != 0)
  92.     {
  93.         float wynik = liczba1 / _wtof(buffor);
  94.  
  95.  
  96.         wstaw(wynik);
  97.  
  98.  
  99.     }
  100.     else SetWindowText(hText, L"Blad");
  101.  
  102.  
  103.  
  104. }
  105.  
  106. void pomnoz()
  107. {
  108.     GetWindowText(hText, buffor, GetWindowTextLength(hText) + 1);
  109.     float wynik = liczba1 * _wtof(buffor);
  110.  
  111.  
  112.     wstaw(wynik);
  113.     zmienna = 0;
  114.  
  115. }
  116.  
  117.  
  118.  
  119. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  120. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  121. {
  122.     srand(time(NULL));
  123.     // WYPEŁNIANIE STRUKTURY
  124.     WNDCLASSEX wc;
  125.  
  126.     wc.cbSize = sizeof(WNDCLASSEX);
  127.     wc.style = CS_VREDRAW | CS_HREDRAW;
  128.     wc.lpfnWndProc = WndProc;
  129.     wc.cbClsExtra = 0;
  130.     wc.cbWndExtra = 0;
  131.     wc.hInstance = hInstance;
  132.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  133.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  134.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  135.     wc.lpszMenuName = NULL;
  136.     wc.lpszClassName = L"Klasa okna";
  137.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  138.  
  139.     // REJESTROWANIE KLASY OKNA
  140.     if (!RegisterClassEx(&wc))
  141.     {
  142.         MessageBox(NULL, L"Nie udalo sie zarejestrowac klasy okna.", L"Blad",
  143.             MB_ICONEXCLAMATION | MB_OK);
  144.         return 1;
  145.     }
  146.  
  147.     // TWORZENIE OKNA
  148.     HWND hwnd;
  149.  
  150.     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, L"Klasa okna", L"Kalkulator", WS_OVERLAPPEDWINDOW ^ (WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX),
  151.         CW_USEDEFAULT, CW_USEDEFAULT, 220, 292, NULL, NULL, hInstance, NULL);
  152.  
  153.     if (hwnd == NULL)
  154.     {
  155.         MessageBox(NULL, L"Nie udało się stworzyć okna.", L"Blad", MB_ICONEXCLAMATION);
  156.         return 1;
  157.     }
  158.  
  159.     ShowWindow(hwnd, nCmdShow); // Pokaż okienko...
  160.  
  161.     hprzycisk1 = CreateWindowEx(0, L"BUTTON", L"1", WS_CHILD | WS_VISIBLE, 0, 50, 50, 50, hwnd, (HMENU)1, hInstance, NULL);
  162.     hprzycisk2 = CreateWindowEx(0, L"BUTTON", L"2", WS_CHILD | WS_VISIBLE, 50, 50, 50, 50, hwnd, (HMENU)2, hInstance, NULL);
  163.     hprzycisk3 = CreateWindowEx(0, L"BUTTON", L"3", WS_CHILD | WS_VISIBLE, 100, 50, 50, 50, hwnd, (HMENU)3, hInstance, NULL);
  164.     hprzycisk4 = CreateWindowEx(0, L"BUTTON", L"4", WS_CHILD | WS_VISIBLE, 0, 100, 50, 50, hwnd, (HMENU)4, hInstance, NULL);
  165.     hprzycisk5 = CreateWindowEx(0, L"BUTTON", L"5", WS_CHILD | WS_VISIBLE, 50, 100, 50, 50, hwnd, (HMENU)5, hInstance, NULL);
  166.     hprzycisk6 = CreateWindowEx(0, L"BUTTON", L"6", WS_CHILD | WS_VISIBLE, 100, 100, 50, 50, hwnd, (HMENU)6, hInstance, NULL);
  167.     hprzycisk7 = CreateWindowEx(0, L"BUTTON", L"7", WS_CHILD | WS_VISIBLE, 0, 150, 50, 50, hwnd, (HMENU)7, hInstance, NULL);
  168.     hprzycisk8 = CreateWindowEx(0, L"BUTTON", L"8", WS_CHILD | WS_VISIBLE, 50, 150, 50, 50, hwnd, (HMENU)8, hInstance, NULL);
  169.     hprzycisk9 = CreateWindowEx(0, L"BUTTON", L"9", WS_CHILD | WS_VISIBLE, 100, 150, 50, 50, hwnd, (HMENU)9, hInstance, NULL);
  170.     hprzycisk0 = CreateWindowEx(0, L"BUTTON", L"0", WS_CHILD | WS_VISIBLE, 50, 200, 50, 50, hwnd, (HMENU)0, hInstance, NULL);
  171.     hprzyciskkropka = CreateWindowEx(0, L"BUTTON", L".", WS_CHILD | WS_VISIBLE, 0, 200, 50, 50, hwnd, (HMENU)11, hInstance, NULL);
  172.     hprzyciskwynik = CreateWindowEx(0, L"BUTTON", L"=", WS_CHILD | WS_VISIBLE, 100, 200, 50, 50, hwnd, (HMENU)12, hInstance, NULL);
  173.     hprzyciskdodac = CreateWindowEx(0, L"BUTTON", L"+", WS_CHILD | WS_VISIBLE, 150, 50, 50, 50, hwnd, (HMENU)13, hInstance, NULL);
  174.     hprzyciskodjac = CreateWindowEx(0, L"BUTTON", L"-", WS_CHILD | WS_VISIBLE, 150, 100, 50, 50, hwnd, (HMENU)14, hInstance, NULL);
  175.     hprzyciskrazy = CreateWindowEx(0, L"BUTTON", L"X", WS_CHILD | WS_VISIBLE, 150, 150, 50, 50, hwnd, (HMENU)15, hInstance, NULL);
  176.     hprzyciskpodzielic = CreateWindowEx(0, L"BUTTON", L"/", WS_CHILD | WS_VISIBLE, 150, 200, 50, 50, hwnd, (HMENU)16, hInstance, NULL);
  177.     hprzyciskkasuj = CreateWindowEx(0, L"BUTTON", L"←", WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)10, hInstance, NULL);
  178.  
  179.  
  180.  
  181.     hText = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT, 50, 0, 150, 50, hwnd, (HMENU)18, hInstance, NULL);
  182.     UpdateWindow(hwnd);
  183.     HFONT hFont = CreateFont(40, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");
  184.     SendMessage(hText, WM_SETFONT, WPARAM(hFont), TRUE);
  185.     SetWindowText(hText, buffor);
  186.     SendMessage(hprzyciskkasuj, WM_SETFONT, WPARAM(hFont), TRUE);
  187.  
  188.  
  189.  
  190.     // Pętla komunikatów
  191.     while (GetMessage(&Komunikat, NULL, 0, 0))
  192.     {
  193.         TranslateMessage(&Komunikat);
  194.         DispatchMessage(&Komunikat);
  195.     }
  196.  
  197.  
  198.  
  199.     return Komunikat.wParam;
  200.  
  201. }
  202.  
  203.  
  204. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  205. {
  206.  
  207.     srand(time(NULL));
  208.  
  209.     static RECT r;
  210.  
  211.  
  212.  
  213.  
  214.     switch (msg)
  215.     {
  216.  
  217.     case WM_CLOSE:
  218.         if (MessageBox(NULL, L"Czy chcesz zamknąć to okno ?", L"Zamykanie",
  219.             MB_ICONQUESTION | MB_YESNO) == 6)
  220.  
  221.             DestroyWindow(hwnd);
  222.         break;
  223.     case WM_DESTROY:
  224.         PostQuitMessage(0);
  225.         break;
  226.     case WM_SIZE:
  227.     {
  228.         InvalidateRect(hwnd, &r, TRUE);
  229.     }
  230.     case WM_PAINT: {
  231.         PAINTSTRUCT ps;
  232.         HDC hdcOkno;
  233.         GetClientRect(hwnd, &r);
  234.         hdcOkno = BeginPaint(hwnd, &ps);
  235.  
  236.  
  237.  
  238.  
  239.         EndPaint(hwnd, &ps);
  240.     }
  241.  
  242.                    break;
  243.  
  244.     case WM_COMMAND:
  245.         switch (LOWORD(wParam))
  246.         {
  247.  
  248.         case 18:
  249.             break;
  250.  
  251.         case 10:
  252.         {
  253.             SetWindowText(hText, L"");
  254.             liczba1 = 0;
  255.             zmienna = 0;
  256.         }
  257.         case 11:
  258.         {
  259.  
  260.         }break;
  261.         case 12:
  262.         {
  263.             if (zmienna == 1)dodaj();
  264.             else if (zmienna == 2)odejmij();
  265.             else if (zmienna == 3)pomnoz();
  266.             else if (zmienna == 4)podziel();
  267.         }break;
  268.         case 13:
  269.         {
  270.             pobierz();
  271.             zmienna = 1;
  272.         }break;
  273.         case 14:
  274.         {
  275.             pobierz();
  276.             zmienna = 2;
  277.         }break;
  278.         case 15:
  279.         {
  280.             pobierz();
  281.             zmienna = 3;
  282.         }break;
  283.         case 16:
  284.         {
  285.             pobierz();
  286.             zmienna = 4;
  287.         }break;
  288.         default:
  289.  
  290.             if (GetWindowTextLength(hText) == 0)
  291.                 wstaw(LOWORD(wParam));
  292.             else if (GetWindowTextLength(hText) < 6) {
  293.  
  294.  
  295.                 wstaw((bufor() * 10) + (LOWORD(wParam)));
  296.             }
  297.  
  298.         }break;
  299.  
  300.  
  301.         break;
  302.  
  303.     default:
  304.         return DefWindowProc(hwnd, msg, wParam, lParam);
  305.     }
  306.  
  307.     return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement