list.h
#pragma once
#ifndef list_H_
#define list_H_
#include <iostream>
namespace VECTOR {
class Move {
public:
enum move { RECH, POL };
private:
double x; //分量
double y; //分量
//double mag; //行走的距离
//double ang; //角度
move mode; //RECH or POL
void set_x(double a, double b);
void set_y(double a, double b);
public:
Move();
Move(double a, double b, move from = RECH); //构造函数,默认直角坐标显示
void reset(double a, double b, move from = RECH); //设置新的坐标值,默认直角坐标显示
double xval()const { return x; }; //返回分量1的值
double yval()const { return y; }; //返回分量2的值
double magval() const { return sqrt(x*x + y*y); }; //返回行走距离
double angval() const { return atan2(y, x); }; //返回角度
void polar_mode(); //设置为直角坐标表示
void rect_mode(); //设置为极坐标表示
//重载运算符
Move operator +(const Move & a)const;
Move operator -(const Move & a)const;
Move operator -()const;
Move operator *(double n)const;
friend Move operator *(double n, const Move & a);
friend std::ostream & operator <<(std::ostream & os, const Move & a);
};
}
#endif
function.cpp
#include <iostream>
#include "list.h"
#include <cmath>
using namespace std;
namespace VECTOR {
const double Rad_to_deg = 45.0 / atan(1.0);
void Move::set_x(double a, double b)
{
x = a*cos(b / Rad_to_deg);
}
void Move::set_y(double a, double b)
{
y = a*sin(b / Rad_to_deg);
}
Move::Move()
{
x = y = 0;
mode = RECH;
}
Move::Move(double a, double b, move from)
{
mode = from;
if (from == RECH)
{
x = a;
y = b;
}
else if(from==POL)
{
set_x(a,b);
set_y(a,b);
}
else {
cout << "Incorrect 3rd arguament to Move() --"
<< "Move set to 0\n";
x = y = 0;
mode = RECH;
}
}
void Move::reset(double a, double b, move from)
{
mode = from;
if (from == RECH)
{
x = a;
y = b;
}
else if (from == POL)
{
set_x(a,b);
set_y(a,b);
}
else {
cout << "Incorrect 3rd arguament to Move() --"
<< "Move set to 0\n";
x = y = 0;
mode = RECH;
}
}
void Move::polar_mode()
{
mode = POL;
}
void Move::rect_mode()
{
mode = RECH;
}
Move Move::operator +(const Move & a)const
{
return Move(x + a.x, y + a.y);
}
Move Move::operator -(const Move & a)const
{
return Move(x - a.x, y - a.y);
}
Move Move::operator -()const
{
return Move(-x, -y);
}
Move Move:: operator *(double n)const
{
return Move(x*n, y*n);
}
Move VECTOR::operator*(double n, const Move & a)
{
return a*n;
}
std::ostream & operator<<(std::ostream & os, const Move & a)
{
if (a.mode == Move::RECH)
{
os << "(x,y) = (" << a.x << ", " << a.y << ")";
}
else if (a.mode == Move::POL)
{
os << "(m,a) = (" << a.magval() << ", " << a.angval() *Rad_to_deg<< ")";
}
else
os << "Move object mode is invalid.";
return os;
}
}
main.cpp
#include "list.h"
#include <cstdlib>
#include <ctime>
int main()
{
using namespace std;
using VECTOR::Move;
srand(time(0));
double direction=0;
Move step;
Move result(0.0, 0.0);
unsigned long steps = 0.0;
double target;
double dstep=0.0;
cout << "Enter target distance(q to quite): ";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
cout << "Target Distance : " << target << ", Step Size :" << dstep << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Move::POL);
result = result + step;
steps++;
}
cout << "After " << steps << " steps ,the subject "
"has the following location:\n";
cout << result << endl;
result.polar_mode();
cout << "or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval() / 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;
system("pause");
return 0;
}