From a8c71acb64aec0a699adc81c57aa9ee083d1d582 Mon Sep 17 00:00:00 2001 From: mooophy Date: Wed, 3 Dec 2014 17:45:57 +1300 Subject: [PATCH] add operator+ for Node modified: as1/assignment01_159201.cpp --- assignment1/as1/assignment01_159201.cpp | 49 +++++++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/assignment1/as1/assignment01_159201.cpp b/assignment1/as1/assignment01_159201.cpp index 4112306..a6d3865 100644 --- a/assignment1/as1/assignment01_159201.cpp +++ b/assignment1/as1/assignment01_159201.cpp @@ -26,13 +26,26 @@ operator<<(std::ostream& os, Node const& rhs) return os << rhs.row << " " << rhs.column << " " << rhs.value << std::endl; } +inline Node +operator +(Node const& lhs, Node const& rhs) +{ + auto ret = lhs; + ret.value += rhs.value; + ret.next = nullptr; + return ret; +} + /////////////////////////////////////////////////////////////////////////////// //! forward declarations -template class SparseMatrix; -template std::ostream& print_data(SparseMatrix const& m); +template +class SparseMatrix; +template +std::ostream& print_data(SparseMatrix const& m); +template +SparseMatrix operator+(SparseMatrix const& lhs, SparseMatrix const& rhs); /** @@ -43,13 +56,22 @@ template std::ostream& print_data(SparseMatrix const& m); template class SparseMatrix { - friend std::ostream& print_data(SparseMatrix const& m); + friend std::ostream& + print_data(SparseMatrix const& m); + friend SparseMatrix + operator+ (SparseMatrix const& lhs, SparseMatrix const& rhs); + public: using ValueType = decltype(Node::value); using IndexType = decltype(Node::row); using SizeType = std::size_t; - SparseMatrix() = delete; + SparseMatrix(): + head_{nullptr}, + tail_{nullptr}, + rows_{0}, + cols_{0} + {} /** * @brief Ctor using file name @@ -58,10 +80,7 @@ public: * @abstraction Top */ explicit SparseMatrix(std::string fn): - head_{nullptr}, - tail_{nullptr}, - rows_{0}, - cols_{0} + SparseMatrix{} { construct_linked_list(fn); } @@ -176,11 +195,25 @@ inline std::ostream& print_data(SparseMatrix const& m) return std::cout; } +template +SparseMatrix operator+(SparseMatrix const& lhs, SparseMatrix const& rhs) +{ + if(lhs.cols_ != rhs.cols_ or lhs.rows_ != rhs.rows_) + throw std::runtime_error{"trying to add matrix with different dimensions!"}; + auto rows = lhs.rows_, cols = rhs.cols_; + + SparseMatrix ret; + + //! working +} + }//namespace int main() { + std::cout << (ads::Node{1,2,42,nullptr} + ads::Node{1,2,10,nullptr}); + ads::SparseMatrix m{"matrix1.txt"}; std::cout << "size : " <