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();
void set_y();
void set_ang();
void set_mag();
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 mag; }; //返回行走距离
double angval()const { return ang; }; //返回角度
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_mag()
{
mag = sqrt(x*x + y*y);
}
void Move::set_ang()
{
if (x == 0 && y == 0)
ang = 0;
else
ang = atan2(y, x);
}
void Move::set_x()
{
x = mag*cos(ang);
}
void Move::set_y()
{
y = mag*sin(ang);
}
Move::Move()
{
x = y = mag = ang = 0;
mode = RECH;
}
Move::Move(double a, double b, move from)
{
mode = from;
if (from == RECH)
{
x = a;
y = b;
set_mag();
set_ang();
}
else if(from==POL)
{
mag = a;
ang = b/Rad_to_deg;
set_x();
set_y();
}
else {
cout << "Incorrect 3rd arguament to Move() --"
<< "Move set to 0\n";
x = y = ang = mag = 0;
mode = RECH;
}
}
void Move::reset(double a, double b, move from)
{
mode = from;
if (from == RECH)
{
x = a;
y = b;
set_mag();
set_ang();
}
else if (from == POL)
{
mag = a;
ang = b;
set_x();
set_y();
}
else {
cout << "Incorrect 3rd arguament to Move() --"
<< "Move set to 0\n";
x = y = ang = mag = 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.mag << ", " << a.ang *Rad_to_deg<< ")";
}
else
os << "Move object mode is invalid.";
return os;
}
}
main.cpp
#include "list.h"
#include <cstdlib>
#include <ctime>
#include <fstream>
int main()
{
using namespace std;
using VECTOR::Move;
srand(time(0));
double direction;
Move step;
Move result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;
fout.open("Walkman.txt");
if (!fout)
cout << "Open file failed!";
cout << "Enter target distance(q to quite): ";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout << "Target Distance : " << target << ", Step Size :" << dstep << endl;
while (result.magval() < target)
{
fout << steps << ": \t" << result<<endl;
direction = rand() % 360;
step.reset(dstep, direction, Move::POL);
result = result + step;
steps++;
}
fout<<steps<<": \t" << result << endl;
fout << "After " << steps << " steps ,the subject "
"has the following location:\n";
fout << result << endl;
result.polar_mode();
fout << "or\n" << result << endl;
fout << "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;
}