This repository has been archived on 2026-07-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

126 lines
3.9 KiB
C++

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <winbgim.h> // Äëÿ ðàáîòû ñ ãðàôèêîé íà Windows
// Ôóíêöèÿ äëÿ ðàñ÷åòà çíà÷åíèÿ y = (x + 4)^2(x + 3) - 6
double func(double x) {
return pow((x + 4), 2) * (x + 3) - 6;
}
int main() {
// Èíèöèàëèçàöèÿ ãðàôè÷åñêîãî ðåæèìà
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Óñëîâèå çàäà÷è
setcolor(WHITE);
outtextxy(10, 10, "Íàéäèòå íàèáîëüøåå çíà÷åíèå ôóíêöèè y = (x+4)^2(x+3) - 6");
outtextxy(10, 30, "íà îòðåçêå [-5, -3.5].");
// Íàéäåì çíà÷åíèÿ ôóíêöèè â ãðàíè÷íûõ òî÷êàõ è êðèòè÷åñêèõ òî÷êàõ
double x1 = -5, x2 = -3.5;
double x_critical1 = -4, x_critical2 = -10.0 / 3.0;
double y1 = func(x1);
double y2 = func(x2);
double y_critical1 = func(x_critical1);
double y_critical2 = func(x_critical2);
// Íàéäåì íàèáîëüøåå çíà÷åíèå
double max_value = y1;
double max_x = x1;
if (y2 > max_value) {
max_value = y2;
max_x = x2;
}
if (y_critical1 > max_value) {
max_value = y_critical1;
max_x = x_critical1;
}
if (y_critical2 > max_value) {
max_value = y_critical2;
max_x = x_critical2;
}
// Âûâîä ðåøåíèÿ íà ýêðàí
char solutionText[500];
sprintf(solutionText, "Íàèáîëüøåå çíà÷åíèå ôóíêöèè = %.2f ïðè x = %.2f", max_value, max_x);
outtextxy(10, 50, solutionText);
// Ïîñòðîåíèå ãðàôèêà ôóíêöèè íà èíòåðâàëå [-5, -3.5]
setcolor(YELLOW);
// Íàéäåì ðàçìåðû ãðàôè÷åñêîãî îêíà
int width = getmaxx();
int height = getmaxy();
int graph_x_start = 50; // Ëåâûé êðàé ãðàôèêà
int graph_y_start = height / 2; // Ñðåäíÿÿ ëèíèÿ äëÿ îñè y
int graph_width = width - 100; // Øèðèíà ãðàôèêà
// Óñòàíîâèì ìàñøòàá ïî îñè X è Y
double x_scale = graph_width / (x2 - x1); // Ìàñøòàá ïî X
double y_scale = 30; // Ìàñøòàá ïî Y (çàâèñèò îò ôóíêöèè)
// Îòîáðàæàåì îñè
setcolor(WHITE);
line(graph_x_start, 0, graph_x_start, height); // Îñü Y
line(0, graph_y_start, width, graph_y_start); // Îñü X
// Ðàçìåòêà (ñåòêà)
setcolor(LIGHTGRAY);
// Ãîðèçîíòàëüíûå ëèíèè (ïî Y)
for (int i = -5; i <= 5; i++) {
int y_line_pos = graph_y_start - i * y_scale;
line(0, y_line_pos, width, y_line_pos);
char label[10];
sprintf(label, "%d", i);
outtextxy(graph_x_start - 30, y_line_pos - 5, label);
}
// Âåðòèêàëüíûå ëèíèè (ïî X)
for (double i = x1; i <= x2; i += 0.5) {
int x_line_pos = graph_x_start + (int)((i - x1) * x_scale);
line(x_line_pos, 0, x_line_pos, height);
char label[10];
sprintf(label, "%.1f", i);
outtextxy(x_line_pos - 10, graph_y_start + 10, label);
}
// Îòðèñîâêà ôóíêöèè
setcolor(YELLOW);
for (int i = graph_x_start; i < graph_x_start + graph_width; i++) {
double x = x1 + (i - graph_x_start) / x_scale;
double y = func(x);
int graph_y = graph_y_start - (int)(y * y_scale);
putpixel(i, graph_y, YELLOW);
}
// Îòìå÷àåì òî÷êó ìàêñèìóìà
int max_graph_x = graph_x_start + (int)((max_x - x1) * x_scale);
int max_graph_y = graph_y_start - (int)(max_value * y_scale);
setcolor(RED);
circle(max_graph_x, max_graph_y, 5);
outtextxy(max_graph_x + 10, max_graph_y - 10, "Max");
// Çàïèñü ðåøåíèÿ â ôàéë
FILE *file = fopen("Ðåøåíèå_çàäà÷è.txt", "w");
if (file == NULL) {
printf("Îøèáêà ïðè îòêðûòèè ôàéëà!\n");
return 1;
}
fprintf(file, "Íàèáîëüøåå çíà÷åíèå ôóíêöèè íà îòðåçêå [-5, -3.5]:\n");
fprintf(file, "Ìàêñèìóì = %.2f ïðè x = %.2f\n", max_value, max_x);
fclose(file);
printf("Îòâåò çàïèñàí â ôàéë 'Ðåøåíèå_çàäà÷è.txt'.\n");
// Îæèäàíèå ââîäà äëÿ çàêðûòèÿ ãðàôè÷åñêîãî îêíà
getch();
closegraph();
return 0;
}