定义一个矢量类,用之实现随机漫步,并用文本输出过程及结果。
沿用Example13中的Vector类定义。
程序清单如下:
// vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
#include <cmath>
namespace VECTOR
{
const double RadToDeg = 45.0 / std::atan(1.0); // 设置常量
class Vector
{
public:
enum Mode {RECL, POL}; // 状态成员
private:
Mode mode; // 状态成员
double x;
double y;
double mag;
double ang; // 各坐标值
void setX();
void setY();
void setMag();
void setAng(); // 设置各坐标值的函数
public:
// 构造函数和析构函数
Vector();
Vector(double n1, double n2, Mode form = RECL);
~Vector();
// 设置坐标值的函数
void reset(double n1, double n2, Mode form = RECL);
// 设置坐标模式的函数
void modeRecl();
void modePol();
// 返回各坐标值的函数
double getX() const {return x;}
double getY() const {return y;}
double getMag() const {return mag;}
double getAng() const {return ang;}
// 运算符重载函数
Vector operator+(const Vector& b) const;
Vector operator-(const Vector& b) const;
Vector operator-() const;
Vector operator*(double n) const;
// 友元函数
friend Vector operator*(double n, const Vector& b);
friend std::ostream& operator<<(std::ostream& os, const Vector& b);
};
}
#endif
// vector.cpp
#include "vector.h"
namespace VECTOR
{
// 定义私有部分成员函数
void Vector::setX()
{
x = mag * std::cos(ang);
}
void Vector::setY()
{
y = mag * std::sin(ang);
}
void Vector::setMag()
{
mag = std::sqrt(x * x + y * y);
}
void Vector::setAng()
{
if (x == 0.0 && y == 0.0)
ang = 0.0;
else
ang = std::atan2(y, x);
}
// 定义公有部分成员函数
// 定义构造函数和析构函数
Vector::Vector()
{
mode = RECL;
x = y = mag = ang = 0.0;
}
Vector::Vector(double n1, double n2, Mode form)
{
switch (form)
{
case RECL: x = n1; y = n2;
setMag(); setAng();
break;
case POL: mag = n1; ang = n2;
setX(); setY();
break;
}
}
Vector::~Vector()
{
}
// 定义设置坐标值的函数
void Vector::reset(double n1, double n2, Mode form)
{
switch (form)
{
case RECL: x = n1; y = n2;
setMag(); setAng();
break;
case POL: mag = n1; ang = n2;
setX(); setY();
break;
}
}
// 定义设置坐标模式的函数
void Vector::modeRecl()
{
mode = RECL;
}
void Vector::modePol()
{
mode = POL;
}
// 定义运算符重载函数
Vector Vector::operator+(const Vector& b) const
{
return Vector(x + b.x, y + b.y);
}
Vector Vector::operator-(const Vector& b) const
{
return Vector(x - b.x, y - b.y);
}
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
Vector Vector::operator*(double n) const
{
return Vector(x * n, y * n);
}
// 定义友元函数
Vector operator*(double n, const Vector& b)
{
return b * n;
}
std::ostream& operator<<(std::ostream& os, const Vector& b)
{
switch (b.mode)
{
case Vector::RECL: return os << "(x, y) = (" << b.x << ", " << b.y << ")\n";
break;
case Vector::POL: return os << "(m, a) = (" << b.mag << ", " << b.ang << ")\n";
break;
}
}
}
// randwalk.cpp
#include "vector.h"
#include <fstream>
#include <cstdlib>
#include <ctime>
double getTarget();
double getDstep();
double getRandAng() {return rand() % 360;}
int main()
{
// 设置输出文本
std::ofstream randWalkTxt;
randWalkTxt.open("randwalk.txt");
// 设置随机种子
std::srand(std::time(0));
// 获取目标距离和步幅
double target = getTarget();
randWalkTxt << "目标距离:" << target;
double dstep = getDstep();
randWalkTxt << ", 步幅:" << dstep << "\n";
// 声明其他变量
using VECTOR::Vector;
Vector result;
Vector step;
unsigned long steps = 0;
// 实现随机漫步的循环体
while (result.getMag() < target)
{
randWalkTxt << steps << ": (x, y) = (" << result.getX() << ", " << result.getY() << ")\n";
step.reset(dstep, getRandAng(), Vector::POL);
result = result + step;
steps++;
}
randWalkTxt << steps << ": (x, y) = (" << result.getX() << ", " << result.getY() << ")\n\n";
// 输出计算结果到文本
randWalkTxt << "经过" << steps << "步达到目标距离,历程:" << dstep * steps
<< ",平均步长为:" << target / steps << "\n";
randWalkTxt << "最终坐标位置为:\n";
randWalkTxt << "\t(x, y) = (" << result.getX() << ", " << result.getY() << ")\n";
randWalkTxt << "\t(m, a) = (" << result.getMag() << ", " << result.getAng() << ")\n";
randWalkTxt.close();
// 结束
std::cout << "计算完成,按任意键退出。";
system("pause>nul");
return 0;
}
double getTarget()
{
double result;
std::cout << "请输入目标距离:";
if (!(std::cin >> result))
{
std::cin.clear();
while (std::cin.get() != '\n')
continue;
std::cout << "输入错误,请重新输入:";
}
return result;
}
double getDstep()
{
double result;
std::cout << "请输入步幅:";
if (!(std::cin >> result))
{
std::cin.clear();
while (std::cin.get() != '\n')
continue;
std::cout << "输入错误,请重新输入:";
}
return result;
}
结束。
原文链接: https://www.cnblogs.com/zhuangdong/archive/2013/05/04/3059690.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/87071
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!