#include #include #include #include #include using namespace std; class Function { public: Function() = default; void options_for(double a, double b, double h, const function& fn) { if (a > b) swap(a, b); const auto stop = b + h / 10.0; for (auto x = a; x <= stop; x += h) { if (fabs(x) < 1e-15) x = 0.0; xy[x] = fn(x); } } void options_while(double a, double b, double h, const function& fn) { if (a > b) swap(a, b); const auto stop = b + h / 10.0; auto x = a; while (x <= stop) { if (fabs(x) < 1e-15) x = 0.0; xy[x] = fn(x); x += h; } } void print(streamsize xw, streamsize xp, streamsize yw, streamsize yp)const { cout.setf(ios::fixed); for (auto& [x, y] : xy) { cout << setw(xw) << setprecision(xp) << x; cout << setw(yw) << setprecision(yp) << y; puts(""); } cout.unsetf(ios::fixed); } void clear() { xy.clear(); } private: map xy; }; int main() { auto neg = [](double x) { return -pow(x, 2); }; auto pos = [](double x) { return sqrt(exp(x) - 1.0); }; const auto a = -0.5; const auto b = 0.5; const auto h = 0.1; Function func; func.options_for(a, 0, h, neg); func.options_for(0, b, h, pos); func.print(8, 1, 20, 15); puts(""); func.clear(); func.options_while(a, 0, h, neg); func.options_while(0, b, h, pos); func.print(8, 1, 20, 15); system("pause > nul"); }
Использовать два метода (с применением for и while). Результаты представить в виде
таблицы: