C语言简单通讯录模板
头文件:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
typedef struct NODE{
int bh;
char *name;
char *phone;
struct NODE *pNext;
}
Node;
typedef struct PAGE
{
int currentPage;
int totalPage;
int onePageItem;
int totalItem;
} Page;
int Getbh();
char *GetPhone();
char *GetName();
Node* GetNode();
void InitInfo(Node**top,Node** end,int nLength);
void AddNode(Node** ppTop,Node** ppEnd,Node* node);
void InsertNode(Node** ppTop,Node** ppEnd,int bh,Node* node);
void DelNode(Node** ppTop,Node** ppEnd,int bh);
Page* GetPage(Node* top,int onePageItem);
Page* InitPage(Node* top,int onePageItem);
void ShowMenu(Page* page);
void Show(Node* top,Page* page);
char GetKey();
int g_menu_type = 0;
char g_key;
void LookContacts(Node* top);
void OperatePage(Node* top,Page* page);
Node* GetNodeIn();
char* Getstring();
void FindContacts(Node* top);
void DelContacts(Node** top,Node** end);
void UpContacts(Node* top);
主代码:
#include"m.h"
int main()
{
Node *top = NULL;
Node *end = NULL;
char key;
InitInfo(&top,&end,120);
while(1)
{
printf("1.查看通讯录\n");
printf("2.添加联系人\n");
printf("3.查询联系人\n");
printf("4.删除联系人\n");
printf("5.修改联系人\n");
printf("6.退出\n");
key = GetKey();
switch(key)
{
case '1':
g_menu_type = 1;
LookContacts(top);
break;
case '2':
AddNode(&top,&end,GetNodeIn());
break;
case '3':
g_menu_type = 3;
FindContacts(top);
break;
case '4':
g_menu_type = 4;
DelContacts(&top,&end);
break;
case '5':
g_menu_type = 5;
UpContacts(top);
break;
case '6':
return 0;
break;
}
}
return 0;
}
int Getbh()
{
static int a=0;
a++;
return a;
}
char *GetPhone()
{
char *phone =(char*)malloc(12);
char c[2];
int i;
switch (rand()%4)
{
case 0:
strcpy_s(phone,12,"131");
break;
case 1:
strcpy_s(phone,12,"132");
break;
case 2:
strcpy_s(phone,12,"133");
break;
case 3:strcpy_s(phone,12,"131");
break;
}
for(i=0;i<8;i++)
{
itoa (rand()%10,c,10);
strcat_s(phone,12,c);
}
return phone;
}
char *GetName()
{
char *name=(char*)malloc(9);
int i;
for(i=0;i<8;i++)
{
name[i]=rand()%26+97;
}
name[8]=0;
return name;
}
Node* GetNode()
{
Node *node=(Node*)malloc(sizeof(Node));
node->bh=Getbh();
node->name=GetName();
node->phone=GetPhone();
node->pNext=NULL;
return node;
}
void InitInfo(Node**top,Node** end,int nLength)
{
int i;
for(i=0;i<nLength;i++)
{
AddNode(top,end,GetNode());
}
}
void AddNode(Node** ppTop,Node**ppEnd,Node* node)
{
if(*ppTop==NULL)
{
*ppTop=node;
}else
(*ppEnd)->pNext=node;
*ppEnd=node;
}
void InsertNode(Node** ppTop,Node** ppEnd,int bh,Node* node)
{
Node* bj=*ppTop;
if(bh==(*ppTop)->bh)
{
node->pNext=*ppTop;
*ppTop=node;
return;
}
while(bj->pNext!=NULL)
{
if( bj->pNext->bh==bh)
{
node->pNext=bj->pNext;
bj->pNext=node;
return;
}
bj=bj->pNext;
}
(*ppEnd)->pNext=node;
*ppEnd=node;
}
void DelNode(Node** ppTop,Node** ppEnd,int bh)
{
Node* pDel = NULL;
Node* bj = *ppTop;
//头删除
if((*ppTop)->bh == bh)
{
pDel = *ppTop;
*ppTop = (*ppTop)->pNext;
free(pDel);
return ;
}
//中间删除
while(bj->pNext)
{
if(bj->pNext->bh == bh)
{
//先让pDel指向要删除的节点
pDel = bj->pNext;
bj->pNext = bj->pNext->pNext;
free(pDel);
if(bj->pNext == NULL)
{
*ppEnd = bj;
}
return ;
}
bj = bj->pNext;
}
}
Page* GetPage(Node* top,int onePageItem)
{
Page *page = (Page*)malloc(sizeof(Page));
page->onePageItem = onePageItem;
page->currentPage = 0;
page->totalItem = 0;
while(top!=NULL)
{
page->totalItem++;
top = top->pNext;
}
page->totalPage = page->totalItem%page->onePageItem == 0
? page->totalItem/page->onePageItem : page->totalItem/page->onePageItem+1 ;
return page;
}
void ShowMenu(Page* page)
{
switch(g_menu_type)
{
case 1:
printf("共%d条 共%d页 当前第%d页 w上一页,s下一页 b返回主菜单\n",page->totalItem,page->totalPage,page->currentPage);
break;
case 3:
printf("共%d条 共%d页 当前第%d页 w上一页,s下一页 c重新查询 b返回主菜单\n",page->totalItem,page->totalPage,page->currentPage);
break;
case 4:
printf("共%d条 共%d页 当前第%d页 w上一页,s下一页 c重新查询 d删除信息 b返回主菜单\n",page->totalItem,page->totalPage,page->currentPage);
break;
}
}
void Show(Node* top,Page* page)
{
int begin=(page->currentPage-1)*page->onePageItem+1;
int end =page->currentPage*page->onePageItem;
int count=0;
while(top)
{
count++;
if(count>=begin&&count<=end)
printf("%d %s %s\n",top->bh,top->name,top->phone);
top = top->pNext;
}
}
char GetKey()
{
char c;
char v=-1;
int a = -1;
while((c =getchar()) != '\n' || a == -1)
{
a = 1;
v = c;
}
return v;
}
Page* InitPage(Node* top,int onePageItem)
{
Page* page = (Page*)malloc(sizeof(Page));
page->currentPage = 0;
page->onePageItem = onePageItem;
page->totalItem = 0;
while(top)
{
page->totalItem++;
top = top->pNext;
}
page->totalPage = page->totalItem%page->onePageItem == 0? page->totalItem/page->onePageItem:page->totalItem/page->onePageItem+1;
return page;
}
void LookContacts(Node* top)
{
Page* page = InitPage(top,10);
OperatePage(top,page);
}
void OperatePage(Node* top,Page* page)
{
char key = 's';
int d;
while( key != 'b')
{
switch(key)
{
case 'w'://上一页
if(page->currentPage == 1)
{
printf("已经是第一页了\n");
}
else
{
page->currentPage--;
Show(top,page);
ShowMenu(page);
}
break;
case 's'://下一页
if(page->currentPage == page->totalPage)
{
printf("已经是最后一页了\n");
}
else
{
page->currentPage++;
Show(top,page);
ShowMenu(page);
}
break;
case 'c': //重新查询
return;
case 'd': //删除
return;
default:
printf("按错了,再来一次\n");
}
g_key = key = GetKey();
}
}
Node* GetNodeIn()
{
Node* node = (Node*)malloc(sizeof(Node));
node->bh = Getbh();
printf("请输入名字:\n");
node->name = Getstring();
printf("请输入电话号码:\n");
node->phone = Getstring();
node->pNext = NULL;
return node;
}
char* Getstring()
{
int size = 5;
char* str = (char*)malloc(size);
char c;
int count = 0;
char* jstr = str;
char* newstr = NULL;
while((c=getchar()) != '\n')
{
//1.取字符 放到申请的空间里
*str = c;
str++;
count++;
//2.判断,如果空间不够了,申请更大的空间
if(size == count+1 )
{
//把旧空间存的字符 变成字符串
*str = 0;
size += 5;
//3.把旧空间的字符串拷贝到新的空间
newstr = (char*)malloc(size);
strcpy(newstr,jstr);
//4.把旧的空间释放掉
free(jstr);
jstr = newstr;
str = newstr + count;
}
}
*str = 0;
return jstr;
};
void FindContacts(Node* top)
{
//1.输入关键
Node* node;
char* keyword = NULL;
Node* newtop = NULL;
Node* newend = NULL;
Node* bj = top;
while(1)
{
top = bj;
newtop = NULL;
newend = NULL;
while(1)
{
//输入关键字
printf("请输入要查询的关键字:\n");
keyword = Getstring();
//a建确定 其他件重新输入
printf("按a确定,其他键重新输入\n");
if(GetKey() == 'a')
{
break;
}
}
//2.根据关键字 查找链表中的节点
while(top)
{
if(strncmp(keyword,top->name,strlen(keyword)) == 0
|| strncmp(keyword,top->phone,strlen(keyword)) == 0)
{
//如果找到了,申请新的节点,把节点放到新的链表中
node = (Node*)malloc(sizeof(Node));
node->bh = top->bh;
node->name = top->name;
node->phone = top->phone;
node->pNext = NULL;
AddNode(&newtop,&newend,node);
}
top = top->pNext;
}
//3.找到 或者没找到;如果找到了,对新的链表进行分页显示
if(newtop)
{
LookContacts(newtop);
}
else
{
printf("没找到\n");
}
if(g_key == 'b'|| g_key == 'd')
{
return ;
}
}
}
void DelContacts(Node** top,Node** end)
{
//1.调用查询
int bh;
while(1)
{
FindContacts(*top);
if(g_key == 'b')
{
return ;
}
//2.删除信息
printf("请输入要删除的编号:\n");
bh = atoi(Getstring());
DelNode(top,end,bh);
}
//3.y继续删除
printf("y继续删除,其他键返回\n");
if(GetKey() != 'y')
{
return ;
}
}
void UpContacts(Node* top)
{
int bh;
Node* bj = top;
while(1)
{
FindContacts(top);
if(g_key == 'b')
{
return ;
}
printf("请输入要修改的编号\n");
bh = atoi(Getstring());
bj = top;
while(bj)
{
if(bj->bh == bh)
{
printf("请输入新的名字:\n");
bj->name = Getstring();
printf("请输入新的电话号码:\n");
bj->phone = Getstring();
break;
}
bj = bj->pNext;
}
if(bj == NULL)
{
printf("没有此编号\n");
}
printf("y继续修改,其他键返回\n");
if(g_key == 'y')
{
}
else
{
return ;
}
}
}