随笔 - 28  文章 - 4 评论 - 52 trackbacks - 0


/*
FileName:Array.h
 *Author:Lupin
 * Description:A simple array with iterator for int
 
*/

#ifndef ARRAY_H_
#define ARRAY_H_
#include
<stdexcept>


namespace DataStruct
{

class Array
{
    
class Iterator
    {
        
private:
            Array
* _array;
            
int _length;
            
int _current;
        
public:
            Iterator( Array
* array, int length );
            
            
//Get the next element
            const int Next();
            
//Reset the iterator
            void Reset();
    };
    
private:
    
int _length, _maxSize;
    
int* _list;
public:
    Array();
    Array( 
int maxSize );
    
virtual ~Array();
    
    
//Return the length of the array
    int Length() const;
    
//Return the Max Size of the array
    int MaxSize() const;
    
    
//If the array is full
    bool IsFull() const;
    
//If the array is empty
    bool IsEmpty() const;
    
    
//To append the @elem to the end of the array
    void Append( int elem );
    
//To Get the element at the position of @pos
    int Get( int pos ) const;
    
//To remove the element at the position of @pos
    void Remove( int pos );
    
//To find the position of the @elem in the array
    int Find( int elem );
    
};

}

#endif /*ARRAY_H_*/


  1 /*FileName:Array.cpp
  2  *Author:Lupin
  3  * Description:A simple array with iterator for int
  4  */
  5 #include "Array.h"
  6 
  7 namespace DataStruct
  8 {
  9 
 10 Array::Array()
 11 {
 12     _maxSize = 100;
 13     _list = new int100 ];
 14 }
 15 Array::Array( int maxSize )
 16 {
 17     _maxSize = maxSize;
 18     _list = new int[ _maxSize ];
 19 }
 20 
 21 Array::~Array()
 22 {
 23     delete[] _list;
 24 }
 25 
 26 int Array::Length() const
 27 {
 28     return _length;
 29 }
 30 
 31 int Array::MaxSize() const
 32 {
 33     return _maxSize;
 34 }
 35 
 36 bool Array::IsFull() const
 37 {
 38     return ( _length == _maxSize );
 39 }
 40 
 41 bool Array::IsEmpty() const
 42 {
 43     return ( _length == 0 );
 44 }
 45 
 46 void Array::Append( int elem )
 47 {
 48     if( IsFull() )
 49         throw range_error( "The array is full" );
 50     
 51     _list[ _length++ ] = elem;
 52 }
 53 
 54 int Array::Get( int pos ) const
 55 {
 56     if( pos < 0 || pos >= _length )
 57         throw out_of_range( "You can't get a element isn's exsits" );
 58     
 59     return _list[ pos ];
 60 }
 61 
 62 void Array::Remove( int pos )
 63 {
 64     if( pos < 0 || pos > _length )
 65         throw out_of_range( "No element at the position" );
 66         
 67     _length--;
 68     int i = pos;
 69     while( i < _length )
 70         _list[ i++ ] = _list[ i + 1 ];
 71 }
 72 
 73 int Array::Find( int elem )
 74 {
 75     int i = 0;
 76     while( i < _length )
 77     {
 78         if( _list[ i++ ] == elem )
 79             return i-1;
 80     }
 81     return -1;
 82 }
 83 
 84 Array::Iterator::Iterator( Array* array, int length )
 85 {
 86     _array = array;
 87     _length = length;
 88     _current = 0;
 89 }
 90 
 91 const int Array::Iterator::Next()
 92 {
 93     if( _current < _length  )
 94         return _array->Get( _current++ );
 95         
 96     return NULL;
 97 }
 98 
 99 void Array::Iterator::Reset()
100 {
101     _current = 0;
102 }
103 
104 }
105 

posted @ 2006-03-18 22:49 Lupin 阅读(438) 评论(0) 编辑
    Python支持数据类型:整数,浮点数,复数,字符串
    复数在这里虚数由一个 "j"后缀表示,也可以通过complex(real, img)来创建。实部和虚部可以分别访问。就像这样:
>>> c = complex(5,3)
>>> c.real
5.0
>>> c.imag
3.0
>>>
    字符串操作也有一些比较特别的地方。
1 >>> word = "abc"
2 >>> print word*3
3 abcabcabc
4 
还可以很方便的截取字符串
>>> sentence = "Python is good"
>>> sentence
[8]#取得下标为8的字符
's'
>>> sentence
[10:14]#取得下标为10到14组成的字符串
'good'
>>> sentence
[:6]#取得前六个字符组成的字符串
'Python'
支持Unicode字符串定义
>>> u'Hello\u0020Python'
u'Hello Python'
它还支持;链表和字典(相当于哈希表)
>>> list=['You','are','bad']#用中括号定义链表
>>> list
['You', 'are', 'bad']
>>> dict
={'You':'Bad','Me':'Good'}#用花括号定义字典
>>> dict
['You']#用中括号访问字典元素
'Bad'


posted @ 2006-03-18 22:41 Lupin 阅读(273) 评论(0) 编辑
 
       这两天在Linux下用Eclipse编数据结构,还有试用CPPUNIT,总是遇到一个错误,就是“undefined reference to [function name]”。找了很久,终于发现是在写makefile的疏忽所致。

出错的时候是这样的:
1 main:main.o Array.o
2     g++ main.o -o main
3     ./main
4 main.o:main.cpp Array.h
5     g++ -c main.cpp
6 Array.o: Array.cpp Array.h
7     g++ -c Array.cpp

后来才发现第二行编译的时候没有加上Array.o,把它加上就可以了。
1 main:main.o Array.o
2     g++ main.o Array.o -o main
3     ./main
4 main.o:main.cpp Array.h
5     g++ -c main.cpp
6 Array.o: Array.cpp Array.h
7     g++ -c Array.cpp

这个“undefined reference to ”一般是在找不到相应的库文件的时候产生的,没有加入Array.o也算是没有找到库文件的一种。有时可能需要在编译选项里加入"_lLIBNAME",来指定要加入的库文件。
posted @ 2006-03-18 21:40 Lupin 阅读(31686) 评论(3) 编辑