CSDN博客

img solotony

电话薄程序的雏型

发表于2004/7/17 15:06:00  1029人阅读

include<string>
#include<iomanip>
#include<cassert>

using namespace std;

class PhoneBookEntry
{
public:
    typedef unsigned long  PhoneNumber;
    typedef char const*    NameString;

    PhoneBookEntry(NameString person, PhoneNumber telephone);
    ~PhoneBookEntry(void){}

    //
    //bit-wise copy is fine
    //
    //PhoneBookEntry(PhoneBookEntry const&);
    //PhoneBookEntry operator=(PhoneBookEntry const&);

    void SetName(NameString name);
    NameString const GetName(void) const;

    void SetPhoneNumber(PhoneNumber telephone);
    PhoneNumber const GetPhoneNumber(void) const;

    friend ostream& operator << ( ostream &out, PhoneBookEntry const &entry );
    friend bool operator ==( PhoneBookEntry const& lhs, PhoneBookEntry const& rhs);

private:
    static int const MAX_NAME_LEN = 60;

    PhoneNumber mPhoneNumber;
    char mPersonName[MAX_NAME_LEN];
};

PhoneBookEntry::PhoneBookEntry(NameString person, PhoneNumber telephone)
    : mPhoneNumber(telephone)
{
    strncpy(mPersonName, person, MAX_NAME_LEN);
}

void PhoneBookEntry::SetName(NameString name)
{
    strncpy(mPersonName, name,  MAX_NAME_LEN);
}

PhoneBookEntry::NameString const PhoneBookEntry::GetName(void) const
{
    return mPersonName;
}

void PhoneBookEntry::SetPhoneNumber(PhoneNumber telephone)
{
    mPhoneNumber = telephone;
}

PhoneBookEntry::PhoneNumber const PhoneBookEntry::GetPhoneNumber(void) const
{
    return  mPhoneNumber;
}

bool operator ==( PhoneBookEntry const& lhs, PhoneBookEntry const& rhs)
{
    return ( 0 == strcmp(lhs.mPersonName,rhs.mPersonName)
        && lhs.mPhoneNumber == rhs.mPhoneNumber);
}

ostream & operator<<( ostream &out, PhoneBookEntry const& entry )
{
    out << "姓名:"     << entry.mPersonName << '/t'
        << "电话号码:" <<entry.mPhoneNumber;

    return out;
}


template <class T>
class List
{
public:
    List();
    ~List();
    void Add(T const& );

    //T * Namefind( char *name );
    //T * Telfind( unsigned int tl );
    //void PrintList();

protected:
    struct Node
    {
        Node *next;
        T value;

        Node(Node* aNode, T aValue)
            : next(aNode), value(aValue)
        {
        }
        virtual ~Node(){}
    };
    Node *mFirst;

    virtual Node* Create(Node* next, T aValue);
    virtual void Delete(Node* node);
};

template <class T>
List<T>::List() : mFirst(0)
{
}

template <class T>
List<T>::~List()
{
    Node * p = mFirst;
    while( mFirst )
    {
        mFirst = mFirst->next;
        Delete(p);
        p = mFirst;
    }
}


template <class T>
void List<T>::Add(T const& value)
{
    mFirst = Create(mFirst, value);
}

template <class T>
typename List<T>::Node* List<T>::Create(Node* next, T aValue)
{
    return new Node(next, aValue);
}

template <class T>
void List<T>::Delete(Node* node)
{
    delete node;
}


class PhoneBook : public List<PhoneBookEntry *>
{
public:
    typedef PhoneBookEntry::PhoneNumber PhoneNumber;
    typedef PhoneBookEntry::NameString  NameString;

    PhoneBookEntry* const FindByName(NameString person) const;
    PhoneBookEntry* const FindByPhoneNumber(PhoneNumber telephone) const;

    friend ostream & operator<<( ostream &out, PhoneBook const& entry);

};

PhoneBookEntry* const PhoneBook::FindByName(NameString person) const
{
    Node * p = mFirst;
    while( p )
    {
        if( 0 == strcmp(person, p->value->GetName()))
        {
            return p->value;
        }
        p = p->next;
    }
    return 0;
}

PhoneBookEntry* const PhoneBook::FindByPhoneNumber(PhoneNumber telephone) const
{
    Node * p = mFirst;
    while( p )
    {
        if( telephone == p->value->GetPhoneNumber())
        {
            return p->value;
        }
        p = p->next;
    }
    return 0;
}

ostream & operator<<( ostream &out, PhoneBook const& entry)
{
    PhoneBook::Node * p = entry.mFirst;
    out <<"---PhoneBook Begin--" << endl;

    while(p)
    {
        cout << *(p->value) <<endl;
        p = p->next;
    }
    out <<"---PhoneBook end---" <<endl;
    return out;
}

void test1()
{
    cout<<endl<<"!!!!!!!test 1 !!!!!!!"<<endl;
    PhoneBook aPhoneBook;

    cout << aPhoneBook;

    assert( 0 == aPhoneBook.FindByName("杨文"));
    assert( 0 == aPhoneBook.FindByPhoneNumber(85424161));
}

void test2()
{
    cout<<endl<<"!!!!!!!test 2 !!!!!!!"<<endl;
    PhoneBook aPhoneBook;

    PhoneBookEntry cc( "杨文", 85424161 );
    aPhoneBook.Add(&cc);
    cout << aPhoneBook;

    assert( cc == *aPhoneBook.FindByName("杨文"));

    assert( &cc == aPhoneBook.FindByName("杨文"));
    cout << *aPhoneBook.FindByPhoneNumber(85424161)<<endl;
}

void test3()
{
    cout<<endl<<"!!!!!!!test 3 !!!!!!!"<<endl;
    PhoneBook aPhoneBook;

    PhoneBookEntry a1 = PhoneBookEntry( "杨文", 85424161 );
    PhoneBookEntry a2 = PhoneBookEntry( "张三", 85424161 );
    PhoneBookEntry a3 = PhoneBookEntry( "李四", 85424161 );

    aPhoneBook.Add(&a1);
    aPhoneBook.Add(&a2);
    aPhoneBook.Add(&a3);

    cout << aPhoneBook;

    assert( a3 == *aPhoneBook.FindByName("李四"));
    assert( &a3 == aPhoneBook.FindByName("李四"));

    cout << *aPhoneBook.FindByPhoneNumber(85424161)<<endl;
}

void main()
{
    test1();
    test2();
    test3();
}

功能不完整.

0 0

相关博文

我的热门文章

img
取 消
img