
5 Текст программы
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <locale.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
void square(HDC hdc, int x, int y, int size, COLORREF color)
{
HBRUSH hBrush = CreateSolidBrush(color);
SelectObject(hdc, hBrush);
Rectangle(hdc, x, y, x + size, y + size);
DeleteObject(hBrush);
}
void Animation()
{
HWND hwnd = GetConsoleWindow();
HDC hdc = GetDC(hwnd);
int width = 800;
int height = 600;
while (true)
{
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = width;
rect.bottom = height;
FillRect(hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
int x = rand() % width;
int y = rand() % height;
int size = rand() % 100 + 50;
COLORREF color = RGB(rand() % 256, rand() % 256, rand() % 256);
for (int i = 0; i < 10; i++)
{
square(hdc, x - i, y - i, size + 2 * i, color);
Sleep(50);
square(hdc, x - i, y - i, size + 2 * i, RGB(0, 0, 0));
}
Sleep(100);
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
if (GetAsyncKeyState(VK_RETURN))
{
break;
}
}
ReleaseDC(hwnd, hdc);
cout << "\n ";
_getch();
}
void AboutAuthor()
{
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\tРасчётно-графическая работа\n";
cout << "\t\t\t\t\t\t по программированию\n\n\n\n";
cout << "\t\t\t\t\t\tАвтор: \x1b[37mКукушкина Алина Антоновна\x1b[35m\n"; cout << "\t\t\t\t\t\tКурс: \x1b[37m1\x1b[35m\n";
cout << "\t\t\t\t\t\tФакультет: \x1b[37mФИТиКС\x1b[35m\n";
cout << "\t\t\t\t\t\tГруппа: \x1b[37mИВТ-234\x1b[35m\n";
cout << "\t\t\t\t\t\tПодгруппа: \x1b[37m1\x1b[35m\n\n\n\n\n\n\n\n\n ";}
void Table()
{
int a = 1.0;
int b = 3.0;
int n = 15.0;
int i;
float X[15]{}, F1[15]{}, F2[15]{}, x, s;
s = fabs(b - a) / (n - 1);
x = a;
for (i = 0; i < n; i++)
{
F1[i] = sqrt(exp(x) - 1);
F2[i] = x * pow(log(x), 2);
X[i] = x;
x = x + s;
}
cout << fixed << setprecision(6);
float f1min = 5, f1max = 0, f2min = 5, f2max = 0;
for (i = 0; i < n; i++)
{
if (F1[i] > f1max)
f1max = F1[i];
if (F1[i] < f1min)
f1min = F1[i];
if (F2[i] > f2max)
f2max = F2[i];
if (F2[i] < f2min)
f2min = F2[i];
}
cout << "\n \x1b[37m|\x1b[35m i \x1b[37m|\x1b[35m x \x1b[37m|\x1b[35m F1 \x1b[37m|\x1b[35m F2 \x1b[37m|\n";
cout << " ---------------------------------------------\x1b[35m\n";
for (i = 0; i < n; ++i)
{
if ((F1[i] == f1max) && (F2[i] == f2max))
cout << " \x1b[37m|\x1b[35m " << i + 1 << (i + 1 < 10 ? " \x1b[37m|\x1b[35m " : " \x1b[37m|\x1b[35m ") << X[i] << " \x1b[37m|\x1b[35m \x1b[32m" << F1[i] << " \x1b[37m|\x1b[35m \x1b[32m" << F2[i] << " \x1b[37m|\x1b[35m" << endl;
else if ((F1[i] == f1min) && (F2[i] == f2min))
cout << " \x1b[37m|\x1b[35m " << i + 1 << (i + 1 < 10 ? " \x1b[37m|\x1b[35m " : " \x1b[37m|\x1b[35m ") << X[i] << " \x1b[37m|\x1b[35m \x1b[31m" << F1[i] << " \x1b[37m|\x1b[35m \x1b[31m" << F2[i] << " \x1b[37m|\x1b[35m" << endl;
else cout << " \x1b[37m|\x1b[35m " << i + 1 << (i + 1 < 10 ? " \x1b[37m|\x1b[35m " : " \x1b[37m|\x1b[35m ") << X[i] << " \x1b[37m|\x1b[35m " << F1[i] << " \x1b[37m|\x1b[35m " << F2[i] << " \x1b[37m|\x1b[35m" << endl;
}
cout << "\n";
cout << " Наибольшие значения функций выделены \x1b[32mзелёным цветом\x1b[35m." << endl;
cout << " Наименьшие значения функций выделены \x1b[31mкрасным цветом\x1b[35m." << endl;
cout << "\n\n\n\n\n\n\n ";
_getch();
}
void Graph()
{
system("cls");
int flag;
HDC screen = GetDC(GetConsoleWindow());
HPEN pen = CreatePen(PS_DASH, 1, RGB(255, 255, 255));
SelectObject(screen, pen);
int start_x = 500;
int start_y = 270;
MoveToEx(screen, start_x - 400, start_y, NULL);
LineTo(screen, start_x + 400, start_y);
MoveToEx(screen, start_x, start_y - 250, NULL);
LineTo(screen, start_x, start_y + 250);
MoveToEx(screen, start_x + 380, start_y + 7, NULL);
LineTo(screen, start_x + 400, start_y);
LineTo(screen, start_x + 380, start_y - 7);
MoveToEx(screen, start_x + 7, start_y - 230, NULL);
LineTo(screen, start_x, start_y - 250);
LineTo(screen, start_x - 7, start_y - 230);
for (int i = -375; i <= 360; i += 25)
{
MoveToEx(screen, start_x + i, start_y - 5, NULL);
LineTo(screen, start_x + i, start_y + 5);
}
for (int i = -210; i <= 240; i += 25)
{
MoveToEx(screen, start_x - 5, start_y + i, NULL);
LineTo(screen, start_x + 5, start_y + i);
}
SetBkMode(screen, TRANSPARENT);
SetTextColor(screen, RGB(255, 255, 255));
TextOut(screen, start_x + 15, start_y - 240, L"Y", 1);
TextOut(screen, start_x + 380, start_y + 15, L"X", 1);
TextOut(screen, start_x - 12, start_y + 2, L"0", 1);
TextOut(screen, start_x + 10, start_y + 6, L"1", 1);
TextOut(screen, start_x - 17, start_y - 27, L"1", 1);
SetTextColor(screen, RGB(255, 0, 153));
TextOut(screen, start_x + 200, start_y + 70, L"y = x * pow(log(x), 2)", 22);
SetTextColor(screen, RGB(51, 51, 255));
TextOut(screen, start_x + 200, start_y + 100, L"y = sqrt(exp(x) - 1)", 20);
HPEN first_pen = CreatePen(PS_DASH, 1, RGB(255, 0, 153));
HPEN second_pen = CreatePen(PS_DASH, 1, RGB(51, 51, 255));
int i = 1;
for (double x = -20.0; x <= 20.0; x += 0.001)
{
SelectObject(screen, first_pen);
MoveToEx(screen, 30 * x + start_x, -30 * x * pow(log(x), 2) + start_y, NULL);
LineTo(screen, 30 * (x + 0.1) + start_x, -30 * x * pow(log(x), 2) + start_y);
SelectObject(screen, second_pen);
MoveToEx(screen, 30 * x + start_x, -30 * sqrt(exp(x) - 1) + start_y, NULL);
LineTo(screen, 30 * (x + 0.1) + start_x, -30 * sqrt(exp(x) - 1) + start_y);
i++;
if (i % 50 == 0) Sleep(0.5);
}
cout << "\n ";
_getch();
}
float funcEquation(float x)
{
return sqrt(log(x * x + 3)) + 2 * x - 3;
}
float bisectionMethod(float a, float b, float e)
{
if (funcEquation(a) * funcEquation(b) >= 0)
{
cout << " \x1b[31mНеверные начальные границы\x1b[35m" << endl;
return -1;
}
float c;
while ((b - a) >= e)
{
c = (a + b) / 2;
if (funcEquation(c) == 0)
break;
else if (funcEquation(c) * funcEquation(a) < 0)
b = c;
else
a = c;
}
return c;
}
float chordMethod(float a, float b, float e)
{
if (funcEquation(a) * funcEquation(b) >= 0)
{
cout << " \x1b[31mНеверные начальные границы\x1b[35m" << endl;
return -1;
}
float c;
while ((b - a) >= e)
{
c = (a * funcEquation(b) - b * funcEquation(a)) / (funcEquation(b) - funcEquation(a));
if (funcEquation(c) == 0)
break;
else if (funcEquation(c) * funcEquation(a) < 0)
b = c;
else
a = c;
}
return c;
}
void Equation()
{
float a, b, e = 0.001;
cout << "\n \x1b[37mf(x) = sqrt(log(x^2 + 3)) + 2x - 3\x1b[35m\n\n";
cout << " Введите нижнюю границу интервала: \x1b[37m";
cin >> a;
cout << "\x1b[35m Введите верхнюю границу интервала: \x1b[37m";
cin >> b;
cout << "\x1b[35m Интервал: \x1b[37m[" << a << ", " << b << "]\x1b[35m\n";
cout << " Точность: \x1b[37m" << e << endl;
float root = bisectionMethod(a, b, e);
if (root != -1)
cout << "\n \x1b[37m>\x1b[35m Метод половинного деления: \x1b[37m" << root << endl;
root = chordMethod(a, b, e);
if (root != -1)
cout << "\n >\x1b[35m Метод хорд: \x1b[37m" << root;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\x1b[35m ";
_getch();
}
float funcIntegral(float x)
{
return exp(x) + exp(-x);
}
float rectangleMethod(int N, float a, float b)
{
float h = (b - a) / N;
float integral = 0;
for (int i = 0; i < N; i++)
{
float x_i = a + i * h;
integral += funcIntegral(x_i) * h;
}
return integral;
}
float trapezoidMethod(int N, float a, float b)
{
float h = (b - a) / N;
float integral = 0;
integral += (funcIntegral(a) + funcIntegral(b)) / 2;
for (int i = 0; i < N; i++)
{
float x_i = a + i * h;
integral += funcIntegral(x_i) * h;
}
return integral;
}
void Integral()
{
int N;
float a, b;
cout << "\n Интеграл: \x1b[37mb|a((exp(x) + exp(-x))dx)\x1b[35m\n\n";
cout << " Введите количество итераций N: \x1b[37m";
cin >> N;
cout << "\x1b[35m Введите нижнюю границу интервала a: \x1b[37m";
cin >> a;
cout << "\x1b[35m Введите верхнюю границу интервала b: \x1b[37m";
cin >> b;
float result = rectangleMethod(N, a, b);
cout << "\n \x1b[37m>\x1b[35m Значение определенного интеграла методом прямоугольников: \x1b[37m" << result << endl;
result = trapezoidMethod(N, a, b);
cout << "\n \x1b[37m>\x1b[35m Значение определенного интеграла методом трапеций: \x1b[37m" << result;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\x1b[35m ";
_getch();
}
void Exit()
{
exit(0);
}
int main()
{
setlocale(LC_ALL, "RUS");
int SelectedOption = 0;
while (true)
{
system("color 05");
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t \x1b[37m~\x1b[35m Меню \x1b[37m~\x1b[35m\n\n";
cout << (SelectedOption == 1 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " Заставка " << (SelectedOption == 1 ? "\x1b[37m<\x1b[35m" : " ") << endl;
cout << (SelectedOption == 2 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " Об авторе " << (SelectedOption == 2 ? "\x1b[37m<\x1b[35m" : " ") << endl;
cout << (SelectedOption == 3 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " Таблица " << (SelectedOption == 3 ? "\x1b[37m<\x1b[35m" : " ") << endl;
cout << (SelectedOption == 4 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " График " << (SelectedOption == 4 ? "\x1b[37m<\x1b[35m" : " ") << endl;
cout << (SelectedOption == 5 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " Уравнение " << (SelectedOption == 5 ? "\x1b[37m<\x1b[35m" : " ") << endl;
cout << (SelectedOption == 6 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " Интеграл " << (SelectedOption == 6 ? "\x1b[37m<\x1b[35m" : " ") << endl;
cout << (SelectedOption == 7 ? "\t\t\t\t\t\t \x1b[37m>\x1b[35m" : "\t\t\t\t\t\t ") << " Выход " << (SelectedOption == 7 ? "\x1b[37m<\x1b[35m" : " ") << endl;
char input = _getch();
switch (input)
{
case 72:
SelectedOption = (SelectedOption > 1) ? SelectedOption - 1 : 7;
break;
case 80:
SelectedOption = (SelectedOption < 7) ? SelectedOption + 1 : 1;
break;
case '\r':
system("cls");
switch (SelectedOption)
{
case 1:
Animation();
break;
case 2:
AboutAuthor();
break;
case 3:
Table();
break;
case 4:
Graph();
break;
case 5:
Equation();
break;
case 6:
Integral();
break;
case 7:
Exit();
break;
}
system("pause");
break;
}
}
return 0;
}