c++ Primer Plus(第六版)第十一章,编程练习之路

c++ Primer Plus(习题11.1~2)

/*书上的例题,不过换成文件输出*/
/*这个比方便了一点,不用那些标准io,和cout用法的用法一样*/
/*并且会覆盖以前的*/
/*连着做第二题,修改头文件,不修改接口*/
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include"vector.h"
int 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;
	ofstream file;						//输出到文件
	file.open("酒鬼漫步.txt");
	cout << "Enter target distance(q to quit):";
	while (cin >> target)
	{
		cout << "Enter the length: ";
		if (!(cin >> dstep))
			break;
		cout << "Target Distance: " << target << " , " << "Step Size: " << dstep<<endl;
		file << "Target Distance: " << target << " , " << "Step Size: " << dstep << endl;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			cout << steps << ": " << result << endl;	
			file<< steps << ": " << result << endl;			//输出到文件
			steps++;

		}
		cout << steps << ": " << result << endl;			//外面补一个,我认为
		file<<steps << ": " << result << endl;
		cout << "After " << steps << " steps,the subject "
			"has the following location:\n";
		file<< "After " << steps << " steps,the subject "
			"has the following location:\n";
		cout << result << endl;
		file << result << endl;
		result.polar_mode();
		cout << "Or\n" << result << endl;
		file << "Or\n" << result << endl;
		cout << "Average outward distance per step="
			<< result.magval() / steps << endl;
		file<<"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";
	cin.clear();
	while (cin.get() != '\n')
		continue;
	file.close();
	return 0;

}
头文件

/*vertor.h--矢量的类,with<<,made state.*/
#pragma once
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR					//矢量的名称空间
{
	class Vector
	{
	public:
		enum Mode{RECT,POL};		//两种模式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; };			//报告x坐标值
		double yval()const { return y; };			//报告y坐标值
		double magval()const ;		//报告矢量长度
		double angval()const ;		//报告矢量角度
		void polar_mode();							//设置模式为极坐标
		void rect_mode();							//设置模式为直角坐标
		//运算符重载
		Vector operator+(const Vector &b)const;		//重载加法
		Vector operator-(const Vector &b)const;		//重载j减法
		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);	//重载<<运算符
	};
}
#endif // !VECTOR_H
实现文件

/*vector类的方法*/
#include<cmath>
#include"vector.h"			//已经包含iostream.h
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;			//返回半个圆的弧度
using std::atan2;			//返回一个圆的弧度
using std::cout;

namespace VECTOR
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值