Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Нужно написать код С++ для функции

поаро2 Знаток (300), закрыт 1 год назад
Вычислить и вывести на экран таблицу функции y=f(x) в интервале [a,b] с шагом h.
Использовать два метода (с применением for и while). Результаты представить в виде
таблицы:

Лучший ответ
Николай Веселуха Высший разум (356538) 1 год назад
 #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");
}
Остальные ответы
artem ufarkin Знаток (299) 1 год назад
#include<iostream>
#include<conio.h>

int main()
{
std::cout<< " \n"
<< " * * ** \n"
<< " * ** \n"
<< " * * \n"
<< "* * * * * * * * *\n"
<< " * * \n"
<< " * *** * \n"
<< " * *** * \n"
<< " * * *** * * * ";
getch();
return 0;
}
имя фамилия Оракул (65284) 1 год назад
Ты устарел, так же как и твои формулы.
Похожие вопросы