点坐标转换极坐标
atrctfun.cpp
1 // 程序清单 7.12 atrctfun.cpp 2 // 坐标转换极坐标 3 #include <IOSTREAM> 4 #define _USE_MATH_DEFINES 5 #include <MATH.H> 6 7 struct rect // 点坐标结构 8 { 9 double x; 10 double y; 11 }; 12 13 struct polar // 极坐标结构 14 { 15 double distance; // 长度 16 double angle; // 角度 17 }; 18 19 polar rect_to_polar(rect rcpos); // 点坐标转换极坐标 20 void show_Polar(polar dapos); // 显示极坐标 21 22 int main(int argc, char * argv[]) 23 { 24 using namespace std; 25 rect rplace; 26 polar pplace; 27 cout << "Enter the x and y value: "; 28 while (cin >> rplace.x >> rplace.y) // 当输入非数值型数据cin返回false,退出while循环 29 { 30 pplace = rect_to_polar(rplace); 31 show_Polar(pplace); 32 cout << "Next two numbers(q to quit):"; 33 } 34 cout << "Done.\n"; 35 return 0; 36 } 37 38 polar rect_to_polar(rect rcpos) 39 { 40 polar pret; 41 pret.distance = sqrt(rcpos.x * rcpos.x + rcpos.y * rcpos.y); 42 pret.angle = atan2(rcpos.y, rcpos.x); 43 return pret; 44 } 45 46 void show_Polar(polar dapos) 47 { 48 using std::cout; 49 using std::endl; 50 double rad_to_deg = 180/M_PI; // 弧度转角度(弧度 * 180 / π) 角度转弧度(角度 * π / 180) 51 cout << "distance = " << dapos.distance << endl; 52 cout << "angle = " << dapos.angle * rad_to_deg << endl; 53 }
实现坐标类Vector
vector.h
1 /* 2 * 程序清单 11.13 vector.h 3 */ 4 #ifndef VECTOR_H_ 5 #define VECTOR_H_ 6 #include <IOSTREAM> 7 namespace VECTOR 8 { 9 class Vector 10 { 11 private: 12 double x; // 点坐标x 13 double y; // 点坐标y 14 double mag; // 矢量长度 15 double ang; // 矢量角度(弧度) 16 char mode; // mode = 'r'点坐标,mode = 'p'极坐标 17 18 void set_mag(); // 更新矢量长度 19 void set_ang(); // 更新矢量角度 20 void set_x(); // 更新点坐标x 21 void set_y(); // 更新点坐标y 22 23 public: 24 Vector(); // 默认构造函数 25 Vector(double n1, double n2, char from = 'r'); // 带参数构造函数 26 void set(double n1, double n2, char from = 'r'); // 更新Vector 27 ~Vector(); 28 double xval()const {return x;} // 输出x 29 double yval()const {return y;} // 输出y 30 double magval()const {return mag;} // 输入矢量长度 31 double angval()const {return ang;} // 输出矢量角度(弧度) 32 33 void rect_mode() {mode = 'r';} 34 void polar_mode() {mode = 'p';} 35 // 重载操作符 36 Vector operator + (const Vector & b) const; 37 Vector operator - (const Vector & b) const; 38 Vector operator - (); 39 Vector operator * (const double n) const; 40 // 友元函数 41 friend Vector operator* (const double n, const Vector &a); 42 friend std::ostream & operator<< (std::ostream & os, const Vector &v); 43 }; 44 } 45 #endif
vector.cpp
1 #define _USE_MATH_DEFINES 2 #include <CMATH> 3 #include "vector.h" 4 5 using std::cout; 6 7 namespace VECTOR 8 { 9 const double rad_to_deg = 180/M_PI; 10 Vector::Vector() 11 { 12 x = y = mag = ang = 0.0; 13 mode = 'r'; 14 } 15 16 Vector::Vector(double n1, double n2, char from /* = 'r' */) 17 { 18 mode = from; 19 if (mode == 'r') 20 { 21 x = n1; 22 y = n2; 23 set_mag(); 24 set_ang(); 25 } 26 else if (mode == 'p') 27 { 28 mag = n1; 29 ang = n2; 30 set_x(); 31 set_y(); 32 } 33 else 34 { 35 cout << "Incorrect 3rd argument to Vector()--"; 36 cout << "vector object set to 0.\n"; 37 x = y = mag = ang = 0.0; 38 mode = 'r'; 39 } 40 } 41 42 void Vector::set(double n1, double n2, char from /* = 'r' */) 43 { 44 mode = from; 45 if (mode == 'r') 46 { 47 x = n1; 48 y = n2; 49 set_mag(); 50 set_ang(); 51 } 52 else if (mode == 'p') 53 { 54 mag = n1; 55 ang = n2/rad_to_deg; 56 set_x(); 57 set_y(); 58 } 59 else 60 { 61 cout << "Incorrect 3rd argument to Vector()--"; 62 cout << "vector object set to 0.\n"; 63 x = y = mag = ang = 0.0; 64 mode = 'r'; 65 } 66 } 67 68 Vector::~Vector() 69 { 70 71 } 72 73 void Vector::set_x() 74 { 75 x = mag * cos(ang); 76 } 77 78 void Vector::set_y() 79 { 80 y = mag * sin(ang); 81 } 82 83 void Vector::set_mag() 84 { 85 mag = sqrt(x * x + y * y); 86 } 87 88 void Vector::set_ang() 89 { 90 if (x == 0.0 && y == 0.0) 91 { 92 ang = 0.0; 93 } 94 else 95 { 96 ang = atan2(y, x); 97 } 98 } 99 100 // 重载操作符 101 Vector Vector::operator +(const Vector & b) const 102 { 103 return Vector(x + b.x, y + b.y); 104 } 105 106 Vector Vector::operator -(const Vector & b) const 107 { 108 return Vector(x - b.x, y - b.y); 109 } 110 111 Vector Vector::operator -() 112 { 113 return Vector(-x, -y); 114 } 115 116 Vector Vector::operator *(const double n) const 117 { 118 return Vector(x * n, y * n); 119 } 120 121 // 友元 122 Vector operator * (const double n, const Vector &a) 123 { 124 return a * n; 125 } 126 127 std::ostream & operator << (std::ostream & os, const Vector & v) 128 { 129 if (v.mode == 'r') 130 { 131 os << "(x, y) = (" << v.x << ", " << v.y << ")"; 132 } 133 else if (v.mode == 'p') 134 { 135 os << "(m, a) = (" << v.mag << ", " << v.ang * rad_to_deg << ")"; 136 } 137 else 138 { 139 os << "vector object mode is invalid."; 140 } 141 return os; 142 } 143 }
randwalk.cpp
1 #include <IOSTREAM> 2 #include <CSTDLIB> 3 #include <CTIME> 4 #include "vector.h" 5 6 int main(int argc, char * argv[]) 7 { 8 using namespace std; 9 using VECTOR::Vector; 10 srand(time(0)); 11 Vector step; 12 Vector result(0.0, 0.0); 13 unsigned long steps = 0; //步骤 14 double direction; //方向 15 double target; //目标 16 double dstep; //步长 17 cout << "Enter target distance (q to quit): "; 18 while (cin >> target) 19 { 20 cout << "Enter step lenght: "; 21 if (!(cin >> dstep)) 22 { 23 break; 24 } 25 while (result.magval() < target) 26 { 27 direction = rand() % 360; 28 step.set(dstep, direction, 'p'); 29 result = result + step; 30 steps++; 31 } 32 cout << "After " << steps << " steps, the subject has the following loction: \n"; 33 cout << result << endl; 34 result.polar_mode(); 35 cout << "or\n" << result << endl; 36 cout << "Average outward distance per step = " << result.magval() / steps << endl; 37 steps = 0; 38 result.set(0.0, 0.0); 39 cout << "Enter target distance (q to quit): "; 40 } 41 cout << "Bye!\n"; 42 return 0; 43 }