设计向量类,实现类CVector的构造函数、拷贝构造函数、赋值函数、析构函数、实现向量的加减法与内积。 10
设计向量类,实现类CVector的构造函数、拷贝构造函数、赋值函数、析构函数、实现向量的加减法与内积。ClassCVector{public:CVector(constd...
设计向量类,实现类CVector的构造函数、拷贝构造函数、赋值函数、析构函数、实现向量的加减法与内积。
Class CVector
{ public:
CVector(const double *data=NULL, int length=0);
CVector(const CVector&other);
CVector& operate = (const CVector&other);
~CVector(void);
private:
double *m_data;
int m_length;
}; 展开
Class CVector
{ public:
CVector(const double *data=NULL, int length=0);
CVector(const CVector&other);
CVector& operate = (const CVector&other);
~CVector(void);
private:
double *m_data;
int m_length;
}; 展开
3个回答
展开全部
我这里有Vector3的头文件和cpp文件。改一下,你应该可以使用
MyVector.h
#pragma once
#include <math.h>
#define PI 3.14159265358979323846
//_____________________________________
// class Vector3D ---> An object to represent a 3D vector or a 3D point in space
class Vector3D
{
public:
float x; // the x value of this Vector3D
float y; // the y value of this Vector3D
float z; // the z value of this Vector3D
Vector3D(); // Constructor to set x = y = z = 0
Vector3D(float x, float y, float z); // Constructor that initializes this Vector3D to the intended values of x, y and z
Vector3D& operator= (Vector3D v); // operator= sets values of v to this Vector3D. example: v1 = v2 means that values of v2 are set onto v1
Vector3D operator+ (Vector3D v); // operator+ is used to add two Vector3D's. operator+ returns a new Vector3D
Vector3D operator- (Vector3D v); // operator- is used to take difference of two Vector3D's. operator- returns a new Vector3D
friend Vector3D operator *(float scale,Vector3D v);//用友元函数处理一个数乘以一个向量
Vector3D operator* (float value); // operator* is used to scale a Vector3D by a value. This value multiplies the Vector3D's x, y and z.
Vector3D operator/ (float value); // operator/ is used to scale a Vector3D by a value. This value divides the Vector3D's x, y and z.
Vector3D& operator+= (Vector3D v); // operator+= is used to add another Vector3D to this Vector3D.
Vector3D& operator-= (Vector3D v) ; // operator-= is used to subtract another Vector3D from this Vector3D.
Vector3D& operator*= (float value); // operator*= is used to scale this Vector3D by a value.
Vector3D& operator/= (float value); // operator/= is used to scale this Vector3D by a value.
Vector3D operator- (); // operator- is used to set this Vector3D's x, y, and z to the negative of them.
float length(); // length() returns the length of this Vector3D
void setZero(); // set x = y = z = 0
void normalSelf(); // normalizes this Vector3D that its direction remains the same but its length is 1.
Vector3D normalNewOne(); // returns a new Vector3D. The returned value is a unitized version of this Vector3D.
};
typedef Vector3D Point;
//_____________________________________
//function
float distance(const Point& p1,const Point& p2);
float dot(const Vector3D& v1,const Vector3D& v2);
Vector3D cross(const Vector3D& a,const Vector3D& b);
MyVector.cpp
#include "MyVector.h"
Vector3D::Vector3D() // Constructor to set x = y = z = 0
{
x = 0;
y = 0;
z = 0;
}
Vector3D::Vector3D(float x, float y, float z) // Constructor that initializes this Vector3D to the intended values of x, y and z
{
this->x = x;
this->y = y;
this->z = z;
}
Vector3D& Vector3D::operator= (Vector3D v) // operator= sets values of v to this Vector3D. example: v1 = v2 means that values of v2 are set onto v1
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
Vector3D Vector3D::operator+ (Vector3D v) // operator+ is used to add two Vector3D's. operator+ returns a new Vector3D
{
return Vector3D(x + v.x, y + v.y, z + v.z);
}
Vector3D Vector3D::operator- (Vector3D v) // operator- is used to take difference of two Vector3D's. operator- returns a new Vector3D
{
return Vector3D(x - v.x, y - v.y, z - v.z);
}
Vector3D operator *(float scale,Vector3D v){
return Vector3D(v.x*scale,v.y*scale,v.z*scale);
}
Vector3D Vector3D::operator* (float value) // operator* is used to scale a Vector3D by a value. This value multiplies the Vector3D's x, y and z.
{
return Vector3D(x * value, y * value, z * value);
}
Vector3D Vector3D::operator/ (float value) // operator/ is used to scale a Vector3D by a value. This value divides the Vector3D's x, y and z.
{
return Vector3D(x / value, y / value, z / value);
}
Vector3D& Vector3D::operator+= (Vector3D v) // operator+= is used to add another Vector3D to this Vector3D.
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vector3D& Vector3D::operator-= (Vector3D v) // operator-= is used to subtract another Vector3D from this Vector3D.
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vector3D& Vector3D::operator*= (float value) // operator*= is used to scale this Vector3D by a value.
{
x *= value;
y *= value;
z *= value;
return *this;
}
Vector3D& Vector3D::operator/= (float value) // operator/= is used to scale this Vector3D by a value.
{
x /= value;
y /= value;
z /= value;
return *this;
}
Vector3D Vector3D::operator- () // operator- is used to set this Vector3D's x, y, and z to the negative of them.
{
return Vector3D(-x, -y, -z);
}
float Vector3D::length() // length() returns the length of this Vector3D
{
return sqrtf(x*x + y*y + z*z);
}
void Vector3D::setZero(){ // set x = y = z = 0
x = 0;
y = 0;
z = 0;
}
void Vector3D::normalSelf() // normalizes this Vector3D that its direction remains the same but its length is 1.
{
float length = this->length();
if (length == 0)
return;
x /= length;
y /= length;
z /= length;
}
Vector3D Vector3D::normalNewOne() //returns a new Vector3D. The returned value is a unitized version of this Vector3D.
{
float length = this->length();
if (length == 0)
return *this;
return Vector3D(x / length, y / length, z / length);
}
//____________________________________________________________________
float distance(const Point& p1,const Point& p2){
float f = (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z);
return sqrt(f);
}
float dot(const Vector3D& v1,const Vector3D& v2){
return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
}
Vector3D cross(const Vector3D& a,const Vector3D& b){
Vector3D t;
t.x = a.y*b.z-a.z*b.y;
t.y = a.z*b.x-a.x*b.z;
t.z = a.x*b.y-a.y*b.x;
return t;
}
MyVector.h
#pragma once
#include <math.h>
#define PI 3.14159265358979323846
//_____________________________________
// class Vector3D ---> An object to represent a 3D vector or a 3D point in space
class Vector3D
{
public:
float x; // the x value of this Vector3D
float y; // the y value of this Vector3D
float z; // the z value of this Vector3D
Vector3D(); // Constructor to set x = y = z = 0
Vector3D(float x, float y, float z); // Constructor that initializes this Vector3D to the intended values of x, y and z
Vector3D& operator= (Vector3D v); // operator= sets values of v to this Vector3D. example: v1 = v2 means that values of v2 are set onto v1
Vector3D operator+ (Vector3D v); // operator+ is used to add two Vector3D's. operator+ returns a new Vector3D
Vector3D operator- (Vector3D v); // operator- is used to take difference of two Vector3D's. operator- returns a new Vector3D
friend Vector3D operator *(float scale,Vector3D v);//用友元函数处理一个数乘以一个向量
Vector3D operator* (float value); // operator* is used to scale a Vector3D by a value. This value multiplies the Vector3D's x, y and z.
Vector3D operator/ (float value); // operator/ is used to scale a Vector3D by a value. This value divides the Vector3D's x, y and z.
Vector3D& operator+= (Vector3D v); // operator+= is used to add another Vector3D to this Vector3D.
Vector3D& operator-= (Vector3D v) ; // operator-= is used to subtract another Vector3D from this Vector3D.
Vector3D& operator*= (float value); // operator*= is used to scale this Vector3D by a value.
Vector3D& operator/= (float value); // operator/= is used to scale this Vector3D by a value.
Vector3D operator- (); // operator- is used to set this Vector3D's x, y, and z to the negative of them.
float length(); // length() returns the length of this Vector3D
void setZero(); // set x = y = z = 0
void normalSelf(); // normalizes this Vector3D that its direction remains the same but its length is 1.
Vector3D normalNewOne(); // returns a new Vector3D. The returned value is a unitized version of this Vector3D.
};
typedef Vector3D Point;
//_____________________________________
//function
float distance(const Point& p1,const Point& p2);
float dot(const Vector3D& v1,const Vector3D& v2);
Vector3D cross(const Vector3D& a,const Vector3D& b);
MyVector.cpp
#include "MyVector.h"
Vector3D::Vector3D() // Constructor to set x = y = z = 0
{
x = 0;
y = 0;
z = 0;
}
Vector3D::Vector3D(float x, float y, float z) // Constructor that initializes this Vector3D to the intended values of x, y and z
{
this->x = x;
this->y = y;
this->z = z;
}
Vector3D& Vector3D::operator= (Vector3D v) // operator= sets values of v to this Vector3D. example: v1 = v2 means that values of v2 are set onto v1
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
Vector3D Vector3D::operator+ (Vector3D v) // operator+ is used to add two Vector3D's. operator+ returns a new Vector3D
{
return Vector3D(x + v.x, y + v.y, z + v.z);
}
Vector3D Vector3D::operator- (Vector3D v) // operator- is used to take difference of two Vector3D's. operator- returns a new Vector3D
{
return Vector3D(x - v.x, y - v.y, z - v.z);
}
Vector3D operator *(float scale,Vector3D v){
return Vector3D(v.x*scale,v.y*scale,v.z*scale);
}
Vector3D Vector3D::operator* (float value) // operator* is used to scale a Vector3D by a value. This value multiplies the Vector3D's x, y and z.
{
return Vector3D(x * value, y * value, z * value);
}
Vector3D Vector3D::operator/ (float value) // operator/ is used to scale a Vector3D by a value. This value divides the Vector3D's x, y and z.
{
return Vector3D(x / value, y / value, z / value);
}
Vector3D& Vector3D::operator+= (Vector3D v) // operator+= is used to add another Vector3D to this Vector3D.
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vector3D& Vector3D::operator-= (Vector3D v) // operator-= is used to subtract another Vector3D from this Vector3D.
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vector3D& Vector3D::operator*= (float value) // operator*= is used to scale this Vector3D by a value.
{
x *= value;
y *= value;
z *= value;
return *this;
}
Vector3D& Vector3D::operator/= (float value) // operator/= is used to scale this Vector3D by a value.
{
x /= value;
y /= value;
z /= value;
return *this;
}
Vector3D Vector3D::operator- () // operator- is used to set this Vector3D's x, y, and z to the negative of them.
{
return Vector3D(-x, -y, -z);
}
float Vector3D::length() // length() returns the length of this Vector3D
{
return sqrtf(x*x + y*y + z*z);
}
void Vector3D::setZero(){ // set x = y = z = 0
x = 0;
y = 0;
z = 0;
}
void Vector3D::normalSelf() // normalizes this Vector3D that its direction remains the same but its length is 1.
{
float length = this->length();
if (length == 0)
return;
x /= length;
y /= length;
z /= length;
}
Vector3D Vector3D::normalNewOne() //returns a new Vector3D. The returned value is a unitized version of this Vector3D.
{
float length = this->length();
if (length == 0)
return *this;
return Vector3D(x / length, y / length, z / length);
}
//____________________________________________________________________
float distance(const Point& p1,const Point& p2){
float f = (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z);
return sqrt(f);
}
float dot(const Vector3D& v1,const Vector3D& v2){
return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
}
Vector3D cross(const Vector3D& a,const Vector3D& b){
Vector3D t;
t.x = a.y*b.z-a.z*b.y;
t.y = a.z*b.x-a.x*b.z;
t.z = a.x*b.y-a.y*b.x;
return t;
}
展开全部
Class CVector;
CVector operater+ (const CVector&lhs, const CVector &rhs);
CVector operater- (const CVector&lhs, const CVector &rhs);
double operator· (const CVector&lhs, const CVector &rhs);
Class CVector
{ public:
CVector(const double *data=NULL, int length=0);
CVector(const CVector&other);
CVector& operate = (const CVector&other);
~CVector(void);
friend CVector operater+ (const CVector&lhs, const CVector &rhs);
friend CVector operater- (const CVector&lhs, const CVector &rhs);
friend double operater· (const CVector&lhs, const CVector &rhs);
private:
double *m_data;
int m_length;
};
CVector::CVector(const double *data=NULL, int length=0){
m_data = new double[length];
for(int i = 0; i < length; ++i)
m_data[i] = data[i];
m_length = length;
}
CVector::CVector(const Cvector&other) {
m_data = new double[other.m_length];
for(int i = 0; i <other.m_length; ++i)
m_data[i] =other.m_data[i];
m_length =other.m_length;
}
CVector& CVector::operate = (const CVector&other){
if(m_data != NULL) {
delete []m_data;
m_length = 0;
}
m_data = new double[other.m_length];
for(int i = 0; i <other.m_length; ++i)
m_data[i] =other.m_data[i];
m_length =other.m_length;
return *this;
}
CVector::~CVector(void){
if(m_data != NULL) {
delete []m_data;
m_length = 0;
}
}
CVector operater+ (const CVector&lhs, const CVector &rhs){
CVector res(lhs);
for(int i = 0; i <res.m_length; ++i)
res.m_data[i] += rhs.m_data[i];
return res;
}
CVector operater- (const CVector&lhs, const CVector &rhs){
CVector res(lhs);
for(int i = 0; i <res.m_length; ++i)
res.m_data[i] -= rhs.m_data[i];
return res;
}
double operater· (const CVector&lhs, const CVector &rhs){
double res = 0.0;
for(int i = 0; i <lhs.m_length; ++i)
res += lhs.m_data[i] * rhs.m_data[i];
return res;
}
CVector operater+ (const CVector&lhs, const CVector &rhs);
CVector operater- (const CVector&lhs, const CVector &rhs);
double operator· (const CVector&lhs, const CVector &rhs);
Class CVector
{ public:
CVector(const double *data=NULL, int length=0);
CVector(const CVector&other);
CVector& operate = (const CVector&other);
~CVector(void);
friend CVector operater+ (const CVector&lhs, const CVector &rhs);
friend CVector operater- (const CVector&lhs, const CVector &rhs);
friend double operater· (const CVector&lhs, const CVector &rhs);
private:
double *m_data;
int m_length;
};
CVector::CVector(const double *data=NULL, int length=0){
m_data = new double[length];
for(int i = 0; i < length; ++i)
m_data[i] = data[i];
m_length = length;
}
CVector::CVector(const Cvector&other) {
m_data = new double[other.m_length];
for(int i = 0; i <other.m_length; ++i)
m_data[i] =other.m_data[i];
m_length =other.m_length;
}
CVector& CVector::operate = (const CVector&other){
if(m_data != NULL) {
delete []m_data;
m_length = 0;
}
m_data = new double[other.m_length];
for(int i = 0; i <other.m_length; ++i)
m_data[i] =other.m_data[i];
m_length =other.m_length;
return *this;
}
CVector::~CVector(void){
if(m_data != NULL) {
delete []m_data;
m_length = 0;
}
}
CVector operater+ (const CVector&lhs, const CVector &rhs){
CVector res(lhs);
for(int i = 0; i <res.m_length; ++i)
res.m_data[i] += rhs.m_data[i];
return res;
}
CVector operater- (const CVector&lhs, const CVector &rhs){
CVector res(lhs);
for(int i = 0; i <res.m_length; ++i)
res.m_data[i] -= rhs.m_data[i];
return res;
}
double operater· (const CVector&lhs, const CVector &rhs){
double res = 0.0;
for(int i = 0; i <lhs.m_length; ++i)
res += lhs.m_data[i] * rhs.m_data[i];
return res;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询