标签:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
typedef struct NODE
{
int id;
int age;
char *name;
char *phone;
struct NODE *pNext;
}List;
typedef struct
{
int count;
int item;
int CurrentPage;
int PageCount;
}Page;
char *GetString();
List *GetNode();
List *GetNodeIn(char *name,int age, char *phone);
int GetID();
void FAdd(List **,List **,List *);
void FDelete(int ,List **,List **);
void FInert(int ,List **,List **,List *);
void FClear(List **,List **);
void FUpdate(int ,List *);
void FPrint(List *);
typedef struct LINK
{
List *top;
List *end;
void (*fp_Add)(List **,List **,List *);
void (*fp_Delete)(int,List **,List **);
void (*fp_Clear)(List **,List **);
void (*fp_Insert)(int ,List **,List **,List *);
void (*fp_Update)(int ,List *);
}Link;
Link ListUtil={NULL,NULL,FAdd,FDelete,FClear,FInert,FUpdate};
char GetKey();
void Finit();
void PrintMenu(Page );
Page *Tongji(List *);
void FenYe(Page *,List *);
void ShowIn(int begin,int end,List *top);
void HomePage();
void ShowAllContacts(List *top);
void FQuery();
void Delete();
void Update();
//m.h
#include"m.h"
char g_menu_type=‘1‘;
char g_key=-1;
int main()
{
/* Page *page=NULL;*/
srand(time(0));
Finit();
while (1)
{
HomePage();
switch (GetKey())
{
case ‘1‘:
g_menu_type=‘1‘;
ShowAllContacts(ListUtil.top);
break;
case ‘2‘:
g_menu_type=‘2‘;
ListUtil.fp_Add(&ListUtil.top,&ListUtil.end,GetNode());
break;
case ‘3‘:
g_menu_type=‘3‘;
FQuery();
break;
case ‘4‘:
g_menu_type=‘4‘;
Delete();
break;
case ‘5‘:
g_menu_type=‘5‘;
Update();
break;
case ‘q‘:
return 0;
break;
default:
printf("输入错误请重新输入!\n");
break;
}
}
return 0;
}
int GetID()
{
static int id=1;
return id++;
}
char *GetString()
{
size_t size=50;
size_t count=0;
char *str=(char *)malloc(size);
char *jstr=str;
char *newStr=NULL;
char c;
while ((c=getchar())!=‘\n‘)
{
count++;
*str++=c;
if(count+1==size)
{
size+=5;
newStr=(char *)malloc(size);
*str=0;
strcpy_s(newStr,size,jstr);
free(str);
jstr=str=newStr;
str+=count;
}
}
*str=0;
return jstr;
}
List *GetNode()
{
List *node=(List *)malloc(sizeof(List));
node->id=GetID();
printf("请输入姓名:\n");
node->name=GetString();
printf("请输入年龄:\n");
node->age=atoi(GetString());
printf("请输入手机号:\n");
node->phone=GetString();
node->pNext=NULL;
return node;
}
List *GetNodeIn(char *name,int age, char *phone)
{
List *node=(List *)malloc(sizeof(List));
node->id=GetID();
node->name=name;
node->age=age;
node->phone=phone;
node->pNext=NULL;
return node;
}
void FAdd(List **pptop,List **ppend,List *node)
{
if(*pptop==NULL)
{
*pptop=node;
}
else
{
(*ppend)->pNext=node;
}
*ppend=node;
}
void FDelete(int bh,List **pptop,List **ppend)
{
List *top=*pptop;
List *del=NULL;
if(top->id==bh)
{
del=top;
*pptop=top->pNext;
free(del);
return ;
}
while (top->pNext!=NULL)
{
if(top->pNext->id==bh)
{
del=top->pNext;
top->pNext=top->pNext->pNext;
free(del);
if(top->pNext==NULL)
{
*ppend=top;
}
return ;
}
top=top->pNext;
}
}
void FInert(int bh,List **pptop,List **ppend,List *node)
{
List *top=*pptop;
if(top->id >=bh)
{
node->pNext=top;
*pptop=node;
return ;
}
while(top->pNext!=NULL)
{
if(top->pNext->id >=bh)
{
node->pNext=top->pNext;
top->pNext=node;
return ;
}
top=top->pNext;
}
FAdd(pptop,ppend,node);
}
void FClear(List **pptop,List **ppend)
{
List *del=NULL;
List *top=*pptop;
*pptop=NULL;
*ppend=NULL;
while (top!=NULL)
{
del=top;
top=top->pNext;
free(del);
}
}
void FUpdate(int bh,List *top)
{
char *name=NULL;
char *phone=NULL;
char *age=NULL;
if(top!=NULL)
{
while (top!=NULL)
{
if(bh==top->id)
{
printf("输入你要修改的姓名:");
name=GetString();
if(name!=NULL&&*name!=0)
{
top->name=name;
}
printf("输入你要修改的年龄:");
age=GetString();
if(age!=NULL&&*age!=0)
{
top->age=atoi(age);
}
printf("输入你要修改的电话:");
phone=GetString();
if(phone!=NULL&&*phone!=0)
{
top->phone=phone;
}
return ;
}
top=top->pNext;
}
}
}
char GetKey()
{
char *str=NULL;
char c;
while (str==NULL||*str==0)
{
str=GetString();
}
c=*str;
free(str);
return c;
}
void Finit()
{
int i,j;
char *name=NULL;
char *phone=NULL;
char *jname;
int age;
for(i=0;i<151;i++)
{
name=(char *)malloc(6);
phone=(char *)malloc(12);
jname=name;
for(j=0;j<5;j++)
{
*name++=rand()%25+97;
}
*name=0;
_itoa_s(rand()%999999999+10000000000,phone,12,10);
age=rand()%99;
ListUtil.fp_Add(&ListUtil.top,&ListUtil.end,GetNodeIn(jname,age,phone));
}
}
Page *Tongji(List *top)
{
Page *page=(Page *)malloc(sizeof(Page));
page->count=0;
page->CurrentPage=0;
page->PageCount=0;
page->item=10;
while (top!=NULL)
{
page->count++;
top=top->pNext;
}
page->PageCount=page->count%page->item==0 ? page->count/page->item :page->count/page->item+1;
return page;
}
void FenYe(Page *page,List *top)
{
char key =‘n‘;
int begin=0;
int end=0;
while(key==‘n‘||key==‘w‘)
{
if(key==‘n‘)
{
if(page->CurrentPage==page->PageCount)
{
printf("亲,这已经是最后一页了");
}
else
{
page->CurrentPage++;
begin=(page->CurrentPage-1)*page->item+1;
end=page->CurrentPage*page->item;
ShowIn(begin,end,top);
}
}
if(key==‘w‘)
{
if(page->CurrentPage==1)
{
printf("亲,这已经是第一页了");
}
else
{
page->CurrentPage--;
begin=(page->CurrentPage-1)*page->item+1;
end=page->CurrentPage*page->item;
ShowIn(begin,end,top);
}
}
PrintMenu(*page);
g_key=key=GetKey();
}
}
void PrintMenu(Page page)
{
switch (g_menu_type)
{
case ‘3‘:
printf("当前第(%d)页 (w)上一页 (n)下一页 (b)返回菜单 (c)重新查询 共(%d)页 共(%d)条\n",page.CurrentPage,page.PageCount,page.count);
break;
case ‘4‘:
printf("当前第(%d)页 (w)上一页 (n)下一页 (b)返回菜单(d)删除 共(%d)页 共(%d)条\n",page.CurrentPage,page.PageCount,page.count);
break;
case ‘5‘:
printf("当前第(%d)页 (w)上一页 (n)下一页 (b)返回菜单(u)修改 共(%d)页 共(%d)条\n",page.CurrentPage,page.PageCount,page.count);
break;
default:
printf("当前第(%d)页 (w)上一页 (n)下一页 (b)退出 共(%d)页 共(%d)条\n",page.CurrentPage,page.PageCount,page.count);
break;
}
}
void ShowIn(int begin,int end,List *top)
{
int count =0;
printf("id name age phone \n");
while(top!=NULL&&count<=end)
{
count++;
if(count>=begin&&count<=end)
{
printf("%d %s %d %s \n",top->id,top->name,top->age,top->phone);
}
top=top->pNext;
}
}
void ShowAllContacts(List *top)
{
Page *page=NULL;
page=Tongji(top);
FenYe(page,top);
}
void FQuery()
{
List *ptop=NULL;
List *pend=NULL;
List *node=NULL;
List *top=NULL;
char *word=NULL;
while (1)
{
top=ListUtil.top;//重新查询时,从头开始查
printf("输入查询关键字:\n");
word=GetString();
printf("a 确认查询 c 重新查询 b返回主菜单:\n");
switch(GetKey())
{
case ‘a‘:
while (top!=NULL)
{
if(0==strncmp(top->name,word,strlen(word))||0==strncmp(top->phone,word,strlen(word)))
{
node=(List *)malloc(sizeof(List));
node->id=top->id;
node->phone=top->phone;
node->name=top->name;
node->age=top->age;
node->pNext=NULL;
ListUtil.fp_Add(&ptop,&pend,node);
}
top=top->pNext;
}
ShowAllContacts(ptop);
ListUtil.fp_Clear(&ptop,&pend);
if(‘b‘==g_key||‘d‘==g_key||‘u‘==g_key)
{
return ;
}
break;
case ‘b‘:
return ;
break;
default:
break;
}
}
}
void Delete()
{
int bh=-1;
while(1)
{
FQuery();
if(g_key==‘d‘)
{
printf("输入你要删除的编号:");
bh=atoi(GetString());
ListUtil.fp_Delete(bh,&ListUtil.top,&ListUtil.end);
printf("y 继续删除 其他键返回主菜单\n");
if(‘y‘!=GetKey())
{
return ;
}
}
}
}
void Update()
{
int bh=1;
while(1)
{
FQuery();
if(g_key==‘u‘)
{
printf("输入你要修改的编号:");
bh=atoi(GetString());
ListUtil.fp_Update(bh,ListUtil.top);
printf("y 继续删除 其他键返回主菜单\n");
if(‘y‘!=GetKey())
{
return ;
}
}
}
}
void HomePage()
{
printf("1.查看通讯录\n");
printf("2.添加联系人\n");
printf("3.查询联系人\n");
printf("4.删除联系人\n");
printf("5.修改联系人\n");
printf("q.退出\n");
}
标签:
原文地址:http://blog.csdn.net/sunshine_505/article/details/51993918