chapter11test2

这里的程序和chapter11test1相似,不过按照题目要求,我的理解是:把vector.h中的void setdis()/void setang()删掉,然后用x/y计算dis/ang带入原来有void setdis()/void setang()的地方,这里只把vector.h和vector.cpp附上,user.cpp没有变化

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();
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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值