主题:文本文档为何只记录一个数据?
#include"iostream.h"
#include<fstream>
using namespace std;
class One_Improved_Euler_ways //声明类:用于一阶微分方程的改进的欧拉算法
{
private:
float h;
int n;
float YP,YC,x,y;
float first_order_derivative(float x,float y) //声明一个一阶微分方程
{
float f;
f=y-2*x/y; //针对不同的导数关系修改这里即可以!
return f;
}
public:
void result()
{
cout<<"请输入初值x"<<endl;
cin>>x;
cout<<"请输入初值y"<<endl;
cin>>y;
cout<<"请输入步长h"<<endl;
cin>>h;
cout<<"请输入步数n"<<endl;
cin>>n;
int i=0;
while(i<=n)
{
i=i++;
YP=y+h*first_order_derivative(x,y);
x=x+h;
YC=y+h*first_order_derivative(x,YP) ;
y=(YP+YC)/(float)2;
cout<<"y="<<y<<endl;
ofstream outobj("data.txt");
outobj<<y<<endl;
outobj.close();
}
}
}; //类的结束不能忘记分号!
class One_Runge_Kutta_ways //声明类:用于一阶微分方程的龙格-库塔算法
{
private:
float h;
int n;
float K1,K2,K3,K4,x,y;
float first_order_derivative(float x,float y) //声明一个一阶微分方程
{
float f;
f=y-2*x/y; //针对不同的导数关系修改这里即可以!
return f;
}
public:
void result()
{
cout<<"请输入初值x"<<endl;
cin>>x;
cout<<"请输入初值y"<<endl;
cin>>y;
cout<<"请输入步长h"<<endl;
cin>>h;
cout<<"请输入步数n"<<endl;
cin>>n;
int i=0;
while(i<=n)
{
i=i++;
K1=first_order_derivative(x,y);
K2=first_order_derivative(x+h/2,y+h*K1/2);
K3=first_order_derivative(x+h/2,y+h*K2/2);
x=x+h;
K4=first_order_derivative(x,y+h*K3);
y=y+h/6*(K1+2*K2+2*K3+K4);
cout<<"y="<<y<<endl;
ofstream outobj("data.txt");
outobj<<y<<endl;
outobj.close();
}
}
};
//类的结束不能忘记分号!
int main()
{
One_Improved_Euler_ways equation1;
One_Runge_Kutta_ways equation2;
equation1.result();
equation2.result();
return 0;
}
上面是我的C++代码,其中我打开了一个文本文档记录数据,从代码中看出我把每次的Y值记录在了data.txt中,但是当我打开data.txt,只有一个Y值!请问是怎么回事啊?
#include<fstream>
using namespace std;
class One_Improved_Euler_ways //声明类:用于一阶微分方程的改进的欧拉算法
{
private:
float h;
int n;
float YP,YC,x,y;
float first_order_derivative(float x,float y) //声明一个一阶微分方程
{
float f;
f=y-2*x/y; //针对不同的导数关系修改这里即可以!
return f;
}
public:
void result()
{
cout<<"请输入初值x"<<endl;
cin>>x;
cout<<"请输入初值y"<<endl;
cin>>y;
cout<<"请输入步长h"<<endl;
cin>>h;
cout<<"请输入步数n"<<endl;
cin>>n;
int i=0;
while(i<=n)
{
i=i++;
YP=y+h*first_order_derivative(x,y);
x=x+h;
YC=y+h*first_order_derivative(x,YP) ;
y=(YP+YC)/(float)2;
cout<<"y="<<y<<endl;
ofstream outobj("data.txt");
outobj<<y<<endl;
outobj.close();
}
}
}; //类的结束不能忘记分号!
class One_Runge_Kutta_ways //声明类:用于一阶微分方程的龙格-库塔算法
{
private:
float h;
int n;
float K1,K2,K3,K4,x,y;
float first_order_derivative(float x,float y) //声明一个一阶微分方程
{
float f;
f=y-2*x/y; //针对不同的导数关系修改这里即可以!
return f;
}
public:
void result()
{
cout<<"请输入初值x"<<endl;
cin>>x;
cout<<"请输入初值y"<<endl;
cin>>y;
cout<<"请输入步长h"<<endl;
cin>>h;
cout<<"请输入步数n"<<endl;
cin>>n;
int i=0;
while(i<=n)
{
i=i++;
K1=first_order_derivative(x,y);
K2=first_order_derivative(x+h/2,y+h*K1/2);
K3=first_order_derivative(x+h/2,y+h*K2/2);
x=x+h;
K4=first_order_derivative(x,y+h*K3);
y=y+h/6*(K1+2*K2+2*K3+K4);
cout<<"y="<<y<<endl;
ofstream outobj("data.txt");
outobj<<y<<endl;
outobj.close();
}
}
};
//类的结束不能忘记分号!
int main()
{
One_Improved_Euler_ways equation1;
One_Runge_Kutta_ways equation2;
equation1.result();
equation2.result();
return 0;
}
上面是我的C++代码,其中我打开了一个文本文档记录数据,从代码中看出我把每次的Y值记录在了data.txt中,但是当我打开data.txt,只有一个Y值!请问是怎么回事啊?