// vect.cpp -- methods for Vector class
#include <cmath>
#include "vect.h"
#include"iostream.h"
// includes <iostream>
/*using std::sqrt;
using std::sin;
using std::cos;
using std::atan2;
using std::cout;*/
namespace VECTOR
{
const double Rad_to_deg = 57.2957795130823;
// private methods
// calculates magnitude from x and y
void Vector::set_mag()
{
mag = sqrt(x * x + y * y);
}
void Vector::set_ang()
{
if (x == 0.0 && y == 0.0)
ang = 0.0;
else
ang = atan2(y, x);
}
// set x from polar coordinate
void Vector::set_x()
{
x = mag * cos(ang);
}
// set y from polar coordinate
void Vector::set_y()
{
y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = mag = ang = 0.0;
mode = 'r';
}
// construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
Vector::Vector(double n1, double n2, char form)
{
mode = form;
if (form == 'r')
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 'p')
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = 'r';
}
}
// set vector from rectangular coordinates if form is r (the
// default) or else from polar coordinates if form is p
void Vector:: set(double n1, double n2, char form)
{
mode = form;
if (form == 'r')
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == 'p')
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = 'r';
}
}
Vector::~Vector() // destructor
{
}
void Vector::polar_mode() // set to polar mode
{
mode = 'p';
}
void Vector::rect_mode() // set to rectangular mode
{
mode = 'r';
}
// operator overloading
// add two Vectors
Vector Vector::operator+(const Vector & b) const
{
return Vector(x + b.x, y + b.y);
}
// subtract Vector b from a
Vector Vector::operator-(const Vector & b) const
{
return Vector(x - b.x, y - b.y);
}
// reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
// multiple vector by n
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
// friend methods
// multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return a * n;
}
// display rectangular coordinates if mode is r,
// else display polar coordinates if mode is p
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == 'r')
os << "(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == 'p')
{
os << "(m,a) = (" << v.mag << ", "
<< v.ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
} // end namespace VECTOR
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论









收起资源包目录





































































































共 296 条
- 1
- 2
- 3
资源评论

- yyailp2012-02-16确实没有课后题答案。都是代码
- lyric02212011-11-28只是例子的原代码,没有习题的答案代码,哎。名不副实
- glyrocky2011-11-15是书本上的实例代码,不是课后练习题
- hsg__tg2011-10-12下了几天,没下下来。不知道是不是没有种子了

stqcw
- 粉丝: 1
- 资源: 27
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
