помогите сделать вывод в файл
#include<conio.h>
#include<windows.h>
using namespace std;
void ru(char text[100])
{
char str[100];
CharToOem(text,str);
cout<<str;
}
double function(double x,double y)
{
return y*(1-x);
}
void main()
{
ru("Численное решение ДУ 1-го порядка\n\n");
ru(" ДУ: y'=y*(1-x)\n\n");
double xa,xb,y[25],yk,k0,k1,k2,k3,h;
int n;
cout<<" x(a): "; cin>>xa;
cout<<" x(b): "; cin>>xb;
cout<<" y(a): "; cin>>y[0];
cout<<" h : "; cin>>h;
ru("\n Схема Эйлера\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
y[n+1]=y[n]+h*function(xa+n*h,y[n]);
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
ru("\n Схема Адамса\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
yk=y[n]+h*function(xa+n*h,y[n]);
y[n+1]=y[n]+h/2*(function(xa+n*h,y[n])+function(xa+n*h+h,yk));
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
ru("\n Схема Рунге-Кутта\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
k0=h*function(xa+n*h,y[n]);
k1=h*function(xa+n*h+h/2,y[n]+k0/2);
k2=h*function(xa+n*h+h/2,y[n]+k1/2);
k3=h*function(xa+n*h+h,y[n]+k2);
y[n+1]=y[n]+(k0+2*k1+2*k2+k3)/6;
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
getch();}
программа рабочая, просто надо результаты вывести в текстовый файл. помогите пожалуйста
Код:
#include<iostream>
#include<conio.h>
#include<windows.h>
#include <fstream>
using namespace std;
void ru(char text[100])
{
char str[100];
CharToOem(text,str);
cout<<str;
}
double function(double x,double y)
{
return y*(1-x);
}
int main()
{
ru("Численное решение ДУ 1-го порядка\n\n");
ru(" ДУ: y'=y*(1-x)\n\n");
double xa,xb,y[25],yk,k0,k1,k2,k3,h;
int n;
cout<<" x(a): ";
cin>>xa;
cout<<" x(b): ";
cin>>xb;
cout<<" y(a): ";
cin>>y[0];
cout<<" h : ";
cin>>h;
ru("\n Схема Эйлера\n");
cout<<"\n y[0]="<<y[0]<<"\n";
streambuf *psbuf;
ofstream filestr;
filestr.open ("вывод.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
n=0;
do
{
y[n+1]=y[n]+h*function(xa+n*h,y[n]);
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
ru("\n Схема Адамса\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
yk=y[n]+h*function(xa+n*h,y[n]);
y[n+1]=y[n]+h/2*(function(xa+n*h,y[n])+function(xa+n*h+h,yk));
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
ru("\n Схема Рунге-Кутта\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
k0=h*function(xa+n*h,y[n]);
k1=h*function(xa+n*h+h/2,y[n]+k0/2);
k2=h*function(xa+n*h+h/2,y[n]+k1/2);
k3=h*function(xa+n*h+h,y[n]+k2);
y[n+1]=y[n]+(k0+2*k1+2*k2+k3)/6;
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
filestr.close();
//getch();
}
#include<conio.h>
#include<windows.h>
#include <fstream>
using namespace std;
void ru(char text[100])
{
char str[100];
CharToOem(text,str);
cout<<str;
}
double function(double x,double y)
{
return y*(1-x);
}
int main()
{
ru("Численное решение ДУ 1-го порядка\n\n");
ru(" ДУ: y'=y*(1-x)\n\n");
double xa,xb,y[25],yk,k0,k1,k2,k3,h;
int n;
cout<<" x(a): ";
cin>>xa;
cout<<" x(b): ";
cin>>xb;
cout<<" y(a): ";
cin>>y[0];
cout<<" h : ";
cin>>h;
ru("\n Схема Эйлера\n");
cout<<"\n y[0]="<<y[0]<<"\n";
streambuf *psbuf;
ofstream filestr;
filestr.open ("вывод.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
n=0;
do
{
y[n+1]=y[n]+h*function(xa+n*h,y[n]);
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
ru("\n Схема Адамса\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
yk=y[n]+h*function(xa+n*h,y[n]);
y[n+1]=y[n]+h/2*(function(xa+n*h,y[n])+function(xa+n*h+h,yk));
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
ru("\n Схема Рунге-Кутта\n");
cout<<"\n y[0]="<<y[0]<<"\n";
n=0;
do
{
k0=h*function(xa+n*h,y[n]);
k1=h*function(xa+n*h+h/2,y[n]+k0/2);
k2=h*function(xa+n*h+h/2,y[n]+k1/2);
k3=h*function(xa+n*h+h,y[n]+k2);
y[n+1]=y[n]+(k0+2*k1+2*k2+k3)/6;
cout<<" y["<<n+1<<"]="<<y[n+1]<<"\n";
n++;
}
while(xa+n*h<xb);
filestr.close();
//getch();
}