Recent Posts

Thứ Hai, 15 tháng 8, 2011

Các kiểu dữ liệu trừu tượng ( C++)





///////////////////////////////////////////////////////////////////////

/////Cac kieu du lieu truu tuong///////
////   Stack   //
template <class T>
class Stack
{
      T* data;
      int top;//Kieu cua top phai la so nguyen
public:
      Stack():top(-1),data(NULL){}
      Stack(T size):top(-1), data(new T[size]){}
      ~Stack(){ delete []data;}//Do data la 1 mang
public:
      void Push(T v)
      {
            data[++top]=v;
      }
      T Pop()
      {
                  return data[top--];
      }
      T Top() { return this->data[top];}
      bool IsEmpty() { return top==-1;}//tra ve 1 neu Satck rong,nguoc lai tra ve 0
};//Finished
///
//  Mot so Class  //////////////////////////////////////////////////////////////////
//Class Complex
//template<class T>
class complex
{
      double re;//Phan thuc
      double im;//Phan ao;
public:
      complex() {re=im=0.0;}
      complex(double real):re(real),im(0.0){}
      complex(double real,double imagine):re(real),im(imagine){}
public:
      /////Xep chong toan tu hai so phuc
      complex operator + (complex &c)
      {
            return complex(this->re+c.re,this->im+c.im);
      }
      complex operator - (complex &c)
      {
            return complex(this->re-c.re,this->im-c.im);
      }
      complex operator * (complex &c)
      {
            double r=this->re*c.re - this->im*c.im;
            double i=this->re*c.im + c.re*this->im;
            return complex(r,i);
      }
      complex operator / (complex &c)
      {
            double m=c.re*c.re + c.im*c.im;
            double r=(this->re*c.re+this->im*c.im)/m;
            double i=(c.re*this->im - this->re*c.im)/m;
            return complex (r,i);
      }
      complex& operator = (complex& z)
      {
            re=z.re;
            im=z.im;
            return *this;
      }
      ///Modul cu so phuc
      double operator () ()
      {
            double x=re*re + im*im;
            return sqrt(x);
      }
      friend ostream& operator << (ostream& o,complex &z)
      {
            return  o <<"["<<z.re<<" "<<z.im<<"]";
      }
};//Ket thuc
/////////////////     Classes         ///////////////////////////
//Class vector
#include<iostream>
#include"process.h"
using namespace std;
template<class T>
class vector
{
protected:
      T *data;
      int length;
public:
      vector(T *p,int len):length(len),data(p){}
      vector():length(0){}
      vector(int len):length(len){}
      //~vector() { delete [] data;}
public:
////////Toan tu + hai vector
vector<T> operator +(vector&v)
{
      vector *p1=this;//con tro this tro den doi tuong dang lam viec
      vector *p2=&v;
      if(p1->length>p2->length)
      {
            p1=p2;
            p2=this;
      }//Thay doi vi tri tro den cua cac con tro su dung lenh gan hai con tro
T *p=new T[p2->length];//Cap phat dong mang de chua du lieu
for(int i=0;i<p1->length;i++)
{
      p[i]=p1->data[i]+p2->data[i];
}
for(int i=p1->length;i<p2->length;i++)//Bat dau vong lap voi gia tri i tu vong lap for truoc
       p[i]=p2->data[i];
return vector(p,p2->length);
}
T &operator [](int index)
{
      return this->data[index];
}

//Toan tu * voi 1 so thuc
vector<T> operator *(const double x)
{
      T *p=new T[this->length];
      for(int i=0;i<this->length;i++)
            p[i]=(this->data[i])*x;
      return vector(p,this->length);
}
vector<T> &operator = ( vector &v)//Kieu tra ve la 1 tham chieu den vector v//Khi goi chu y :Example:vector<int>&B=A;
{
      if(this->length) { delete [] data;}
      length=v.length;
      T*data=new T[this->length];
      for(int i=0;i<this->length;i++)
            data[i]=v.data[i];
      return *this;
}///////////Correctly
public:
void in()//Ham in 1 vector
{
      cout<<"Cac thanh phan cua vector:"<<endl;
      for(int i=0;i<this->length;i++) {
            cout<<this->data[i]<<'\t';}
      cout<<"Chieu dai cua vector la:"<<this->length<<endl;
}
};////Ket thuc Class
/////     Class Matrix        /////////////////
template<class T>
class Matrix: public vector<T>///Class Matrix ke thua Class vector
{
protected:
      int row;//So hang
      int col;//So cot
public:
      Matrix(): {row=col=0;}
      Matrix(int r,int c):vector(r*c),row(r),col(c){}//Khai bao vector(r*c) de tao ra 1 vector co chieu dai length=r*c//
      Matrix( T* A,int r,int c):vector(A,r*c),row(r),col(c)
      {
            T*data=new T[row*col];
            for(int i=0;i<(row*col);i++)
                  data[i]=A[i];
      }
      //~Matrix() { delete [] data;}


public:
      Matrix<T> operator + (Matrix &M)
      {
            if(row!=M.row||col!=M.col)exit(0);

            T *P=new T[length];
            for(int i=0;i<length;i++)
                  P[i]=this->data[i]+M.data[i];
            return Matrix(P,row,col);
      }
      Matrix<T> operator * (Matrix &M)
      {
            if(row!=M.col) exit(0);
            T *p=new T[row*M.col];
            for(int i=0,k=0,k1=0;i<row;i++,k1+=col)
            {
                  for(int j=0;j<M.col;j++,k++)
                  {
                        double S=0;
                        int k3=j;
                        for(int k2=0;k2<col;k2++)
                        {
                              S+=data[k1+k2]*M.data[k3];
                              k3+=col;
                              p[k]=S;
                        }
                  }
            }
            return Matrix(p,row,M.col);
      }

//// Toan tu " = ",va cac ham "in()",.....duoc ke thua tu Class Vector.

};///Ket thuc Class
/////////    Class Array      ///////////////////
// Array.h
#pragma once

template <class T>
class Array
{
protected:
      T *data;
      int size;

protected:
      void CopyData(const T * data)
      {
            for (int i = 0; i < size; i++)
                  this->data[i] = data[i];
      }

public:
      Array() : size(0), data(NULL) {}
      Array(int size) : size(size), data(new T[size]) {}
      Array(int size, const T *data)
      {
            this->size = size;
            this->data = new T[size];
            CopyData(data);
      }
      Array(Array & A)
      {
            this->size = A.size;
            this->data = new T[size];
            CopyData(A.data);
      }
      ~Array() { if (data) delete[] data; }

public:
      int GetSize() { return this->size; }

public:
      T& operator [](int index) { return this->data[index]; }

public:
      Array<T>& operator =(const T * data)
      {
            CopyData(data);
            return *this;
      }

      Array<T>& operator =(const Array & A)
      {
            if (this->data) delete[] data;
            this->size = A.size;
            this->data = new T[size];
            CopyData(A.data);

            return *this;
      }
};


Không có nhận xét nào:

Đăng nhận xét