//vector.h
#ifndef VECTOR_h_
#define VECTOR_h_
#include <iostream>
namespace VECTOR
{
class Vector{
public:
enum Mode{RECT,POL};//RECT表示x,y
private:
double x;//x坐标
double y;//y坐标
double mag;//长度
double ang;//弧度
Mode mode;//模式,根据模式来决定坐标输出时的形式
void set_mag();
void set_ang();//设置极坐标(根据x,y坐标)
void set_x();
void set_y();//设置x、y坐标(根据极坐标)
public:
Vector();
Vector(double n1,double n2,Mode form=RECT);//默认构造函数,默认为x、y坐标赋值
void reset(double n1,double n2,Mode form=RECT);//重置坐标
~Vector();
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 & a);
friend std::ostream & operator<<(std::ostream &os,const Vector & v);//用于cout或其他输出
};
}
#endif
//vector.cpp
//Vector要实现矢量的基本运算
#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
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);//临边(x) = cos弧度*斜边
}
void Vector::set_y(){
y=mag*sin(ang);//对边(y) = sin弧度*斜边
}
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=ang=0.0;
mode=RECT;
}
}
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=ang=0.0;
mode=RECT;
}
}
Vector::~Vector(){
}
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 & a){
return a*n;
}
std::ostream & operator<<(std::ostream &os,const Vector & v){
if (v.mode==Vector::RECT)
{
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;
}
}
//randwalk.cpp
//随机漫步问题:将一个人领到街灯柱下,这个人开始走动,但每一步方向都是随机的(与前一步不同)。求这个人走到离灯柱50英尺需要多少步。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vector.h"
int main(){
using namespace std;
using VECTOR::Vector;
ofstream fout;//声明一个输出的对象
fout.open("walk.txt");//打开文件
srand(time(0));//设置随机数种子
double direction;//方向
Vector step;//新的一步
Vector result(0.0,0.0);//当前位置
unsigned long steps=0;//步数
double target;//目标距离
double dstep;//每一步的距离
cout<<"输入目标距离 (q退出):";
while(cin>>target){
cout<<"输入步长:";
if (!(cin>>dstep)){
break;}
fout<< "目标距离: " << target <<", 步长: " << dstep<<endl;
fout << 0 << ": " << result << endl;
while(result.magval()<target){
//离起点的距离还没达到目标距离,就要继续执行
direction=rand()%360;//随机一个方向,角度
step.reset(dstep,direction,Vector::POL);//当前的一步
result=result+step;//当前的实际位置
steps++;//计数
fout<< steps << ": " << result << endl; //记录每一步,存到文件中
}
cout<<steps<<"步后,对象到达以下位置:\n";
cout<<result<<endl;
fout<<steps<<"步后,对象到达以下位置:\n";
fout<<result<<endl;//
result.polar_mode();
cout<<"or\n"<<result<<endl;
fout<<"or\n"<<result<<endl;//
cout<<"每步平均向外移动="<<result.magval()/steps<<endl;
fout<<"每步平均向外移动="<<result.magval()/steps<<endl;
steps=0;
result.reset(0.0,0.0);
cout<<"输入目标距离 (q退出):";
}
cout<<"Bye!!\n";
cin.clear();
while(cin.get()!='\n')
continue;
fout.close();
return 0;
}