C++primer plus第六版课后编程练习答案11.3

之前的类都是都是和11.1一样的
#include <iostream>
#include <fstream>
#include <cstdio>
#include <ctime>
//#include <vector>
#include "vector.h"
using namespace std;
using  namespace VECTOR;
void main113()
{
	int MaxStep=-999;
	int MinStep=999;
	int sumS=0;
	int avg=0;
	int num;
	cout<<"Enter the number:"<<endl;
	cin>>num;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0,0.0);
	unsigned long steps=0;
	double target;//要走的距离
	double dstep;//每一步的距离
	cout<<"Enter step length:(q to quit:)";
	int number=num;
	while(cin>>target&&number--)
	{
		cout<<"Enter step length:";
		if(!(cin>>dstep))
			break;
		int i=0;
		while(result.magval()<target)
		{
			direction=rand()%360;
			step.reset(dstep,direction,Vector::POL);
			result=result+step;
			steps++;
		}
		sumS+=steps;
		if(steps>MaxStep)
			MaxStep=steps;
		if(steps<MinStep)
			MinStep=steps;

		cout<<"After "<<steps<<" steps ,the subject "
			<<" has the following location:"<<endl;
		
		result.polar_mode();
		cout<<"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<<"Max:"<<MaxStep<<endl;
	cout<<"Min:"<<MinStep<<endl;
	cout<<"avg:"<<sumS/num<<endl;

	cin.get();

	cin.clear();
	while(cin.get()!='\n')
		continue;

}
来自 http://blog.csdn.net/qq844352155/article/details/24112981
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值