C++

#include <iostream> using namespace std; template <typename H> void HV(H &a, H &b) { H c=a; a=b; b=c; }; template <typename T> class vecto{ public: int size; T *data; public: vecto(); vecto(const vecto<T>&v); vecto(int a, T b); void Show(); vecto operator =(vecto<T>&); T operator [](int i); bool compareSize(vecto&); }; template<typename T> bool vecto<T>::compareSize(vecto<T>&v){ if(this->size>v.size) return true; return false; } template <typename T> vecto<T>::vecto(){ this->size = 0; this->data = NULL; } template<typename T> T vecto<T>::operator [](int i){ return this.data[i]; } template <typename T> vecto<T>::vecto(int a, T b){ this->size=a; this->data=new T[this->size]; for(int i = 0; i<this->size; i++) this->data[i]=b; } ostream& operator <<(ostream&o, vecto<int> &v){ for(int i=0; i<v.size;i++ ) o<<"so thu "<<i+1<<" ="<<v.data[i]<<endl; return o; } template <typename T> vecto<T>::vecto (const vecto&v){ this->size=v.size; this->data=new T[this->size]; for(int i = 0; i<this->size; i++) this->data[i]=v.data[i]; } template<typename U> void Sort(U *d, int n){ for(int i = 0; i<n; i++) for(int j=i+1; j<n; j++) { if(d[i] >= d[j]){ HV<U>(d[i],d[j]); } } } template<typename C> void SortVecto(vecto<C>*v, int n){ for(int i =0; i<n; i++) for(int j =i; j<n; j++){ if(v[i].compareSize(v[j])) HV(v[i],v[j]); } } int main(){ vecto<int>*v; v=new vecto<int>[4]; /*{ vecto<int>(1,3),vecto<int>(2,3),vecto<int>(4,5),vecto<int>(1,2) };*/ SortVecto(v,4); for(int i =0; i<4; i++){ cout<<v[i]; } return 0; } //////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; template <typename T> void HV(T &a, T &b) { T c=a; a=b; b=c; }; class Point2D{ private: int xVal, yVal; public: Point2D(); Point2D(int a, int b):xVal(a),yVal(b){ }; Point2D(const &Point2D); void Show(){ cout<<this->xVal<<" "<<this->yVal<<endl; } }; template <typename T> class vecto{ private: int size; T *d; public: vecto (const vecto&v){ this->size=v.size; this->d=new T[this->size]; for(int i = 0; i<this->size; i++) this->d[i]=v.d[i]; } vecto(int s=1, T n=2){ this->size=s; this->d=new T[this->size]; for(int i = 0; i<this->size; i++) this->d[i]=n; } int& operator [](int i){ static T t = 0; return (this->size>i&&i>=0)?this->d[i]:t; } const vecto& operator =(vecto &v){ this->size=v.size; this->d=new T[this->size]; for(int i = 0; i<this->size; i++) this->d[i]=v.d[i]; return *this; } friend ostream& operator <<(ostream&, vecto & ); friend istream& operator >>(istream&, vecto &); }; ostream& operator <<(ostream&o, vecto<float> &v){ for(int i=0; i<v.size;i++ ) o<<"so thu "<<i+1<<" ="<<v.d[i]<<endl; return o; } istream& operator >>(istream&i, vecto<float> &v){ cout<<"Nhap so luong :"; i>>v.size; cout<<"Nhap gia tri: "; int gt; i>>gt; for(int i=0; i<v.size; i++){ v.d[i]=gt; } return i; } int main() { vecto<float> v1(1,3.8), v2(3,4.5); Point2D p1(1,2), p2(3,4); float a=1.2, b=3.5; HV(v1,v2); cout<<v1<<v2; return 1; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.