summaryrefslogtreecommitdiff
path: root/graphs/cpp/cartesian_vector
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/cpp/cartesian_vector')
-rw-r--r--graphs/cpp/cartesian_vector/format_numerical_data.cc13
-rw-r--r--graphs/cpp/cartesian_vector/format_numerical_data.hh22
-rw-r--r--graphs/cpp/cartesian_vector/state_saver.cc11
-rw-r--r--graphs/cpp/cartesian_vector/state_saver.hh20
-rw-r--r--graphs/cpp/cartesian_vector/vector.cc69
-rw-r--r--graphs/cpp/cartesian_vector/vector.hh29
-rw-r--r--graphs/cpp/cartesian_vector/vector_test.cc27
7 files changed, 191 insertions, 0 deletions
diff --git a/graphs/cpp/cartesian_vector/format_numerical_data.cc b/graphs/cpp/cartesian_vector/format_numerical_data.cc
new file mode 100644
index 0000000..b6355d2
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/format_numerical_data.cc
@@ -0,0 +1,13 @@
+#include "format_numerical_data.hh"
+std::ostream& FormatNumericalData::formatOs(std::ostream& os) const
+{
+ if (precision >= 0)
+ {
+ os.precision(precision);
+ }
+ if (scientific_notation)
+ std::scientific(os);
+ if (display_plus)
+ std::showpos(os);
+ return os;
+} \ No newline at end of file
diff --git a/graphs/cpp/cartesian_vector/format_numerical_data.hh b/graphs/cpp/cartesian_vector/format_numerical_data.hh
new file mode 100644
index 0000000..359e297
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/format_numerical_data.hh
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <ostream>
+#include <string>
+
+struct FormatNumericalData
+{
+ //! string to be printed before the data
+ std::string prefix;
+ //! string to be printed after the data
+ std::string suffix;
+ //! gives the minimum number of digits to appear (after the radix if
+ //! scientific_notation is enabled)
+ int precision = -1;
+ //! define if scientific notation is enabled
+ bool scientific_notation = false;
+ //! define if a sign + should be placed before a number
+ bool display_plus = false;
+
+ //! format the given stream according to class attributes
+ std::ostream& formatOs(std::ostream& os) const;
+};
diff --git a/graphs/cpp/cartesian_vector/state_saver.cc b/graphs/cpp/cartesian_vector/state_saver.cc
new file mode 100644
index 0000000..4b4a244
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/state_saver.cc
@@ -0,0 +1,11 @@
+#include "state_saver.hh"
+StateSaver::StateSaver(std::ostream& os)
+ : saved_stream_{ os }
+ , saved_flags_{ os.flags() }
+ , saved_precision_{ os.precision() }
+{}
+StateSaver::~StateSaver()
+{
+ saved_stream_.flags(saved_flags_);
+ saved_stream_.precision(saved_precision_);
+} \ No newline at end of file
diff --git a/graphs/cpp/cartesian_vector/state_saver.hh b/graphs/cpp/cartesian_vector/state_saver.hh
new file mode 100644
index 0000000..f52127c
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/state_saver.hh
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <iostream>
+
+class StateSaver
+{
+public:
+ //! constructor should save all flags and precision
+ StateSaver(std::ostream& os);
+ //! destructor should restore all flags and precision
+ ~StateSaver();
+
+private:
+ //! original stream
+ std::ostream& saved_stream_;
+ //! flags of the original stream
+ std::ios_base::fmtflags saved_flags_;
+ //! precision of the original stream
+ std::streamsize saved_precision_;
+};
diff --git a/graphs/cpp/cartesian_vector/vector.cc b/graphs/cpp/cartesian_vector/vector.cc
new file mode 100644
index 0000000..1291f69
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/vector.cc
@@ -0,0 +1,69 @@
+#include "vector.hh"
+
+#include "state_saver.hh"
+
+Vector::Vector(double x, double y)
+ : x_{ x }
+ , y_{ y }
+{}
+double Vector::get_x() const
+{
+ return x_;
+}
+double Vector::get_y() const
+{
+ return y_;
+}
+Vector& Vector::operator+=(const Vector& rhs)
+{
+ x_ += rhs.get_x();
+ y_ += rhs.get_y();
+ return *this;
+}
+Vector& Vector::operator-=(const Vector& rhs)
+{
+ x_ -= rhs.get_x();
+ y_ -= rhs.get_y();
+ return *this;
+}
+Vector& Vector::operator*=(double scalar)
+{
+ x_ *= scalar;
+ y_ *= scalar;
+ return *this;
+}
+Vector operator+(const Vector& lhs, const Vector& rhs)
+{
+ Vector lhs1{ lhs };
+ return lhs1 += rhs;
+}
+Vector operator-(const Vector& lhs, const Vector& rhs)
+{
+ Vector lhs1{ lhs };
+ return lhs1 -= rhs;
+}
+Vector operator*(const Vector& lhs, double scalar)
+{
+ Vector lhs1{ lhs };
+ return lhs1 *= scalar;
+}
+Vector operator*(double scalar, const Vector& rhs)
+{
+ return rhs * scalar;
+}
+double operator*(const Vector& lhs, const Vector& rhs)
+{
+ return lhs.get_x() * rhs.get_x() + lhs.get_y() * rhs.get_y();
+}
+
+FormatNumericalData Vector::format_numerical_data_{ "[ ", " ]", 12, true,
+ true };
+
+std::ostream& operator<<(std::ostream& os, const Vector& vec)
+{
+ StateSaver savestate{ os };
+ Vector::format_numerical_data_.formatOs(os);
+ os << Vector::format_numerical_data_.prefix << vec.get_x() << " , "
+ << vec.get_y() << Vector::format_numerical_data_.suffix;
+ return os;
+} \ No newline at end of file
diff --git a/graphs/cpp/cartesian_vector/vector.hh b/graphs/cpp/cartesian_vector/vector.hh
new file mode 100644
index 0000000..b18838c
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/vector.hh
@@ -0,0 +1,29 @@
+#pragma once
+#include <iostream>
+
+#include "format_numerical_data.hh"
+
+class Vector
+{
+public:
+ Vector() = default;
+ Vector(double x, double y);
+ double get_x() const;
+ double get_y() const;
+
+ Vector& operator+=(const Vector& rhs);
+ Vector& operator-=(const Vector& rhs);
+ Vector& operator*=(double scalar);
+
+ friend Vector operator+(const Vector& lhs, const Vector& rhs);
+ friend Vector operator-(const Vector& lhs, const Vector& rhs);
+ friend Vector operator*(const Vector& lhs, double scalar);
+ friend Vector operator*(double scalar, const Vector& rhs);
+ friend double operator*(const Vector& lhs, const Vector& rhs);
+ friend std::ostream& operator<<(std::ostream& os, const Vector& vec);
+
+private:
+ double x_ = 0;
+ double y_ = 0;
+ static FormatNumericalData format_numerical_data_;
+};
diff --git a/graphs/cpp/cartesian_vector/vector_test.cc b/graphs/cpp/cartesian_vector/vector_test.cc
new file mode 100644
index 0000000..d822c9f
--- /dev/null
+++ b/graphs/cpp/cartesian_vector/vector_test.cc
@@ -0,0 +1,27 @@
+#include <cmath>
+#include <iostream>
+
+#include "vector.hh"
+
+double pi()
+{
+ return std::atan2(0., -1.);
+}
+
+int main()
+{
+ std::cout << "default state pi (double) : " << pi() << '\n';
+ Vector piVector{ pi(), pi() };
+ Vector t(-1.0E-7, 6.812031E-4);
+ std::cout << "piVector :\n" << piVector << '\n';
+ std::cout << "t + piVector :\n" << t + piVector << '\n';
+
+ Vector u{ 1923, 8 };
+ std::cout << "u :\n" << u << '\n';
+ std::cout << "default state (double) : " << u * t << '\n';
+ t -= u;
+ std::cout << "t :\n" << t << '\n';
+ std::cout << "t * 3 :\n" << t * 3 << '\n';
+
+ return 0;
+}