#if !defined(AFX_ADDRESSBOOK_H__25D03C00_B5B2_45DF_AB39_71283D0C8A1E__INCLUDED_)
#define AFX_ADDRESSBOOK_H__25D03C00_B5B2_45DF_AB39_71283D0C8A1E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef struct tagContact{
int id;
CString Name;
CString TelNum;
int Sex;
}Contact_t;
typedef struct tagContactList{
Contact_t Cont;
struct tagContactList *Next;
}ContactList_t;
class CAddressBook
{
public:
CAddressBook();
virtual ~CAddressBook();
bool AddContact(Contact_t &Cont, int id = -1);
bool DelContact(int id);
bool ModifyContact(int id, Contact_t &Cont);
bool FindFirstContact();
bool FindNextContact(Contact_t &Cont);
private:
void LoadContact();
void SaveContact();
void DestoryAddressBook();
private:
ContactList_t * m_ContList;
ContactList_t * m_Finder;
int m_Total;
};
#endif // !defined(AFX_ADDRESSBOOK_H__25D03C00_B5B2_45DF_AB39_71283D0C8A1E__INCLUDED_)
#include "AddressBook.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAddressBook::CAddressBook()
{
m_ContList = NULL;
m_Total = 0;
LoadContact();
}
CAddressBook::~CAddressBook()
{
DestoryAddressBook();
}
//插入节点
bool CAddressBook::AddContact(Contact_t &Cont, int id)
{
ContactList_t *pCont;
//联系人是否已存在,已存在则返回false
pCont = m_ContList;
while(NULL != pCont)
{
if((pCont->Cont.Name == Cont.Name)||(pCont->Cont.TelNum == Cont.TelNum))
{
return false;
}
pCont = pCont->Next;
}
//生成节点,并将节点从表头插入
pCont = new ContactList_t;
pCont->Cont.Name = Cont.Name;
pCont->Cont.Sex = Cont.Sex;
pCont->Cont.TelNum = Cont.TelNum;
pCont->Next = NULL;
if(-1 == id)
{
pCont->Cont.id = m_Total;
++m_Total;
}
else
{
if(id > m_Total)
{
m_Total = id;
}
pCont->Cont.id = id;
}
if(NULL != m_ContList)
{
pCont->Next = m_ContList;
}
m_ContList = pCont;
return true;
}
//删除节点
bool CAddressBook::DelContact(int id)
{
ContactList_t ** pCont = &m_ContList;
ContactList_t *pCont1;
while(NULL != *pCont)
{
if((*pCont)->Cont.id == id)
{
pCont1 = *pCont;
(*pCont) = (*pCont)->Next;
delete pCont1;
return true;
}
pCont = &((*pCont)->Next);
}
return false;
}
//修改节点
bool CAddressBook::ModifyContact(int id, Contact_t &Cont)
{
ContactList_t *pCont;
pCont = m_ContList;
while(NULL != pCont)
{
if(pCont->Cont.id == id)
{
pCont->Cont.Name = Cont.Name;
pCont->Cont.Sex = Cont.Sex;
pCont->Cont.TelNum = Cont.TelNum;
return true;
}
pCont = pCont->Next;
}
return false;
}
//遍历链表
bool CAddressBook::FindFirstContact()
{
m_Finder = m_ContList;
if(NULL == m_Finder)
{
return false;
}
return true;
}
bool CAddressBook::FindNextContact(Contact_t &Cont)
{
Cont.id = m_Finder->Cont.id;
Cont.Name = m_Finder->Cont.Name;
Cont.Sex = m_Finder->Cont.Sex;
Cont.TelNum = m_Finder->Cont.TelNum;
if(NULL == (m_Finder = m_Finder->Next))
{
return false;
}
return true;
}
//从SD卡中读取数据到链表中
void CAddressBook::LoadContact()
{
FILE *fp;
int ntmp;
char strBuf[100];
Contact_t Cont;
if(NULL == (fp = fopen("c:\\tmp.txt", "r")))
{
return;
}
while(1)
{
if(NULL == fgets(strBuf, 100, fp))
{
break;
}
Cont.id = atoi(strBuf);
if(NULL == fgets(strBuf, 100, fp))
{
break;
}
Cont.Sex = atoi(strBuf);
if(NULL == fgets(strBuf, 100, fp))
{
break;
}
Cont.Name = strBuf;
if(NULL == fgets(strBuf, 100, fp))
{
break;
}
Cont.TelNum = strBuf;
AddContact(Cont, Cont.id);
}
fclose(fp);
}
//将链表中的数据存储到SD卡中
void CAddressBook::SaveContact()
{
FILE *fp;
//int ntmp;
//char strBuf[100];
//Contact_t Cont;
if(NULL == (fp = fopen("c:\\tmp.txt", "a+")))
{
return;
}
fclose(fp);
}
//销毁链表
void CAddressBook::DestoryAddressBook()
{
ContactList_t *pConList = m_ContList;
while(NULL != pConList)
{
m_ContList = pConList->Next;
delete pConList;
pConList = m_ContList;
}
m_ContList = NULL;
}