#ifndef READDATA_H #define READDATA_H ///////////////////////////////////////////////////////////////////////// // matrix.h - Fairly simple prototype for a matrix class // ver 1.0 // // Language: Visual C++, ver 6.0 // Platform: Dell XPS B1000r, Windows 2000 // Application: Demonstration for ELE591/791 - Intelligent Systems // Author: Jim Fawcett, CST 2-187, Syracuse University // (315) 443-3948, jfawcett@twcny.rr.com ///////////////////////////////////////////////////////////////////////// /* Module Operations ================= mat class defines construction, indexing, and I/O operations consistent with a mathematical matrix. This prototype does not define the usual mathematical operations, e.g., +,-,*,inversion,norm,... Public Interface ================ mat m; // void construction of empty matrix mat m(3,5); // construction of matrix with 3 rows, 2 cols m.resize(4,2); // reconfigures matrix to 4 rows, 2 cols double d = m[0][2] = 3.5; // read and write a matrix element m.name() = "matrix m"; // give matrix a name cout << m; // display matrix with default field width of 8 in >> m; // read data into an existing matrix int r = m.rows(); // return number of rows int c = m.cols(); // return number of columns */ /////////////////////////////////////////////////////////////////////////// // Build Process /////////////////////////////////////////////////////////////////////////// // Required Files: // matrix.h, matrix.cpp // // Command Line Compile: // cl /GX matrix.cpp setargv.obj /////////////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.0 : 29 June 2000 - first release */ // #include // basic I/O module #include // manipulators module #include // standard strings module #include // STL extendable array /////////////////////////////////////////////////////////////////////////// // mat class declaration /////////////////////////////////////////////////////////////////////////// template class mat { public: mat(); mat(int row, int col); void resize(int row, int col); std::vector& operator[](int n); std::vector operator[](int n) const; void put(std::ostream &out, int width=8) const; void get(std::istream &in); int rows() const; int cols() const; std::string& name(); std::string name() const; // Don't need copy constructor, assignment operator, or destructor // The compiler generated versions work correctly. private: std::string _name; std::vector< std::vector > _array; }; //----< void constructor >----------------------------------------------- // // Needed only because compiler will not generate void ctor // if any other ctors have been defined for this class. template inline mat::mat() {} // //----< sizing constructor >--------------------------------------------- template mat::mat(int row, int col) { _array.resize(row); int i,j; for(i=0; i---------------------------------------- template void mat::resize(int rows, int cols) { _array.resize(rows); int i; for(i=0; i---------------------------------- // // Would also work for reading data from non-constant matrix, // but would not work for reading data from constant matrix template inline std::vector& mat::operator[](int n) { if(n<0 || _array.size() <= n) throw "index out of bounds in mat::operator[]"; return _array[n]; } //----< index operator for reading data >---------------------------------- // // Works for reading data from constant matrix template inline std::vector mat::operator [](int n) const { if(n<0 || _array.size() <= n) throw "index out of bounds in mat::operator[]"; return _array[n]; } // //----< return number of rows >-------------------------------------------- template inline int mat::rows() const { return _array.size(); } //----< return number of cols >-------------------------------------------- template inline int mat::cols() const { if(rows() > 0) return _array[0].size(); return 0; } //----< read or write matrix name >---------------------------------------- template std::string& mat::name() { return _name; } //----< read name of constant matrix >------------------------------------- template std::string mat::name() const { return _name; } //----< write matrix onto output stream >---------------------------------- template void mat::put(std::ostream &out, int width) const { if(_name.size() > 0) out << "\n " << _name; out << "\n " << rows() << " " << cols(); int i,j; for(i=0; i---------------------------------------- template std::ostream& operator<<(std::ostream &out, const mat &m) { m.put(out); return out; } // //----< read matrix from input stream >------------------------------------ template void mat::get(std::istream &in) { string line; int row, col; do { // The next line will read in leading white space and any // characters it can interpret as part of an integer, e.g., // '-', '+', or any digits. If this results in a valid // integer the read succeeds and the stream stays in good // state. Otherwise the stream enters a bad state. in >> row >> col; if(!in.good()) { in.clear(); getline(in,line); // read line of text if(!in.good()) throw "can't read matrix"; if(name().size() == 0) name() = line; // set matrix name } else break; } while(true); resize(row,col); int i,j; for(i=0; i> _array[i][j]; } //----< global extraction operator >--------------------------------------- template std::istream& operator>>(std::istream& in, mat &m) { m.get(in); return in; } #endif