C/C++

Создание таблицы в C++

ЗАДАНИЕ 7
Постройте таблицу значений кусочно-заданной функции на отрезке [Xнач, Xкон] с шагом dX по образцу:
Значения величин Xнач, Xкон, dX, а также коэффициенты функций введите с клавиатуры.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>
#include <string>
using namespace std;
double real(
const char* msg,
const double a = -numeric_limits<double>::max(),
const double b = numeric_limits<double>::max()
) {
auto value = 0.0;
do {
cout << msg;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
} while (value <= a || value >= b);
return value;
}
double y1(const double a, const double b, const double x) {
if (x > 0) return -b * sin(x);
return sqrt(pow(a, 2) + pow(x + a, 2));
}
double y2(const double a, const double b, const double x) {
if (x > -1) return log10(b + sqrt(x + 1));
return log10(b / pow(x, 2));
}
double y3(const double a, const double b, const double x) {
static const double pi = 3.1415926535897932;
if (x > 0) return 1.0 + b * log(1 + x);
return fabs(pow(x, 2) + pi * x + 1.0);
}
int main() {
const auto a = real("a: ");
const auto b = real("b: ", 0);
const auto xn = real("Xn: ");
const auto xk = real("Xn: ", xn);
const auto dx = real("dX: ", 0);
const auto end = xk + dx / 10.0;
cout.setf(ios::fixed);
string line(37, '-');
cout
<< line << '\n'
<< setw(4) << "x" << setw(3) << "|"
<< setw(6) << "y1" << setw(4) << "|"
<< setw(6) << "y2" << setw(4) << "|"
<< setw(6) << "y3" << setw(4) << "|"
<< '\n' << line << '\n';
for (auto x = xn; x < end; x += dx) {
cout
<< setw(5) << setprecision(2) << x << setw(2) << "|"
<< setw(8) << setprecision(4) << y1(a, b, x) << setw(2) << "|"
<< setw(8) << y2(a, b, x) << setw(2) << "|"
<< setw(8) << y3(a, b, x) << setw(2) << "|" << '\n';
}
cout << line << '\n';
system("pause > nul");
}
Aleks Rusak
Aleks Rusak
98 718
Лучший ответ