- 修改程序清单 11.15,使之报告N次测试中的最高、最低和平均步数(其中N是用户输入的整数)
而不是报告每次测试的结果。
头文件和实现文件不变,这里为大家方便还是贴上代码
//vect.h -- Vector class with <<,mode state
#if 1
#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };//以此为直角坐标,极坐标(长度,角度)
//RECT for rectangular,POL for Polar modes
private:
double x;//horizontal value x方向x坐标
double y;//vertical value y方向y坐标
double mag; //length of value 极坐标的长度
double ang;//direction of vector in degrees,极坐标的角度
Mode mode;//RECT or POL
//private methods for setting values
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double x, double y, Mode form = RECT);
void reset(double x, double y, Mode form = RECT);
~Vector();
double xval()const { return x; }//report x val
double yval()const { return y; }//report y val
double magVal()const { return mag; }//report magnitude
double angval()const { return ang; }//report angle
void polar_mode();//set mode to POL
void rect_mode();//set mode to RECT
//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);
};// end Vector
}//end namespace VECTOR
#endif
文件不变,不过之前的函数Vector(double,double,mode)有一个bug,更新过来了。
大家有机会去研究一下。
#if 1
#include<cmath>
#include"vector.h" //includes<iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
//comput degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
//should be about 57.2957795130823
//private methods
//calculates magnitude from x and y
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);
}
}
//set x from polar coorinate
void Vector::set_x()
{
x = mag * cos(ang);
}
//set y from polar coorinate
void Vector::set_y()
{
y = mag * sin(ang);
}
//public methods
//default constructor
Vector::Vector()
{
y = x = mag = ang = 0.0;
mode = RECT;
}
//construct vector from rectangular coordinates if form is r
//(the default) or else from polay coordinates if form is p
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 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
//reset vector from rectangular coorinates if form is
//RECT(the default) or else from polar coorinates if
//form is POL
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 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
Vector::~Vector()//destructor
{
}
void Vector::polar_mode()//set to polay mode
{
mode = POL;
}
void Vector::rect_mode() // set to rectangular mode
{
mode = RECT;
}
//operator overloading
//add two Vectors
Vector Vector::operator+(const Vector& b)const
{
return Vector(x + b.x, y + b.y);
}
//sub vector b from a
Vector Vector::operator-(const Vector& b)const
{
return Vector(x - b.x, y - b.y);
}
//reverse sign of Vector
Vector Vector::operator-()const
{
return Vector(-x, -y);
}
//multyply vector by n
Vector Vector::operator*(double n)const
{
return Vector(n * x, n * y);
}
//friend methods
//multiply n by Vector a
Vector operator*(double n, const Vector& a)
{
return a * n;
}
//display rectangular coorinates if mode is RECT
//else display polar coordinates if mode is POL
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_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
}//end namespace VECTOR
#endif // 0
测试函数,有条件的兄弟们可以测试一下
这里每次执行结果都是不一样的。随机的
#pragma 练习3 3.cpp
/*
*/
#if 1
#include <iostream>
#include"vector.h"
using namespace std;
using namespace VECTOR;
int main()
{
srand(time(0));
while (true)
{
double setp;
double distance;
unsigned times;
unsigned max_setps = 0;
unsigned min_setps = numeric_limits<unsigned>::max();
unsigned sum_setps = 0;
cout << "请输入距离:";
cin >> distance;
if (!cin || distance <= 0)break;
//清空缓冲区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "请输入步长:";
cin >> setp;
if (!cin || setp <= 0)break;
//清空缓冲区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "请输入测试循环次数:";
cin >> times;
if (!cin)break;
//清空缓冲区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(2);
for (unsigned i = 0; i < times; i++)
{
unsigned cntSteps = 0;
Vector vec(0,0,Vector::POL);
cout << i << ": ";
while(vec.magVal() < distance)
{
vec = vec + Vector(setp, rand() % 360, Vector::POL);
cntSteps++;
// cout << cntSteps << endl;
}
cout << cntSteps << "步行到" << vec << endl;
if (cntSteps > max_setps)
{
max_setps = cntSteps;
}
if (cntSteps < min_setps)
{
min_setps = cntSteps;
}
sum_setps += cntSteps;
}
cout << "距离" << distance << ",步长" << setp << ",测试" << times << "次,最多次步"
<< max_setps << "步,最少走步" << min_setps << "步,平均走步" << sum_setps / times << "步\n";
}
cin.clear();
return 0;
}
#endif
#pragma endregion
这里测试了两次,两次结果都不一样,这是一个随机的结果