11.9-2&3

在这里插入图片描述

		void main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	cout << "Enter target distance (q to quit):";
	while (cin>>target)
	{
		cout << "Enter step length: ";
		if (!(cin>>dstep))
		{
			break;
		}
		double a[10000] = {};
		while (result.magval()<target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;

			int i = 0;
			a[i] = step.xval();
			//cout << a[i]<<endl;
			i++;


			steps++;
			//fout << result << endl;
		}
		
			double max = a[0];
			double min = a[0];
			for (int i = 0; i < steps; i++)
			{
				if (max < a[i])
				{
					max = a[i];
				}
				if (min>a[0])
				{
					min = a[0];
				}
			}
			cout << "max: " << max  <<"	,"<<"min: " << max << endl;
		cout << "After " << steps << " steps,the subject"
			"has the following location:\n";

		cout << result << endl;
		result.polar_mode();
		cout << "or\n" << result << endl;
		cout << "Average outward distance per step = "
			<< result.magval() / steps << endl;
		steps = 0.0;
		result.reset(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
	}
	cout << "Bye!\n";
	cin.clear();
	while (cin.get()!='\n')
	{
		continue;
	}
	return ;
}

在这里插入图片描述
在这里插入图片描述

#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
namespace VECTOR
{
	class Vector
	{
	public:
		enum Mode{RECT,POL};
	protected:
	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 form = 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 std::ostream& operator<<(std::ostream & os, const Vector & v);

		operator double()const;

		Vector magval();
		Vector angval();
		double magval() const;
		double angval() const;
	};
}

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值