#ifndef VEC_H #define VEC_H /////////////////////////////////////////////////////////////// // vec.h - vector class // // // // version: 1.2 // // Language: Visual C++, ver 5.0 // // Platform: Micron Dual Pentium Pro 200 // // Windows NT 4.0, service pack 3 // // Application: ELE591/791 Example // // Author: Jim Fawcett // // CST 2-187, (315) 443-3948 // // fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// /* vec Operations: ================== This is a template-based class representing vectors of arbitrary non-negative dimesion. vec Public Interface: ======================== vec yourVec; // create null vector vec myVec(5); // create vector with 5 elements yourVec = myVec; // rebuild yourVec as a clone of myVec int val = myVec[3] = 7; // read/write to 4th element myVec *= 1.5; // multiply myVec by scalar 1.5 vec prod = 1.5*myVec; // scalar multiplication myVec += yourVec; // add yourVec to myVec vec sum = myVec + yourVec; // vector addition myVec -= yourVec; // subtract yourVec from myVec vec diff = myVec - yourVec; // vector subtraction int len = myVec.size(); // read myVec's size myVec.size(7); // change myVec's size, keep elements double n = myVec.norm(); // compute vector absolute norm myVec.display(cout,5,4); // display myVec on cout with 5 elements // per line in a field width of 4 chars. in >> yourVec; // extract yourVec values from stream in out << myVec; // insert myVec values onto stream out. */ // /////////////////////////////////////////////////////////////// // // // Build Process // // ------------- // // Files required: // // vec.h vec.cpp // // // // build command for Neptune cluster with VC++ 5.0: // // I:\fawcett\ecs500\setenv.bat (neptune only) // // cl -GX -DTEST_VEC vec.cpp // // // /////////////////////////////////////////////////////////////// /* Maintenance History ------------------- ver 1.2 : 01 June 00 - cosmetic modifications only ver 1.1 : 12 July 98 - added vector addition and scalar multiplication operators ver 1.0 : 28 June 98 - first release */ // #include #include using namespace std; //----< vec class declaration >-------------------------------- template class vec { T *array; int numElements; public: vec(int n=0); vec(const vec &v); ~vec(); vec& operator= (const vec &v); vec& operator*=(const T &t); vec operator* (const T &t) const; vec& operator+=(const vec &v); vec operator+ (const vec &v) const; vec& operator-=(const vec &v); vec operator- (const vec &v) const; T& operator[](int n); T operator[](int n) const; T norm() const; int size() const; void size(int n); void display(ostream &out, int fold=5, int field=14) const; }; template vec operator*(const T &t, const vec &v); //----< return number of elements in vec >--------------------- template inline int vec::size() const { return numElements; } //----< abort with message >----------------------------------- static void killJob(char *s) { cout << " " << s << endl; exit(1); } //----< size constructor and void constructor >---------------- template vec::vec(int n) : numElements(n) { if(n<=0) { numElements = 0; array = 0; return; } array = new T[numElements]; if(!array) killJob("memory alloc failure in vec"); } // //----< copy constructor >------------------------------------- template vec::vec(const vec &v) : numElements(v.numElements) { array = new T[numElements]; if(!array) killJob("memory alloc failure in vec copy ctor\n"); int i; for(i=0; i------------------------------------------- template vec::~vec() { delete [] array; numElements = 0; } //----< resize vec's array >----------------------------------- template void vec::size(int n) { if(n <= 0) { cout << "attempt to set size = " << n << endl; return; } T *temp = new T[n]; if(!temp) killJob("memory alloc failure in size(n)"); int i; for(i=0; i---------------------------------- template vec& vec::operator=(const vec &v) { if(this == &v) return *this; if(numElements != v.numElements) { delete [] array; array = new T[numElements = v.numElements]; if(!array) killJob("memory alloc failure in assignment"); } int i; for(i=0; i------------------------- template vec operator*(const T &t, const vec &v) { vec temp(v.size()); int i; for(i=0; i------------------------ template vec& vec::operator*=(const T &t) { int i; for(i=0; i-------------------------------- template vec vec::operator*(const T &t) const { vec temp = (*this); return (temp *= t); } // //----< vector addition to self >------------------------------ template vec& vec::operator+=(const vec &v) { if(numElements != v.numElements) { cout << " dimension mismatch in vec::operator+(...)\n"; return *this; } int i; for(i=0; i-------------------------------------- template vec vec::operator+(const vec &v) const { vec temp = (*this); return (temp += v); } //----< vector subtraction to self >--------------------------- template vec& vec::operator-=(const vec &v) { if(numElements != v.numElements) { cout << " dimension mismatch in vec::operator+(...)\n"; return *this; } int i; for(i=0; i----------------------------------- template vec vec::operator-(const vec &v) const { vec temp = (*this); return (temp -= v); } // //----< non-const index operator - can be written to >--------- template T& vec::operator[](int n) { static T safe; if(n <0 || numElements <= n) { cout << " index domain error in operator[]\n"; return safe; } return array[n]; } //----< const index operator - can only be read >-------------- template T vec::operator[](int n) const { static T safe; if(n <0 || numElements <= n) { cout << " index domain error in operator[]\n"; return safe; } return array[n]; } //----< display vec with fold elements/line in width field >--- template void vec::display(ostream &out, int fold, int field) const { int i; for(i=0; i----------------------------------- template ostream& operator<<(ostream &out, const vec &v) { const int fold=5, field=14, lenfield=5; out << setw(lenfield) << v.size() << '\n'; int i; for(i=0; i---------------------------------- template istream& operator>>(istream &in, vec &v) { int len; in >> len; if(len <= 0) return in; v.size(len); int i; for(i=0; i> v[i]; return in; } //----< absolute norm >---------------------------------------- template T vec::norm() const { T sum = 0; int i; for(i=0; i