#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
#ifndef VECTOR_H_
#define VECTOR_H_
namespace VECTOR{
class vector
{
private:
double x;
double y;
double mag;
double ang;
char mode;
//private methods for setting values
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
vector();
vector(double n1,double n2,char form='r');
void set(double n1,double n2,char form='r');
~vector();
double xval() const{return x;}
double yval() const{return y;}
double magval() const{return mag;}
double angval() const{return val;}
void polar_mode();
void rect_mode();
//operator overloading
vector operator +(const vertor &b)const;
vector operator -(const vertor &b)const;
vector operator -()const;
vector operator *(double n)const;
//friends
friend vector operator *(double n,const vertor & a);
friend ostream & operator <<(ostream & os ,const vector &v);
};
}
#endif
namespace VECTOR
{
const double Rad_to_deg=57.2957795130823;
//private methods
//calulates magnitude from x and y
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);
}
//public methods
vector::vector()
{
x=y=mag=ang=0.0;
mode='r';
}
vector::vector(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"<<endl;
x=y=mag=ang=0.0;
mode='r';
}
}
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();
}
vector::~vector()
{
}
void vector::rect_mode()
{
return mode='r';
}
void vector::polar_mode()
{
return mode='p';
}
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::operator *(double n,const vector & a)
{
return a*n;
}
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.mag<<","<<v,ang*Rad_to_deg<<")";}
else
os<<"vector object mode is invalid";
return os;
}
}
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>>dstep){
cout<<"Enter step length:";
if(!(cin>>dstep))
break;
while(result.magval()<target)
{
direction=rand()%360;
step.set(dstep,direction,'p');
result=result+step;
steps++;
}
cout<<"After"<<steps<<"steps,the subject ""has the following location:\n";
cout<<result<<endl;
result.polar_mode();
cout<<"or"<<endl<<result<<endl;
cout<<"Average outward distance per step="
<<result.magval()/steps<<endl;
steps=0;
result.set(0.0,0.0);
cout<<"Enter target distance(q to quit):";
}
cout<<"bye"<<endl;
return 0;
}
错误::::
--------------------Configuration: asd - Win32 Debug--------------------
Compiling...
asd.cpp
C:\Documents and Settings\计算机中心\桌面\asd.cpp(33) : error C2146: syntax error : missing ',' before identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(33) : error C2061: syntax error : identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(34) : error C2146: syntax error : missing ',' before identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(34) : error C2061: syntax error : identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(38) : error C2143: syntax error : missing ',' before '&'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(38) : error C2059: syntax error : '&'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(38) : error C2803: 'operator *' must have at least one formal parameter of class type
C:\Documents and Settings\计算机中心\桌面\asd.cpp(52) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\计算机中心\桌面\asd.cpp(107) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(110) : error C2601: 'rect_mode' : local function definitions are illegal
C:\Documents and Settings\计算机中心\桌面\asd.cpp(114) : error C2601: 'polar_mode' : local function definitions are illegal
C:\Documents and Settings\计算机中心\桌面\asd.cpp(118) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
asd.obj - 12 error(s), 0 warning(s)