您的位置:首页 > 编程语言 > C语言/C++

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

2014-04-19 15:19 555 查看
那两个函数我就弄上来了,都是和11.1一样的

main113.cpp

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息