C++ Primer Plus第六版第十一章使用类编程练习答案

本文提供了C++ Primer Plus第六版第十一章的类编程练习答案,包括涉及类声明和定义的vector.h、Time.h、Stonewt.h头文件,以及对应的main.cpp测试代码,详细解释了各个练习的实现思路。
摘要由CSDN通过智能技术生成

1.

main.cpp

#include "Vector.h"
#include<cstdlib>
#include<ctime>
#include<fstream>
int main()
{
   
	using namespace std;
	ofstream outFile;
	outFile.open("hello.txt");
	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;
		outFile << "Target Distance: " << target
			<< ", Step Size: " << dstep << endl;
		outFile << "0: (x,y) = (0, 0)" << endl;
		while (result.magval() < target)
		{
   
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			outFile << steps + 1 << ": " << result << endl;
			steps++;
		}
		outFile << "After " << steps << " steps, the subject "
			"has the following location:\n";
		outFile << result << endl;
		result.polar_mode();
		outFile << " or\n" << result << endl;
		outFile << "Average outward distance per step = "
			<< result.magval() / steps << endl;
		steps = 0;
		result.reset(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
	}
	cout << "Bye!\n";
	outFile.close();
	cin.clear();
	while (cin.get() != '\n')
		continue;
	return 0;
}

vector.h
为了方便,类的声明和定义都在头文件里面了

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
using std::cout;
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 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);
	};
	const double Rad_to_deg = 45.0 / atan(1.0);
	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 (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;
		}
	}
	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()
	{
   
	}
	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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值