//vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
private:
double x; //horizental move
double y; //vertical move
double mag; //length of vector
double ang; //direction of vector in degrees
Mode mode; //RECT or POL
//private methods for setting values
void set_mag(); //留出的 change Vector private 變量的接口
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1,double n2, Mode form = RECT);
void reset(double n1,double n2,Mode form = RECT);
~Vector();
double xval() const { return x; }
double yval() const {return y ; }
double mgval() const { return mag; }
double angval() const { return ang; }
void polar_mode();
void rect_mode();
//operator overloading
Vector operator+(const Vector & b)const;
Vector operator - (const Vector & b)const;
Vector operator -()const;
Vector operator * (double n)const;
//friends
friend Vector operator*(double n, const Vector & a);
friend std::ostream &
operator<<(std::ostream & os, const Vector &v);
protected:
};
}//end namespace VECTOR
#endif // !VECTOR_H
_
//vector.cpp
#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_2_Deg = 45.0 / atan(1.00);
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;
mode = RECT;
}
Vector::~Vector()
{
}
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 argyment to Vector()---";
cout << "vector set to 0\n";
x = y = mag = ang = 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 argyment to Vector()---";
cout << "vector set to 0\n";
x = y = mag = ang = 0;
mode = RECT;
}
}
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode = RECT;
}
//operator overloading
Vector Vector::operator+(const Vector & b)const
{
Vector temp((this->x) + b.x, (this->y) + b.y);
//Vector temp; //如果使用默認構造函數,那麼造成的影響就是沒有進行reset,所以mag,ang 一直是0
//temp.x = (this->x) + b.x;
//temp.y = (this->y) + b.y;
return temp;
}
Vector Vector::operator - (const Vector & b)const
{
Vector temp((this->x) - b.x, (this->y) - b.y);
/*Vector temp;
temp.x = (this->x) - b.x;
temp.y = (this->y) - b.y;*/
return temp;
}
Vector Vector::operator -()const
{
Vector temp(-(this->x), -(this->y));
//Vector temp;
//temp.x = -(this->x);
//temp.y = -(this->y);
return temp;
}
Vector Vector::operator * (double n)const
{
Vector temp((this->x) * n, (this->y) * n);
/*
Vector temp;
temp.x = (this->x) * n;
temp.y = (this->y) * n;
*/
//如果是調用默認構造函數,不能起到構造的目的,所以應該使用帶參數列表的構造函數
return temp;
}
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_2_Deg << ")";
}
else
{
os << "Vector object mode is invalid !";
}
return os;
}
}//end namespace VECTOR
//main函數
//randwork.cpp
#include<iostream>
#include <fstream>
#include <cstdlib>
#include<cstdlib>
#include <ctime>
#include "vector.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time (0));
double direction;
Vector step;
Vector result(0.0 , 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;
fout.open("thisworkDairy.txt");
if (!fout.is_open())
{
cerr << "can't open this outwork file" << endl;
exit(EXIT_FAILURE);
}
cout << "Enter target distance (q to quit) :";
while (cin>> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout << "Target distance: " << target
<<", Step Size: "<<dstep<<endl;
fout << steps << ": " << result << endl;
while (result.mgval()<target)
{
direction = rand() % 360;
step.reset(dstep,direction,Vector::POL);
result = result + step;
steps += 1;
fout << steps << ": " << result << endl;
}
cout << "After " << steps << "steps, the subject "
<< "has the following location: \n";
cout << result << endl;
fout<< "After " << steps << "steps, the subject "
<< "has the following location: \n";
fout<< result << endl;
result.polar_mode();
cout << "or\n " << result << endl;
cout << "Average outward distance per step = "
<< result.mgval() / steps << endl;
fout << "or\n" << result << endl;
fout << "Average outward distance per step = "
<< result.mgval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit) ";
}
cout << "Bye !\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
總計:主要是更改了一下之前的原有調用使用的構造函數,沒有看仔細,調用了無參的構造函數,而不是我們希望的有參構造函數。最後單步調試纔看出了問題所在,長記性了。