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

来源:互联网 发布:pcb线路板设计软件 编辑:程序博客网 时间:2024/06/05 00:11

那两个函数我就弄上来了,都是和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;}


0 0