欢迎来到天天文库
浏览记录
ID:37974379
大小:40.72 KB
页数:7页
时间:2019-06-04
《C++模拟醉汉游动问题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C++模拟醉汉游动问题问题如下:有一个醉汉,把他领到街灯柱子下面,这个人开始游动,但醉汉每一步的方向都是随机的(与前一步不同)。问,这个人走到离开灯柱50英尺远的地方需要多少步(假定醉汉每一步都是2英尺)。OOP更是一种编程思想,声明与实现分离。这里定义一个向量类来解决,共包含三个文件,vector.h,vector.cpp,main.cpp1.先定义一个Vector类//vector.h---Vectorclasswith<<,modestate#ifndefVECTOR_H_#defineVECTOR_H_#include2、stream>namespaceVECTOR{classVector{public:enumMode{RECT,POL};private://RECTforrectangular,POLforpolarmodes//enumMode{RECT,POL};doublex;doubley;doublemag;doubleang;Modemode;voidset_mag();voidset_ang();voidset_x();voidset_y();public:Vector();Vector(doublen1,doublen2,Mode3、form=RECT);voidreset(doublen1,doublen2,Modeform=RECT);~Vector();doublexval()const{returnx;}doubleyval()const{7returny;}doublemagval()const{returnmag;}doubleangval()const{returnang;}voidset_polar_mode();voidset_rect_mode();//operatoroverloadingVectoroperator+(constVecto4、r&b)const;Vectoroperator-(constVector&b)const;Vectoroperator-()const;Vectoroperator*(doublen)const;//friendfriendVectoroperator*(doublen,constVector&b);//keyfunctiontomakefacefriendstd::ostream&operator<<(std::ostream&os,Vector&v);};}#endif2.给出这个类以及其他函数的定义//vector.cpp-5、--methodsfortheVectorclass#include#include#include"vector.h"usingstd::sqrt;usingstd::sin;usingstd::cos;usingstd::atan2;//atanusingstd::cout;namespaceVECTOR{//radiustodegree180/piconstdoublepi=3.14159;constdoublerad_2_def=180/pi;voidVector::set_mag(){ma6、g=sqrt(x*x+y*y);7}voidVector::set_ang(){ang=atan2(y,x);}voidVector::set_x(){x=mag*cos(ang);}voidVector::set_y(){y=sin(ang);}Vector::Vector(){x=y=0,0;//set_mag();//set_ang();mag=ang=0.0;mode=RECT;}Vector::Vector(doublen1,doublen2,Modeform){if(form==RECT){x=n1;y=n2;set_m7、ag();set_ang();}else{//form==POLmag=n1;ang=n2/rad_2_def;//converttoradiusset_x();set_y();}//setmodeaccordingtoformmode=form;}voidVector::reset(doublen1,doublen2,Modeform){if(form==RECT){x=n1;y=n2;set_mag();7set_ang();}else{mag=n1;ang=n2/rad_2_def;//converttoradiusset_x8、();set_y();}//setmodeaccordingtoformmode=form;}Vector::~Vector(){}voidVector::set_rect_mode(){mode=RECT;}voidVector::
2、stream>namespaceVECTOR{classVector{public:enumMode{RECT,POL};private://RECTforrectangular,POLforpolarmodes//enumMode{RECT,POL};doublex;doubley;doublemag;doubleang;Modemode;voidset_mag();voidset_ang();voidset_x();voidset_y();public:Vector();Vector(doublen1,doublen2,Mode
3、form=RECT);voidreset(doublen1,doublen2,Modeform=RECT);~Vector();doublexval()const{returnx;}doubleyval()const{7returny;}doublemagval()const{returnmag;}doubleangval()const{returnang;}voidset_polar_mode();voidset_rect_mode();//operatoroverloadingVectoroperator+(constVecto
4、r&b)const;Vectoroperator-(constVector&b)const;Vectoroperator-()const;Vectoroperator*(doublen)const;//friendfriendVectoroperator*(doublen,constVector&b);//keyfunctiontomakefacefriendstd::ostream&operator<<(std::ostream&os,Vector&v);};}#endif2.给出这个类以及其他函数的定义//vector.cpp-
5、--methodsfortheVectorclass#include#include#include"vector.h"usingstd::sqrt;usingstd::sin;usingstd::cos;usingstd::atan2;//atanusingstd::cout;namespaceVECTOR{//radiustodegree180/piconstdoublepi=3.14159;constdoublerad_2_def=180/pi;voidVector::set_mag(){ma
6、g=sqrt(x*x+y*y);7}voidVector::set_ang(){ang=atan2(y,x);}voidVector::set_x(){x=mag*cos(ang);}voidVector::set_y(){y=sin(ang);}Vector::Vector(){x=y=0,0;//set_mag();//set_ang();mag=ang=0.0;mode=RECT;}Vector::Vector(doublen1,doublen2,Modeform){if(form==RECT){x=n1;y=n2;set_m
7、ag();set_ang();}else{//form==POLmag=n1;ang=n2/rad_2_def;//converttoradiusset_x();set_y();}//setmodeaccordingtoformmode=form;}voidVector::reset(doublen1,doublen2,Modeform){if(form==RECT){x=n1;y=n2;set_mag();7set_ang();}else{mag=n1;ang=n2/rad_2_def;//converttoradiusset_x
8、();set_y();}//setmodeaccordingtoformmode=form;}Vector::~Vector(){}voidVector::set_rect_mode(){mode=RECT;}voidVector::
此文档下载收益归作者所有