Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ include_directories(include)

enable_testing()

set(SOURCES src/vector.cpp)

add_executable(test_vector tests/test_vector.cpp ${SOURCES})

add_test(NAME ${PROJECT_NAME} COMMAND test_vector)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# vector
Linear space element representation
[![Build status](https://ci.appveyor.com/api/projects/status/5hqdicdrxuhkt958/branch/ivanov?svg=true)](https://ci.appveyor.com/project/Alexalexlxl/vector/branch/ivanov)
[![Build Status](https://travis-ci.org/Alexalexlxl/vector.svg?branch=ivanov)](https://travis-ci.org/Alexalexlxl/vector)
201 changes: 160 additions & 41 deletions include/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,168 @@
#define VECTOR_H

class Vector {
public:
const static unsigned long n = 3;

Vector();

explicit Vector(double);

Vector(const Vector &);

Vector &operator=(const Vector &);

double operator[](unsigned long i) const;

double &operator[](unsigned long i);

Vector &operator+=(const Vector &);

Vector &operator-=(const Vector &);

Vector &operator*=(double);

Vector &operator/=(double);

friend bool operator==(const Vector &, const Vector &);

friend Vector operator+(const Vector &, const Vector &);

friend Vector operator-(const Vector &, const Vector &);

friend Vector operator*(const Vector &, double);

friend Vector operator*(double, const Vector &);

friend Vector operator/(const Vector &, double);

friend double operator^(const Vector &, const Vector &);

Vector operator-() const;

public:
const static unsigned long n = 3;

Vector()
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] = 0;
}
};

explicit Vector(double number)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] = number;
}
};

Vector(const Vector &other)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] = other.coords_[i];
}
};

Vector &operator=(const Vector &other)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] = other.coords_[i];
}
return *this;
};

double operator[](unsigned long i) const
{
return coords_[i];
};

double &operator[](unsigned long i)
{
return coords_[i];
};

Vector &operator+=(const Vector &other)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] += other.coords_[i];
}
return *this;
};

Vector &operator-=(const Vector &other)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] -= other.coords_[i];
}
return *this;
};

Vector &operator*=(double number)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] *= number;
}
return *this;
};

Vector &operator/=(double number)
{
for (unsigned long i = 0; i < n; i++)
{
coords_[i] /= number;
}
return *this;
};

friend bool operator==(const Vector &lhs, const Vector &rhs)
{
for (unsigned long i = 0; i < n; ++i)
{
if (lhs.coords_[i] != rhs.coords_[i])
{
return false;
}
}
return true;
};

friend Vector operator+(const Vector &our, const Vector &other)
{
Vector Vector_return(our);
return Vector_return += other;
};

friend Vector operator-(const Vector &our, const Vector &other)
{
Vector Vector_return(our);
return Vector_return -= other;
};

friend Vector operator*(const Vector &our, double number)
{
Vector Vector_return(our);
return Vector_return *= number;
};

friend Vector operator*(double number, const Vector &our)
{
Vector Vector_return(our);
return Vector_return *= number;
};

friend Vector operator/(const Vector &our, double number)
{
Vector Vector_return(our);
return Vector_return /= number;
};

friend std::ostream &operator<<(std::ostream &stream, const Vector &v)
{
for (unsigned long i = 0; i < n; i++)
{
stream<<v.coords_[i]<<" ";
}
return stream;
};

friend double operator^(const Vector &our, const Vector &other)
{
double sum_return = 0;
for (unsigned long i = 0; i < n; i++)
{
sum_return = sum_return + our.coords_[i]*other.coords_[i];;
}
return sum_return;
};

Vector operator-()const
{
Vector Vector_return;
for (unsigned long i = 0; i < n; i++)
{
Vector_return.coords_[i] = (-1)*coords_[i] ;
}
return Vector_return;
};

private:
double coords_[n];
double coords_[n];

}; // class Vector

bool operator!=(const Vector &, const Vector &);
bool operator!=(const Vector &our, const Vector &other)
{
return !(our == other);
};

#endif // VECTOR_H
#endif // VECTOR_H