📄 main.cpp
字号:
#include "StdAfx.h"
#include "Main.h"
int Book::GetCount(void)
{
return count;
}
/*是否为空*/
bool Book::IsEmpty(void)
{
return count == 0;
}
/*是否为尾*/
bool Book::IsEnd(void)
{
return curcard == last;
}
/*定位第一页*/
bool Book::ToBegin(void)
{
if(IsEmpty() || curcard == head)
return false;
curcard = head;
return true;
}
/*定位最后页*/
bool Book::ToEnd(void)
{
if(IsEmpty() || curcard == last)
return false;
curcard = last;
return true;
}
/*上一页*/
bool Book::Prev(void)
{
if(IsEmpty() || curcard == head)
return false;
if( curcard != head)
{
curcard = curcard->prev;
}
return true;
}
/*下一页*/
bool Book::Next(void)
{
if(IsEmpty() || curcard == last)
return false;
if(curcard != last)
{
curcard = curcard->next;
}
return true;
}
void Book::Show(void)
{
if(IsEmpty())
{
printf("No record\n");
return ;
}
printf("%s %s %s %s\n", curcard->person.name,
curcard->person.address,
curcard->person.phone,
curcard->person.postcode);
}
/*功能实现*/
/*添加人员*/
bool Book::NewPerson(Card *pCard)
{
assert(pCard != NULL);
if(!IsEmpty())
{
pCard->next = last->next;
pCard->prev = last;
last->next = pCard;
last = pCard;
}
else
{
head = pCard;
last = pCard;
}
curcard = pCard;
++ count;
return true;
}
/*插入人员*/
bool Book::InsertPerson(Card *pCard)
{
assert(pCard != NULL);
if(!IsEmpty())
{
pCard->next = curcard->next;
pCard->prev = curcard;
if(curcard != last)
{
curcard->next->prev = pCard;
}
else
{
last = pCard;
}
curcard->next = pCard;
}
else
{
head = pCard;
last = pCard;
}
curcard = pCard;
++ count;
return true;
}
/*删除人员*/
Card* Book::DeletePerson(void)
{
Card *pCard = NULL;
if(!IsEmpty())
{
pCard = curcard;
if( curcard != head && curcard != last)
{
curcard->prev->next = curcard->next;
curcard->next->prev = curcard->prev;
curcard = curcard->next;
}
else if( curcard != head && curcard == last)
{
curcard->prev->next = NULL;
curcard = curcard->prev;
last = curcard;
}
else if( curcard == head && curcard != last)
{
curcard->next->prev = NULL;
curcard = curcard->next;
head = curcard;
}
else if( curcard == head && curcard == last)
{
curcard = head = last = NULL;
}
-- count;
}
return pCard;
}
/*读入人员信息文件*/
bool Book::LoadFile(char *fn)
{
FILE *fp = NULL;
Card *pCard = NULL;
int iSize, i;
fp = fopen(fn, "r");
if(fp == NULL)
return false;
fscanf(fp, "%d", &iSize);
for(i = 0; i < iSize; i++)
{
pCard = (Card*)malloc(sizeof(Card));
pCard->next = pCard->prev = NULL;
fscanf(fp, "%s %s %s %s", pCard->person.name,
pCard->person.address, pCard->person.phone,
pCard->person.postcode);
NewPerson(pCard);
}
fclose(fp);
return true;
}
/*保存人员信息*/
bool Book::SaveFile(char *fn)
{
if(IsEmpty())
{
return false;
}
Card *pCard = curcard;
ToBegin();
FILE *fp = NULL;
Person *pPerson = NULL;
int i;
fp = fopen(fn, "w");
if(fp == NULL)
return false;
fprintf(fp, "%d ", count);
for(i = 0; i < count; i++)
{
pPerson = GetCurPerson();
if(*pPerson->name == '\0')
strcpy(pPerson->name, "*");
if(*pPerson->address == '\0')
strcpy(pPerson->address, "*");
if(*pPerson->phone == '\0')
strcpy(pPerson->phone, "*");
if(*pPerson->postcode == '\0')
strcpy(pPerson->postcode, "*");
fprintf(fp, "%s %s %s %s ", pPerson->name,
pPerson->address, pPerson->phone,
pPerson->postcode);
Next();
}
curcard = pCard;
fclose(fp);
return true;
}
/*退出系统*/
/*初始化数据*/
bool Book::InitData(void)
{
count = 0;
curcard = NULL;
head = last = NULL;
return true;
}
/*释放数据空间*/
bool Book::FiniData(void)
{
ToBegin();
while(!IsEmpty())
{
free (DeletePerson());
}
return true;
}
Person* Book::GetCurPerson(void)
{
return &curcard->person;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -