【C++ Primer Plus习题】11.3

问题:

解答:

main.cpp

cpp 复制代码
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect.h"

using namespace std;
using namespace VECTOR;


int main()
{
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	int max = 0;
	int min = INT_MAX;
	int sum = 0;
	int count = 0;
	cout << "请输入实验次数:";
	cin >> count;
	for (int i = 0; i < count; i++)
	{
		cout << "请输入第"<<i+1<<"次实验目标距离(按q结束) :";
		cin >> target;
		cout << "请输入第" << i + 1 << "次实验步长: ";
		if (!(cin >> dstep))break;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
		}
		sum += steps;
		if (max < steps)
		{
			max = steps;
		}
		if (min > steps)
		{
			min = steps;
		}
		cout << "经过 " << steps << " 步,这实验对象位置如下:" << endl;
		cout << result << endl;
		result.polar_mode();
		cout << " 或者\n" << result << endl;
		cout << "平均每外出一步的距离为:" << result.magval() / steps << endl;
		
		steps = 0;
		result.reset(0.0, 0.0);
	}

	cout << count << "次实验的平均步数为:" << sum / count << endl;
	cout << count << "次实验的最高步数为:" << max << endl;
	cout << count << "次实验的最低步数为:" << min << endl;
	cout << "Bye!" << endl;

	return 0;
}

vect.h

cpp 复制代码
#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);
	};

}

vect.cpp

cpp 复制代码
#include "vect.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日16:11:53

相关推荐
Kx…………1 小时前
Day2:前端项目uniapp壁纸实战
前端·学习·uni-app·实战·项目
Swift社区1 小时前
从表格到序列:Swift 如何优雅地解 LeetCode 251 展开二维向量
开发语言·leetcode·swift
hweiyu001 小时前
Python从入门到精通全套视频教程免费
开发语言·python
Better Rose1 小时前
蓝桥杯备赛学习笔记:高频考点与真题预测(C++/Java/python版)
笔记·学习·蓝桥杯
浪淘沙jkp2 小时前
大模型学习七:‌小米8闲置,直接安装ubuntu,并安装VNC远程连接手机,使劲造
服务器·学习·ubuntu·deepseek
唐人街都是苦瓜脸2 小时前
Java RPC 框架是什么
java·开发语言·rpc
黑不溜秋的2 小时前
Ubuntu24.04 编译 Qt 源码
开发语言·qt
ALex_zry2 小时前
C++17模板编程与if constexpr深度解析
开发语言·c++·性能优化
AugustShuai3 小时前
API-标准controller接口
开发语言·json·设计规范·post·标准接口
幻想趾于现实3 小时前
C# Winform 入门(15)之制作二维码和拼接(QR)
开发语言·c#·winform