——————————————————————————————————————————————————— 该题之前有bug,不知道是当时复制错了还是怎么样,现已修改。如下: mvector.h
#ifndef mVector_H_
#define mVector_H_
#include
#include
using namespace std;
static const double Rad_to_deg=45.0/atan(1.0);
class mVector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set\_mag(){
mag=sqrt(x\*x+y\*y);
};
void set\_ang(){
if(x==0.0&&y==0.0)
ang=0.0;
else
ang=atan2(y,x);
};
void set\_x(){
x=mag\*cos(ang);
};
void set\_y(){
y=mag\*sin(ang);
};
//from here
//static const double Rad\_to\_deg=45.0/atan(1.0);
public:
mVector(){
x=y=mag=ang=0.0;
mode=RECT;
};
mVector(double n1,double n2,Mode form=RECT){
mode=form;
if(form==RECT)
{
x=n1;
y=n2;
set\_mag();
set\_ang();
}
else if(form==POL)
{
mag=n1;
ang=n2;
set\_x();
set\_y();
}
else
{
cout<<"Incorrect 3rd argument to mVector()--";
cout<<"mVector set to 0\\n";
x=y=mag=0.0;
mode=RECT;
}
};
~mVector(){
};
void reset(double n1,double n2,Mode form=RECT){
mode=form;
if(form==RECT)
{
x=n1;
y=n2;
set\_mag();
set\_ang();
}
else if(form==POL)
{
mag=n1;
ang=n2;
set\_x();
set\_y();
}
else
{
cout<<"Incorrect 3rd argument to mVector()--";
cout<<"mVector set to 0\\n";
x=y=mag=0.0;
mode=RECT;
}
};
double xval()const{return x;};
double yval()const{return y;};
double magval()const {return mag;};
double angval()const {return ang;};
void polar\_mode(){
mode=POL;
};
void rect\_mode(){
mode=RECT;
};
mVector operator+(const mVector &b)const{
return mVector(x+b.x,y+b.y);
};
mVector operator-(const mVector &b)const{
return mVector(x-b.x,y-b.y);
};
mVector operator-()const{
return mVector(-x,-y);
};
mVector operator\*(double n)const{
return mVector(n\*x,n\*y);
};
void show(ofstream &os){
//ofstream os;
if(mode==mVector::RECT)//必须用Vector::
os<<”(x,y)=(“<<x<<”,”<<y<<”)”;
else if(mode==mVector::POL)
os<<”(m,a)=(“<<mag<<”,”<<ang<<”)”;
};//add it
friend mVector operator\*(double n,const mVector &b){
return b\*n;
};
//friend ostream & operator<<(ostream &os,const mVector &v);
friend ofstream & operator<<(ofstream &fs,const mVector &v){//写到文件
if(v.mode==mVector::RECT)//必须用Vector::
fs<<"(x,y)=("<<v.x<<","<<v.y<<")";
else if(v.mode==mVector::POL)
{
fs<<"(m,a)=("<<v.mag<<","<<v.ang\*Rad\_to\_deg<<")";
}
else
fs<<"mVector object mode is invalid!";
return fs;
}
};
#endif
m.cpp
#include
#include
#include
#include
#include
//#include
#include “mvector.h”
using namespace std;
int main(int argc,char **argv)
{
ofstream fin;
fin.open(“111.txt”);//打开文件
if(fin.fail())
cout<<”open fali!”<<endl;
//else
//cout<<”Sucess!”<<endl;
//fin<<”111”;
srand(time(0));
double direction;
mVector step;
mVector result(0.0,0.0);
unsigned long steps=0;
double target;//要走的距离
double dstep;//每一步的距离
cout<<”Enter target step length:(q to quit:)”;
while(cin>>target)
{
cout<<”Enter dstep length:”;
if(!(cin>>dstep))
break;
int i=0;//上面的少了这个
while(result.magval()<target)
{
fin<<”\n”<<i++<<” “;
result.show(fin);//2
direction=rand()%360;
step.reset(dstep,direction,mVector::mVector::POL);
result=result+step;
steps++;
}
cout<<endl<<”After “<<steps<<” steps ,the subject “
<<” has the following location:”<<endl;
fin<<”After “<<steps<<” steps ,the subject “
<<” has the following location:”<<endl;
fin<<result;
result.polar_mode();
//cout<<” or \n”<<result<<endl;
fin<<” or \n”;
fin<<result<<endl;
cout<<”Average outward distance per step=”
<<result.magval()/steps<<endl;
fin<<”Average outward distance per step=”
<<result.magval()/steps<<endl;
steps=0;
result.reset(0.0,0.0);
cout<<”Enter target distance (q to quit):”;
}
fin<<"Bye!\\n";
cin.clear();
while(cin.get()!='\\n')
continue;
//cin.get();
fin.close();
}
运行结果: 111.txt的内容:
0 (x,y)=(0,0)
1 (x,y)=(4.81299,1.35467)
2 (x,y)=(1.41287,5.02063)
3 (x,y)=(-3.42338,6.28974)
4 (x,y)=(-2.09017,11.1087)
5 (x,y)=(-4.09009,6.52611)
6 (x,y)=(0.221501,3.99428)
7 (x,y)=(4.72083,6.17511)
8 (x,y)=(9.25807,8.27594)
9 (x,y)=(5.92338,12.0015)
10 (x,y)=(6.21089,7.00978)
11 (x,y)=(3.65988,11.3101)
12 (x,y)=(-0.629139,13.8799)
13 (x,y)=(-4.3101,10.4961)
14 (x,y)=(-2.6812,15.2233)
15 (x,y)=(1.81814,17.4041)
16 (x,y)=(5.75671,14.3239)
17 (x,y)=(7.59331,9.67346)
18 (x,y)=(12.5651,10.2035)
19 (x,y)=(7.6596,9.23618)
20 (x,y)=(5.03286,13.4906)
21 (x,y)=(4.30536,18.4374)
22 (x,y)=(7.47196,22.3069)After 23 steps ,the subject has the following location:
(x,y)=(12.3501,23.4041) or
(m,a)=(26.4628,62.1799)
Average outward distance per step=1.15055
Bye!
截图: 感谢网友v_empire的指出。 谢谢!
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-22
于GDUT
——————————————————————————————————————————————————————————————————
下面的是错误的,仅做保留,请勿参考! ——————————————————————————————————————————————————— vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include
using namespace std;
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1,double n2,Mode form=RECT);
~Vector();
void reset(double n1,double n2,Mode form=RECT);
double xval()const{return x;};
double yval()const{return y;};
double magval()const {return mag;};
double angval()const {return ang;};
void polar_mode();
void rect_mode();
Vector operator+(const Vector &b)const;
Vector operator-(const Vector &b)const;
Vector operator-()const;
Vector operator*(double n)const;
friend Vector operator*(double n,const Vector &b);
//friend ostream & operator<<(ostream &os,const Vector &v);
friend ofstream & operator<<(ofstream &fs,const Vector &v);//写到文件
};
}
#endif
vector.h
/*我去啊,我不过是为了简单一点直接写在h文件里面,用不用这么多错误啊*/
#include
#include
#include “vector.h”
#include
using std::cout;
using std::sin;
using std::sqrt;
using std::cos;
using std::atan;
using std::atan2;
using namespace VECTOR;
namespace VECTOR{
const double Rad_to_deg=45.0/atan(1.0);
void Vector::set_mag(){
mag=sqrt(x*x+y*y);
};
void Vector::set_ang(){
if(x==0.0&&y==0.0)
ang=0.0;
else
ang=atan2(y,x);
};
void Vector::set_x(){
x=mag*cos(ang);
};
void Vector::set_y(){
y=mag*sin(ang);
};
Vector::Vector(){
x=y=mag=ang=0.0;
mode=RECT;
};
//
Vector::Vector(double n1,double n2,Mode form){
mode=form;
if(form==RECT)
{
x=n1;
y=n2;
set_mag();
set_ang();
}
else if(form==POL)
{
mag=n1;
ang=n2;
set_x();
set_y();
}
else
{
cout<<”Incorrect 3rd argument to Vector()–”;
cout<<”Vector set to 0\n”;
x=y=mag=0.0;
mode=RECT;
}
};
Vector::~Vector(){};
void Vector::reset(double n1,double n2,Mode form)
{
mode=form;
if(form==RECT)
{
x=n1;
y=n2;
set_mag();
set_ang();
}
else if(form==POL)
{
mag=n1;
ang=n2;
set_x();
set_y();
}
else
{
cout<<”Incorrect 3rd argument to Vector()–”;
cout<<”Vector set to 0\n”;
x=y=mag=0.0;
mode=RECT;
}
};
/*
double Vector::xval()const{return x;};
double Vector::yval()const{return y;};
double Vector::magval()const {return mag;};
double Vector::angval()const {return ang;};
*/
void Vector::polar_mode(){mode=POL;};
void Vector::rect_mode(){mode=RECT;};
Vector Vector::operator+(const Vector &b)const{
return Vector(x+b.x,y+b.y);
};
Vector Vector::operator-(const Vector &b)const {
return Vector(x-b.x,y-b.y);
};
Vector Vector::operator-()const{
return Vector(-x,-y);
}
Vector Vector::operator*(double n)const{
return Vector(n*x,n*y);
};
Vector operator*(double n,const Vector &b){
return b*n;
};
/*
std::ostream & operator<<(std::ostream &os,const Vector &v)
{
if(v.mode==Vector::RECT)//必须用Vector::
os<<”(x,y)=(“<<v.x<<”,”<<v.y<<”)”;
else if(v.mode==Vector::POL)
{
os<<”(m,a)=(“<<v.mag<<”,”<<v.ang*Rad_to_deg<<”)”;
}
else
os<<”Vector object mode is invalid!”;
return os;
}
*/
std::ofstream & operator<<(std::ofstream &fs,const Vector &v)
{
if(v.mode==Vector::RECT)//必须用Vector::
fs<<”(x,y)=(“<<v.x<<”,”<<v.y<<”)”;
else if(v.mode==Vector::POL)
{
fs<<”(m,a)=(“<<v.mag<<”,”<<v.ang*Rad_to_deg<<”)”;
}
else
fs<<”Vector object mode is invalid!”;
return fs;
}void show(ofstream &os)
{
//ofstream os;
if(mode==Vector::RECT)//必须用Vector::
os<<”(x,y)=(“<<x<<”,”<<y<<”)”;
else if(mode==Vector::POL)
{
os<<”(m,a)=(“<<mag<<”,”<<ang<<”)”;
}
};
}
main111.cpp
#include
#include
#include
#include
//#include
#include “vector.h”
using namespace std;
using namespace VECTOR;
void main111()
{
ofstream fin;
fin.open(“111.txt”);//打开文件
if(fin.fail())
cout<<”open fali!”<<endl;
//else
//cout<<”Sucess!”<<endl;
//fin<<”111”;
srand(time(0));
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps=0;
double target;//要走的距离
double dstep;//每一步的距离
cout<<”Enter step length:(q to quit:)”;
while(cin>>target)
{
cout<<”Enter step length:”;
if(!(cin>>dstep))
break;
while(result.magval()<target)
{
fin<<”\n”<<i++<<” “;
result.show(fin);//2
direction=rand()%360;
step.reset(dstep,direction,Vector::POL);
result=result+step;
steps++;
}
cout<<”After “<<steps<<” steps ,the subject “
<<” has the following location:”<<endl;
fin<<”After “<<steps<<” steps ,the subject “
<<” has the following location:”<<endl;
fin<<result;
result.polar_mode();
//cout<<” or \n”<<result<<endl;
fin<<” or \n”;
fin<<result<<endl;
cout<<”Average outward distance per step=”
<<result.magval()/steps<<endl;
fin<<”Average outward distance per step=”
<<result.magval()/steps<<endl;
steps=0;
result.reset(0.0,0.0);
cout<<”Enter target distance (q to quit):”;
}
fin<<”Bye!\n”;
cin.clear();
while(cin.get()!=’\n’)
continue;
//cin.get();
fin.close();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/cprimerplus第六版课后编程题答案111已修改/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!