//vcet.cpp--methods for vector class
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
#include"vect.h"
namespace vector
{
const double red_to_deg = 57.295774579893;
//private methods
//calculates magnitude form x and y
void vector:: set_mag()
{
mag = (x * x + y * y);
}
void vector:: set_ang()
{
if(x == 0 && y == 0)
ang = 0.0;
else atan2(y,x);
}
//set x form polar coordinate
void vector:: set_x()
{
x = mag * cos (ang);
}
//set y form polar coordinate
void vector:: set_y(ang);
{
y = mag * sin (ang);
}
//public methods
vector:: vector () //default constructor
{ x = y = mag = ang = 0;
mode = " r";
}
//constructor vector form rec coordinate if form is rec
//or else form pol coordinate if form is p
vector:: vector(double n1,double n2,char form)
{
made = form;
if (form == 'r')
{
x1=n1;
x2=n2;
set_mag;
set_ang;
}
else if (form =='p')
{
mag =n1;
ang =n2/rad_to_deg;
set_x();
srt_y();
}
else
{
cout<<"Incorrect 3rd argument to vector ()--";
cout<<"vector set to 0\n";
x = y = mag = ang =0.0;
mode = "r";
}
}
//set voctor form rec coordinate if form is r or else form polcoordinate
//if form is p
void vector:: set (double n1,double n2,char form)
{
mode = form;
if (form = 'r')
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 'p')
{
mag = n1;
ang = n2/rad_to_deg;
set_x();
set_y();
}
else
{
cout<<"Incorrect 3rd argument to vector ()--";
cout<<"vector set to 0\n";
x = y = mag =ang = 0.0;
mode = 'r';
}
}
vector:: ~vector() //default
{
}
void vector:: polar_mode() //set to polar mode
{
mode = 'p';
}
void vector:: rect_mode() //set to rectangular mode
{
mode = 'r';
}
//operate overloading
//add two vectors
vector vector:: operator+ (const vector & b)const
{
return vector (x - b.x,y - b.y);
}
//reverse sign of vector
vector vector:: operator-()const
{
return vector (-x,-y);
}
//mutiple vector by n
vector vrctor:: operator*()const
{
return vector (n*x,n*y);
}
//firend methods
//multiply n by vector add
vector operator * (double n,const vector & a)
{
return a * n;
}
//display rec coordinate if mode is r
//else display polar coordinade if mode is p
ostream & operator <<(ostream & os,const vector & v)
{
if (v.mode == 'r')
os<<" (x,y)=("<<v.x<<","<<v.y<<")";
else if (v.mode == 'p')
{
os <<"(m,a) = (" <<v.mg<<","
<<v.ang * rad_to_deh <<")";
}
else
os << "vector object mode is invalid ";
return os;
}
} //end namespace vector
int main()
{
using vector:: vector;
srand(time (0) );
double direction;
vector step;
vector result (0.0,0.0);
unsigned long steps = 0;
double target;
double dstep;
cout<<"Enter target distance (q to quit):";
while(cin>>target)
{
cout<<"Enter step longth:";
if (!(cin>>target))
break;
while (result .magval()<target)
{
directiom = rand ()%360;
step.set(dstep,direction,'p';
result = result + step;
steps++;
}
cout<<"after"<<steps<<"steps,the subject;
"has the following location:\n";
cout<<result<<:\n";
result.polar_mode();
cout<<"or\n"<<result<<"\n";
cout<<"Average outward distance per step ="
<<result.magval ()/steps<<"\n";
steps =0;
result.set(0.0,0.0);
cout<<"Enter target distance (q to quit):";
}
cout<<"Bye:";
return 0;
}