问题:
解答:
main.cpp
#include <iostream>
#include <fstream>
#include "Vector.h"
#include <time.h>
using namespace std;
using namespace VECTOR;
int main()
{
ofstream fout;
fout.open("randwalk.txt");
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "请输入目标距离(q结束):";
while (cin>>target&&cin.get()!='q')
{
cout << "请输入步长:";
if (!(cin >> dstep))
break;
fout << "目标距离: " << target << ", 步长: " << dstep << endl;
while (result.magval()<target)
{
fout << steps << " : " << result << endl;
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result =result+step;
steps++;
}
cout << "经过 " << steps << "步,实验对象的位置如下:\n";
cout << result << endl;
fout << "经过 " << steps << "步,实验对象的位置如下:\n";
fout << result << endl;
result.polar_mode();
cout << " 或者\n" << result << endl;
cout << "每一步向外的平均距离 = " << result.magval() / steps << endl;
fout << " 或者\n" << result << endl;
fout << "每一步向外的平均距离 = " << result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "请输入目标距离(q结束):";
}
cout << "Bye!\n";
fout.close();
return 0;
}
Vector.h
#pragma once
#include <iostream>
using namespace std;
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode from = RECT);
~Vector();
double xval()const { return x; }
double yval()const { return y; }
double magval()const { return mag; }
double angval()const { return ang; }
void polar_mode();
void rect_mode();
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& a);
friend ostream& operator<<(ostream& os, const Vector& v);
};
}
Vector.cpp
#include "Vector.h"
#include <cmath>
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);//45/(π/4)
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);
}
}
void Vector::set_x()
{
x = mag * cos(ang);
}
void Vector::set_y()
{
y = mag * sin(ang);
}
Vector::Vector()
{
x = y = mag = ang = 0.0;
mode = RECT;
}
Vector::Vector(double n1, double n2, Mode form)
{
mode = form;
if (mode == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (mode == POL)
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "错误!" << endl;
x = y = mag = ang = 0.0;
mode = RECT;
}
}
void Vector::reset(double n1, double n2, Mode from)
{
mode = from;
if (mode == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (mode == POL)
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "错误!" << endl;
x = y = mag = ang = 0.0;
mode = RECT;
}
}
Vector::~Vector()
{
}
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode = RECT;
}
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& a)
{
return a * n;
}
ostream& operator<<(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;
}
}
运行结果:
考查点:
-
写文件
-
运算符重载
-
友元
-
命名空间
-
默认参数
-
内联函数
-
枚举
-
随机数
-
数学
2024年9月5日15:02:35