现在的位置: 首页 > 综合 > 正文

设计模式中的相关基础类文件

2017年12月07日 ⁄ 综合 ⁄ 共 6172字 ⁄ 字号 评论关闭

这个文件是设计模式中可能要用到的数据结构,将它根据需要补全,在后绪的学习中将会根据需要慢慢补,不断更新中。

 

// BasicClass.h: interface for the BasicClass class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_)
#define AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// 
#include <iostream>
/*using namespace std::;*/
using std::cout;
using std::ostream;
using std::istream;
//#include <iostream.h> 
//std::list<int>

#define DEFAULT_LIST_CAPACITY 1000
template
<class T> class List;

template
<class T> class ListNode//T便是一个指针
{
public:
    friend 
class List<T>;
    ListNode()
{ _data = NULL; _next = NULL; }

    ListNode(
const T& item);
    ListNode
& operator=(const ListNode& );
    T _data;
    ListNode
<T>* _next;

}
;



template
<class T> class List//T便是一个指针
{
public:
    List(
long size = DEFAULT_LIST_CAPACITY){ _last = NULL;_first = NULL,_current = NULL; _size = 0; }
    List(List
& list);
    
~List(){}
    List
& operator=(const List&);

    
long Count() const
    
{
        
return _size;
    }

    T
& Get(long index) const
    
{
        ListNode
<T>* p = _first;
        
for(int i=0; i<index; i++)
        
{
            p 
= p->_next;
        }

        
return p->_data;
    }

    T
& First() const
    
{
        
return _first->_data;
    }

    ListNode
<T>* FirstPoint()
    
{
        
return _first;
    }

    T
& Last() const
    
{
        
return _last->_data;
    }

    
bool Includes(const T& data) const
    
{
        ListNode
<T>* p = _first;
        
while ( p != NULL )
        
{
            
if(data == p->_data)
            
{
                
return true;
            }

        }


        
return false;
    }


    
void Append(const T& data)
    
{
        ListNode
<T> * p = new ListNode<T>;

        
if(_current == NULL)
        
{
            _current 
= p;
            _current
->_data = data;
            
if(_first == NULL)
            
{//空链表
                _first = _last = _current;
            }

        }

        
else
        
{
            p
->_next = _current->_next;
            _current
->_next = p;
            _current 
= _current->_next;//将当前指针指向插入的节点

            
if(_current->_next == NULL)
            
{//如果是在链尾加,
                _last = _current;
            }

        }

    }

    
//     void Prepend(const T&data)
    
//     {
    
//         ListNode<T>* p = new ListNode<T>;
    
// 
    
//         if(_current == NULL)
    
//         {
    
// 
    
//         }
    
//     }


    
void Remove(const T& data)
    
{

    }

    
void RemoveLast();
    
void RemoveFirst();
    
void RemoveAll();

    T
& Top() const;
    
void Push(const T&);
    T
& Pop();

    ListNode
<T>* _first;
    ListNode
<T>* _last;
    ListNode
<T>* _current;
    
int _size;
}
;



template
<class T>
class Iterator
{
    friend 
class List<T>;
public:
    
virtual void First() = 0;
    
virtual void Next() = 0;
    
virtual bool IsDone() const = 0;
    
virtual T CurrentItem() const = 0;
}
;

template
<class T>// ListNode<equment*>*
class ListIterator: public Iterator<T>
{
public:
    T _ptr;
    T _first;

    ListIterator()
{ _ptr = 0; _first = 0; }
    ListIterator(T alist)
{_ptr = alist; _first = alist; }

    
//ListIterator(const ListIterator<T> aList& );

    
virtual void First() { _ptr = _first; }
    
virtual void Next(){ _ptr = _ptr->_next; }
    
virtual bool IsDone() constreturn _ptr != NULL ;}

    
virtual T CurrentItem() const
    
{
        
return _ptr;
    }

}
;


typedef 
float Coord;
class Point{
public:
    
static const Point Zero;

    Point(Coord x 
= 0.0,Coord y = 0.0);

    Coord X() 
const;//得到x值
    Coord Y() const;
    
void X(Coord x);//设置x值
    void Y(Coord y);

    Point 
operator+(const Point&);
    Point 
operator-(const Point&);
    Point 
operator*(const Point&);
    Point 
operator/(const Point&);
    
    
void operator+=(const Point&);
    
void operator-=(const Point&);
    
void operator*=(const Point&);
    
void operator/=(const Point&);

    
bool operator==(const Point&);
    
bool operator!=(const Point&);

     friend ostream
& operator<<(ostream&const Point&);
     friend istream
& operator>>(istream&, Point&);
private:
    Coord m_x;
    Coord m_y;
}
;

class Rect
{
public:
    
static const Rect Zero;

    Rect(Coord x, Coord y, Coord w, Coord h);
    Rect(
const Point& origin, const Point& extent);

    Coord Width() 
const;
    
void Width(Coord);
    Coord Height() 
const;
    
void Height(Coord);
    Coord Left() 
const;
    
void Left(Coord);
    Coord Bottom() 
const;
    
void Bottom(Coord);

    Point
& Origin() const;
    
void Origin(const Point&);
    Point
& Extent() const;
    
void Extent(const Point&);

    
void MoveTo(const Point&);
    
void MoveBy(const Point&);

    
bool IsEmpty() const;
    
bool Contains(const Point&const;
}
;

#endif // !defined(AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_)






// BasicClass.cpp: implementation of the BasicClass class.
//
//////////////////////////////////////////////////////////////////////

#include 
"stdafx.h"
#include 
"BasicClass.h"

Point::Point(Coord x 
/* = 0.0 */,Coord y /* = 0.0 */)
{
    m_x 
= x;
    m_y 
= y;
}


Coord Point::X() 
const
{
    
return m_x;
}


Coord Point::Y() 
const
{
    
return m_y;
}


void Point::X(Coord x)
{
    m_x 
= x;
}


void Point::Y(Coord y)
{
    m_y 
= y;
}


Point Point::
operator +(const Point& p)
{
    
return Point(m_x+p.m_x, m_y + p.m_y);
}


Point Point::
operator -(const Point& p)
{
    
return Point(m_x-p.m_x, m_y-p.m_y);
}


Point Point::
operator *(const Point& p)
{
    
return Point(m_x* p.m_x, m_y* p.m_y);
}


Point Point::
operator /(const Point& p)
{
    
return Point(m_x/p.m_x, m_y/p.m_y);
}



void Point::operator +=(const Point& p)
{
    m_x 
+= p.m_x;
    m_y 
+= p.m_y;
}

//CPoint
void Point::operator -=(const Point& p)
{
    m_x 
-= p.m_x;
    m_y 
-= p.m_y;
}


void Point::operator *=(const Point& p)
{
    m_x 
*= p.m_x;
    m_y 
*= p.m_y;
}


void Point::operator /=(const Point& p)
{
    m_x 
/= p.m_x;
    m_y 
/= p.m_y;
}


bool Point::operator==(const Point& p)
{
    
return (m_x == p.m_x && m_y == p.m_y);
}


bool Point::operator!=(const Point& p)
{
    
return !(*this== p);
}


ostream
& operator<<(ostream& cout,const Point& p)
{
    cout
<<p.m_x<<","<<p.m_y;
    
return cout;
}


istream
& operator>>(istream& in, Point& p)
{
    
in>>p.m_x>>p.m_y;
    
return in;
}



 

抱歉!评论已关闭.