vector.h

  1. #ifndef PRO1_VECTOR_H
  2. #define PRO1_VECTOR_H
  3. #include <iostream>
  4. using namespace std;
  5. namespace VECTOR{
  6. class Vector {
  7. private:
  8. double x;
  9. double y;
  10. double mag;
  11. double ang;
  12. char mode;
  13. // private methods for setting values
  14. void set_mag();
  15. void set_ang();
  16. void set_x();
  17. void set_y();
  18. public:
  19. Vector();
  20. Vector(double n1, double n2, char form = 'r');
  21. ~Vector();
  22. void set(double n1, double n2, char form = 'r');
  23. double xval()const { return x;}
  24. double yval()const { return y;}
  25. double magval()const { return mag;}
  26. double angval()const { return ang;}
  27. void polar_mode();
  28. void rect_mode();
  29. // operator overloading
  30. Vector operator+(const Vector & b)const ;
  31. Vector operator-(const Vector & b)const ;
  32. Vector operator-()const ;
  33. Vector operator*(double n)const ;
  34. // friends
  35. friend Vector operator*(double n, const Vector & a);
  36. friend ostream &operator << (ostream & os, const Vector & vector);
  37. };
  38. }
  39. #endif //PRO1_VECTOR_H

vector.cpp

  1. #include "vector.h"
  2. #include <cmath>
  3. using std::sin;
  4. using std::cos;
  5. using std::atan2;
  6. using std::cout;
  7. using std::ostream;
  8. namespace VECTOR{
  9. const double Rad_to_deg = 57.2957795130823;
  10. // private methods
  11. // calculates magnitude from x and y
  12. void Vector::set_mag() {mag - sqrt(x * x + y * y);}
  13. void Vector::set_ang() {
  14. if(x == 0.0 && y == 0.0){
  15. ang = 0.0;
  16. } else {
  17. ang = atan2(y, x);
  18. }
  19. }
  20. void Vector::set_x() {x = mag * cos(ang);}
  21. void Vector::set_y() {y = mag * sin(ang);}
  22. Vector::Vector() {
  23. x = y = mag = ang = 0.0;
  24. mode = 'r';
  25. }
  26. Vector::Vector(double n1, double n2, char form) {
  27. mode = form;
  28. if (form == 'r') {
  29. x = n1;
  30. y = n2;
  31. set_mag();
  32. set_ang();
  33. } else if (form == 'p'){
  34. mag = n1;
  35. ang = n2 / Rad_to_deg;
  36. set_x();
  37. set_y();
  38. } else{
  39. cout << "Incorrect 3rd argument to Vector()-- ";
  40. cout << "vector set to 0\n";
  41. x = y = mag = ang = 0.0;
  42. mode = 'r';
  43. }
  44. }
  45. void Vector::set(double n1, double n2, char form) {
  46. mode = form;
  47. if (form == 'r') {
  48. x = n1;
  49. y = n2;
  50. set_mag();
  51. set_ang();
  52. } else if (form == 'p'){
  53. mag = n1;
  54. ang = n2 / Rad_to_deg;
  55. set_x();
  56. set_y();
  57. } else{
  58. cout << "Incorrect 3rd argument to Vector()-- ";
  59. cout << "vector set to 0\n";
  60. x = y = mag = ang = 0.0;
  61. mode = 'r';
  62. }
  63. }
  64. Vector::~Vector() {}
  65. void Vector::polar_mode() {mode = 'p';}
  66. void Vector::rect_mode() {mode = 'r';}
  67. Vector Vector::operator+(const VECTOR::Vector &b) const { return Vector(x + b.x, y + b.y); }
  68. Vector Vector::operator-(const VECTOR::Vector &b) const { return Vector(x - b.x, y - b.y); }
  69. Vector Vector::operator-() const { return Vector(-x, -y); }
  70. Vector Vector::operator*(double n) const { return Vector(n * x, n * y); }
  71. Vector operator *(double n, const Vector & a){ return a * n; }
  72. ostream & operator<< (ostream & os, const Vector & vector){
  73. if (vector.mode == 'r') {
  74. os << "(x,y) = " << vector.x << ", " << vector.y ;
  75. } else if (vector.mode == 'p') {
  76. os << "(m, a) = " << vector.mag << ", " << vector.ang * Rad_to_deg ;
  77. } else
  78. os << "Vector object mode is invalid";
  79. return os;
  80. }
  81. }

使用Vector类模拟随机行走

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include "module4/vector.h"
  4. using namespace std;
  5. // 使用Vector类模拟随机行走
  6. void randwalk(){
  7. using VECTOR::Vector;
  8. srand(time(0));
  9. double direction;
  10. Vector step;
  11. Vector result(0.0, 0.0);
  12. unsigned long steps = 0;
  13. double dstep;
  14. double target;
  15. cout << "Enter target distance(q to quit): ";
  16. while (cin >> target) {
  17. cout << "Enter step length: ";
  18. if (!(cin >> dstep)) {
  19. break;
  20. }
  21. while (result.magval() < target){
  22. direction = rand()%360;
  23. step.set(dstep, direction, 'p');
  24. result = result + step;
  25. steps++;
  26. }
  27. cout << "After " << steps << " steps, the subject has the following location: \n";
  28. cout << result << endl;
  29. result.polar_mode();
  30. cout << " or\n" << result << endl;
  31. cout << "Average outward distance per step = " << result.magval()/steps << endl;
  32. steps = 0;
  33. result.set(0.0, 0.0);
  34. cout << "Enter target distance (q to quit): ";
  35. }
  36. cout << "Bye!\n";
  37. }
  38. int main() {
  39. randwalk();
  40. return 0;
  41. }