主题:我的程序错在哪呢?
我是用VS2010编辑的。但我已经将step定义为了0.1,但为什么不关用户怎么输入i,最后结果却都是t从0一直等于到2?为什么不能是t从0等于到i的步骤?好比i=2,t=0,............t=1.........
就是这样?应该怎么改?
// Projectile program 3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include <string.h>
#include <fstream>
#include<cmath>
#define G 9.81
#define PI 3.1415926
#define redian 180
#define tab " "
#define tab1 " "
#define tab2 " "
using namespace std;
class Parabola
{
private:
double v,angle,high;
double Vx,Vy;
protected:
double VX()const{return Vx;};
double VY()const{return Vy;};
double Angle()const{return angle;};
double High()const{return high;};
public:
Parabola(const double v1,const double an,const double h);
virtual double Get_Sx(double t)=0;
virtual double Get_Sy(double t)=0;
virtual double Get_Vy(double t)=0;
virtual ~Parabola(){}
};
class Analytical:public Parabola
{
private:
double Sx_a,Sy_a,Vy_a;
public:
Analytical(const double v1,const double an,const double h):Parabola(v1,an,h){}
//get private members
double Asx()const{return Sx_a;};
double Asy()const{return Sy_a;};
double Avy()const{return Vy_a;};
virtual double Get_Sx(double t);
virtual double Get_Sy(double t);
virtual double Get_Vy(double t);
};
class Eular:public Parabola
{
private:
double Sx_e,Sy_e,Vy_e;
public:
Eular(const double v1,const double an,const double h);
//get private members
double Esx()const{return Sx_e;};
double Esy()const{return Sy_e;};
double Evy()const{return Vy_e;};
virtual double Get_Sx(double t);
virtual double Get_Sy(double t);
virtual double Get_Vy(double t);
};
//parabola public method
Parabola::Parabola(const double v1,const double an,const double h)
{
v=v1;angle=an;high=h;
Vx=cos(PI*angle/redian)*v;
Vy=sin(PI*angle/redian)*v;
}
//Analytical public method
double Analytical::Get_Sx(double t)
{
Sx_a=VX()*t;
return Sx_a;
}
double Analytical::Get_Vy(double t)
{
Vy_a=VY()-G*t;
return Vy_a;
}
double Analytical::Get_Sy(double t)
{
Sy_a=High()+VY()*t-G*t*t/2;
return Sy_a;
}
//Eular public method
Eular::Eular(const double v1,const double an,const double h):Parabola(v1,an,h)
{
Sx_e=0;
Sy_e=High();
Vy_e=VY();
}
double Eular::Get_Sx(double t)
{
Sx_e+=VX()*t;
return Sx_e;
}
double Eular::Get_Vy(double t)
{
Vy_e-=G*t;
return Vy_e;
}
double Eular::Get_Sy(double t)
{
Sy_e+=Vy_e*t;
return Sy_e;
}
const char *file="parabola.txt";
int main(void)
{
int i;
float step=0.1;
double v,angle,h;
double t;
char ch,buffer[100];
int selection,lengh;
cout<<"1.Read\n2.Write\n3.quit\n";
while(cin>>selection&&selection!=3)
{
if(selection<1||selection>3)
cout<<"Wrong Selection\n";
else if(selection==1)
{
cout<<"Input the name of file to be opened:";
cin>>buffer;
lengh=strlen(buffer);
char *fname=new char[lengh];
strcpy(fname,buffer);
strcat(fname,".txt");
fstream fin(fname,ios_base::in);
if(fin.is_open())
while(fin.get(ch))
cout<<ch;
else
cout<<"No Such File\n";
fin.close();
}
else if(selection==2)
{
cout<<"input the V:";
cin>>v;
cout<<"input the angle:";
cin>>angle;
cout<<"input the high:";
cin>>h;
cout<<"what is your steplength ?";
cin>>i;
Analytical an(v,angle,h);
Eular eu(v,angle,h);
ofstream fout(file,ios_base::out);
if(fout.is_open())
{
fout<<tab1<<"Analytical"<<tab<<"Eular"<<tab<<tab2<<"Compare"<<endl;
for(i=0;i<21;i++)
{
t=step*i;
if(t==0||t==1||t==2)
fout<<"t="<<t<<" ";
else
fout<<"t="<<t<<" ";
fout<<"Vy_a="<<an.Get_Vy(t)<<" ";
fout<<"Sy_a="<<an.Get_Sy(t)<<" ";
fout<<"Sx_a="<<an.Get_Sx(t);
if(t==0)
fout<<tab1;
else
fout<<tab2;
fout<<"Vy_e="<<eu.Get_Vy(i)<<" ";
fout<<"Sy_e="<<eu.Get_Sy(i)<<" ";
fout<<"Sx_e="<<eu.Get_Sx(i);
if(t==0)
fout<<tab1;
else
fout<<tab2;
fout<<"(Sxe-Sxa)/Sxa="<<(eu.Esx()-an.Asx())/an.Asx()<<" ";
fout<<"(Sye-Sya)/Sya="<<(eu.Esy()-an.Asy())/an.Asy()<<" ";
fout<<"(Vye-Vya)/Vya="<<(eu.Evy()-an.Avy())/an.Avy()<<endl;
}
}
else
cout<<"Can't Create File\n";
fout.close();
cout<<"Input the name of file to be saved:";
cin>>buffer;
lengh=strlen(buffer);
char *name=new char[lengh];
strcpy(name,buffer);
strcat(name,".txt");
ofstream out(name,ios_base::out);
if(out.is_open())
{
ifstream in(file,ios_base::in);
if(in.is_open())
{
while(in.get(ch))
out<<ch;
}
else
cout<<"Can't Open Such File\n";
in.close();
}
else
cout<<"Can't Create";
out.close();
}
cout<<"1.Read\n2.Write\n3.quit\n";
}
}
就是这样?应该怎么改?
// Projectile program 3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include <string.h>
#include <fstream>
#include<cmath>
#define G 9.81
#define PI 3.1415926
#define redian 180
#define tab " "
#define tab1 " "
#define tab2 " "
using namespace std;
class Parabola
{
private:
double v,angle,high;
double Vx,Vy;
protected:
double VX()const{return Vx;};
double VY()const{return Vy;};
double Angle()const{return angle;};
double High()const{return high;};
public:
Parabola(const double v1,const double an,const double h);
virtual double Get_Sx(double t)=0;
virtual double Get_Sy(double t)=0;
virtual double Get_Vy(double t)=0;
virtual ~Parabola(){}
};
class Analytical:public Parabola
{
private:
double Sx_a,Sy_a,Vy_a;
public:
Analytical(const double v1,const double an,const double h):Parabola(v1,an,h){}
//get private members
double Asx()const{return Sx_a;};
double Asy()const{return Sy_a;};
double Avy()const{return Vy_a;};
virtual double Get_Sx(double t);
virtual double Get_Sy(double t);
virtual double Get_Vy(double t);
};
class Eular:public Parabola
{
private:
double Sx_e,Sy_e,Vy_e;
public:
Eular(const double v1,const double an,const double h);
//get private members
double Esx()const{return Sx_e;};
double Esy()const{return Sy_e;};
double Evy()const{return Vy_e;};
virtual double Get_Sx(double t);
virtual double Get_Sy(double t);
virtual double Get_Vy(double t);
};
//parabola public method
Parabola::Parabola(const double v1,const double an,const double h)
{
v=v1;angle=an;high=h;
Vx=cos(PI*angle/redian)*v;
Vy=sin(PI*angle/redian)*v;
}
//Analytical public method
double Analytical::Get_Sx(double t)
{
Sx_a=VX()*t;
return Sx_a;
}
double Analytical::Get_Vy(double t)
{
Vy_a=VY()-G*t;
return Vy_a;
}
double Analytical::Get_Sy(double t)
{
Sy_a=High()+VY()*t-G*t*t/2;
return Sy_a;
}
//Eular public method
Eular::Eular(const double v1,const double an,const double h):Parabola(v1,an,h)
{
Sx_e=0;
Sy_e=High();
Vy_e=VY();
}
double Eular::Get_Sx(double t)
{
Sx_e+=VX()*t;
return Sx_e;
}
double Eular::Get_Vy(double t)
{
Vy_e-=G*t;
return Vy_e;
}
double Eular::Get_Sy(double t)
{
Sy_e+=Vy_e*t;
return Sy_e;
}
const char *file="parabola.txt";
int main(void)
{
int i;
float step=0.1;
double v,angle,h;
double t;
char ch,buffer[100];
int selection,lengh;
cout<<"1.Read\n2.Write\n3.quit\n";
while(cin>>selection&&selection!=3)
{
if(selection<1||selection>3)
cout<<"Wrong Selection\n";
else if(selection==1)
{
cout<<"Input the name of file to be opened:";
cin>>buffer;
lengh=strlen(buffer);
char *fname=new char[lengh];
strcpy(fname,buffer);
strcat(fname,".txt");
fstream fin(fname,ios_base::in);
if(fin.is_open())
while(fin.get(ch))
cout<<ch;
else
cout<<"No Such File\n";
fin.close();
}
else if(selection==2)
{
cout<<"input the V:";
cin>>v;
cout<<"input the angle:";
cin>>angle;
cout<<"input the high:";
cin>>h;
cout<<"what is your steplength ?";
cin>>i;
Analytical an(v,angle,h);
Eular eu(v,angle,h);
ofstream fout(file,ios_base::out);
if(fout.is_open())
{
fout<<tab1<<"Analytical"<<tab<<"Eular"<<tab<<tab2<<"Compare"<<endl;
for(i=0;i<21;i++)
{
t=step*i;
if(t==0||t==1||t==2)
fout<<"t="<<t<<" ";
else
fout<<"t="<<t<<" ";
fout<<"Vy_a="<<an.Get_Vy(t)<<" ";
fout<<"Sy_a="<<an.Get_Sy(t)<<" ";
fout<<"Sx_a="<<an.Get_Sx(t);
if(t==0)
fout<<tab1;
else
fout<<tab2;
fout<<"Vy_e="<<eu.Get_Vy(i)<<" ";
fout<<"Sy_e="<<eu.Get_Sy(i)<<" ";
fout<<"Sx_e="<<eu.Get_Sx(i);
if(t==0)
fout<<tab1;
else
fout<<tab2;
fout<<"(Sxe-Sxa)/Sxa="<<(eu.Esx()-an.Asx())/an.Asx()<<" ";
fout<<"(Sye-Sya)/Sya="<<(eu.Esy()-an.Asy())/an.Asy()<<" ";
fout<<"(Vye-Vya)/Vya="<<(eu.Evy()-an.Avy())/an.Avy()<<endl;
}
}
else
cout<<"Can't Create File\n";
fout.close();
cout<<"Input the name of file to be saved:";
cin>>buffer;
lengh=strlen(buffer);
char *name=new char[lengh];
strcpy(name,buffer);
strcat(name,".txt");
ofstream out(name,ios_base::out);
if(out.is_open())
{
ifstream in(file,ios_base::in);
if(in.is_open())
{
while(in.get(ch))
out<<ch;
}
else
cout<<"Can't Open Such File\n";
in.close();
}
else
cout<<"Can't Create";
out.close();
}
cout<<"1.Read\n2.Write\n3.quit\n";
}
}