...
Код:
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <clocale>
#include <iostream>
#include <iomanip>
#include <cmath>
WCHAR szTitle[] = L"Conhoid";
WCHAR szWindowClass[] = L"ConhoidWndClass";
//координаты окна
const int WND_X = 500;
const int WND_Y = 50;
const int WND_W = 400;
const int WND_H = 400;
ATOM MyRegisterClass(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
DWORD WINAPI Draw(HDC hdc);
DWORD WINAPI Run(LPVOID lpThreadParameter)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
HWND hCon = GetConsoleWindow();
RECT rc;
MSG msg;
MyRegisterClass(hInstance); //инициализация класса окна
GetWindowRect(hCon, &rc); //получаем положение окна консоли
//создаем окно
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_BORDER, rc.left + WND_X, rc.top + WND_Y, WND_W, WND_H, hCon,
nullptr, hInstance, nullptr);
SetWindowLong(hWnd, GWL_STYLE, 0); //убираем строку заголовка
if (hWnd == nullptr)
{
printf("Error CreateWindow\n");
return 0;
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
//запуск цикла обработки сообщений
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
return RegisterClassExW(&wcex);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
switch (message)
{
case WM_PAINT:
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
Draw(hdc);
EndPaint(hWnd, &ps);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
double myfun(double x, double eps)
{
int i = 1;
double s = 0;
double an;
do
{
an = cos(i * x) / i;
s += an;
i++;
} while (fabs(an) > eps);
return s;
}
DWORD WINAPI Draw(HDC hdc) {
double eps = 1e-6;
double xmin = M_PI / 5.;
double xmax = 9. * xmin + eps;
double y, x;
int n = 16;
double h = (xmax - xmin) / (n - 1.);
float x0, y0;
double f = -3;
x = myfun(x, eps);
y = -log(fabs(2. * sin(x / 2.)));
x0 = x;
y0 = y;
SelectObject(hdc, GetStockObject(WHITE_PEN));
MoveToEx(hdc, 300 + x, 50 + y, nullptr);
while (f <= 3) {
x = myfun(x, eps);
y = -log(fabs(2. * sin(x / 2.)));
if (abs(y - y0) < 1000) {
LineTo(hdc, 300 + x, 50 + y);
}
else {
MoveToEx(hdc, 300 + x, 50 + y, nullptr);
}
f += h;
x0 = x; y0 = y;
}
return 0;
}
int main()
{
setlocale(LC_ALL, "RUSSIAN");
DWORD dwStream = 1;
HANDLE hStream, drawStream;
drawStream = CreateThread(NULL, 0, Run, &dwStream, 0, &dwStream);
double eps = 1e-6;
double xmin = M_PI / 5.;
double xmax = 9. * xmin + eps;
int n = 16;
double h = (xmax - xmin) / (n - 1.);
std::cout << std::string(57, '=') << std::endl;
std::cout << "| x || s || y |" << std::endl;
std::cout << std::string(57, '=') << std::endl;
for (double x = xmin; x <= xmax; x += h)
{
double s = myfun(x, eps);
double y = -log(fabs(2. * sin(x / 2.)));
std::cout << std::fixed << std::setprecision(3) << "|" << x
<< "||\t" << std::setprecision(6) << s
<< "\t||\t" << y << "\t|" << std::endl;
}
std::cout << std::string(57, '=') << std::endl;
std::cin.get();
_getch();
return 0;
}