chapter11test3

这段程序最多修改的是main()部分;废了我一吨的脑细胞,听着宋冬野的歌,唱的真扯淡

vector.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
class Vector
{
public:
enum Mode{Rect,Pol};
private:
double x;
double y;
double ang;
double dis;
Mode mode;
void setx();
void sety();

public:
Vector();
~Vector();
Vector(double a, double b, Mode form = Rect);
void reset(double a, double b, Mode form=Rect);
double xshow() const { return x; }
double yshow() const { return y; }
double disshow() const { return dis; }
double angshow() const{ return ang; }
void rmode();
void pmode();
double wmax();
double wmin();
Vector operator+(const Vector &v)const;
Vector operator-(Vector &v)const;
Vector operator-()const;
Vector operator*(double n)const;
friend Vector operator*(double n,const Vector&v);
friend std::ostream &operator<<(std::ostream &os, const Vector &v);
};


#endif


vector.cpp

#include"vector.h"
#include<cmath>
#include<iostream>
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::sqrt;
using std::cout;
using std::endl;
const double dan = 45.0 / atan(1.0);


void Vector::setx()
{
x = dis*cos(ang);
}
void Vector::sety()
{
y = dis*sin(ang);
}


Vector::Vector()
{
x = y = dis = ang = 0;
}
Vector::~Vector()
{}
Vector::Vector(double a, double b, Mode form)
{
mode = form;
if (mode == Rect)
{
x = a; y = b;
dis=sqrt(x*x+y*y);
ang=atan2(y,x);
}
else if (mode == Pol)
{
dis = a;
ang = b;
setx();
sety();
}
else
cout << "Wrong input .\n";
}
void Vector::reset(double a, double b, Mode form)
{
mode = form;
if (mode == Rect)
{
x = a; y = b;
dis=sqrt(x*x+y*y);
ang=atan2(y,x);
}
else if (mode == Pol)
{
dis = a;
ang = b;
setx();
sety();
}
else
cout << "Wrong input .\n";
}
void Vector::rmode()
{
mode = Rect;
}
void Vector::pmode()
{
mode = Pol;
}
Vector Vector::operator+(const Vector &v)const
{
return Vector(x + v.x, y + v.y);
}
Vector Vector::operator-(Vector &v)const
{
return Vector(x - v.x, y - v.y);
}
Vector Vector::operator-()const
{
return Vector(-x, -y);
}
Vector Vector::operator*(double n)const
{
return Vector(x*n, y*n);
}
Vector operator*(double n, const Vector&v)
{
return v*n;
}
std::ostream &operator<<(std::ostream &os, const Vector &v)
{
if (v.mode == Vector::Rect)
os << "(x,y)=(" << v.x << "," << v.y << ")" << endl;
else if (v.mode == Vector::Pol)
os << "(Dis,Ang)=(" << v.dis << "," << v.ang << ")" << endl;
else
cout << "Wrong input.\n";
return os;
}
double Vector::wmax()
{
double m=0;
if (m > dis)
return m;
else
return dis;
}
double Vector::wmin()
{
double m=0;
if (m < dis)
return m;
else
return dis;
}


user.cpp

#include<iostream>
#include"vector.h"
#include<cstdlib>
#include<ctime>
#include<fstream>
int main()
{
using namespace std;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps=0;
unsigned long arr[5];
unsigned long max, min;
unsigned long total=0;
double mean;
double target;
double dstep;
int static time = 0;
cout << "Enter target distance:";
while (cin >> target&&time < 5)
{
cin.clear();
cout << "Enter step length :";
if (!(cin >> dstep))
break;
cin.clear();
while (result.disshow() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::Pol);
result = result + step;
++steps;
}
arr[time] = steps;
time++;
cout << "Time= " << time << " ;Steps= " << steps << endl;
steps = 0; result.reset(0.0, 0.0);
cout << "Enter target distance:";
}
max = min = arr[0];
for (int i = 0; i < time; i++)
{
total += arr[i];
if (max < arr[i])
max = arr[i];
else;
if (min>arr[i])
min = arr[i];
else;
}
mean = total / time;
cout << "After " << time << " trying,we have the follow info:" << endl;
cout << "Max steps= " << max << " ;Min steps= " << min << endl;
cout << "Mean steps =" << mean << endl;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值