2. Разработать программный модуль для нахождения значений функции
для задаваемого диапазона и шага изменения
. Разработать тесты для программного модуля.#include <iostream>
#include <conio>
#include <cctype>
#include <cmath>
using namespace std;
float check_figure(char * str);
float function(float x);
int main(int argc, char* argv[])
{
float left, right, step, x, y;
char *str;
char answer;
do {
clrscr();
//left
cout << "Введите левую границу интервала: ";
left = check_figure(str);
//right
cout << "Введите правую границу интервала: ";
right = check_figure(str);
//step
cout << "Введите шаг: ";
step = check_figure(str);
if (step < 0) {
cout << "Преобразую шаг (он должен быть положительным)." << endl;
step = fabs(step);
}
if (left > right) {
for (x = left; x >= right; x = x - step)
cout << "x = " << x << "y(x) = " << function(x) << endl;
} else {
for (x = left; x <= right; x = x + step)
cout << "x = " << x << "y(x) = " << function(x) << endl;
}
cout << "Для продолжения нажмите 'y', для выхода - любую клавишу." << endl;
answer = getch();
} while (tolower(answer) == 'y');
return 0;
}
//---------------
float check_figure(char * str)
{
while (1) {
cin >> str;
if (atof(str) == 0 ) {
cout << "Неверный ввод!" << endl;
} else {
break;
}
}
return atof(str);
}
//---------------
float function(float x)
{
if (x < -5)
return 3*x;
if (x >= 0)
return x*x;
return 1/x;
}
//---------------