利用线性表编写简单的通讯录程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define MAX 100
#define TRUE 1
#define FALSE 0
#define Status int


typedef struct node
{
char name[12];
char addr[40];
char mphone[12];
char tel[14];
char sex;
unsigned char age;
}DataType


typedef struct
{
DataType Element[MAX];
int length;
}Slist;




char *menu_1[] = {
"***********************\n",
"1.增加记录\n",
"2.删除记录\n",
"3.修改记录\n",
"4.查询记录\n",
"0.退出\n",
"***********************\n"
};
char *menu_query1[] = {
"***********************\n",
"1.按姓名查询\n",
"2.按手机号码查询\n",
"3.全部输出\n",
"0.退出\n",
"***********************\n"

};

char *menu_query2[] = {
"***********************\n",
"1.按姓名修改\n",
"2.按手机号码修改\n",
"0.退出\n",
"***********************\n"
};

char *menu_delete1[] = {
"***********************\n",
"1.按姓名删除\n",
"2.按手机号码删除\n",
"0.退出\n",
"***********************\n"
};




char menu(char *str[],int len)
{
char sel;
int i;
for (i=0 ; i<len; i++)
{
printf("%s",str[i]);
}
printf("请输入您的选择:");
scanf(" %c",&sel);
    getchar();
return sel;
}


Status InitList(Slist *L)
{
L->length = 0;
return TRUE;
}


int ListLength(Slist L)
{
return L.length;
}


Status ListEmpty(Slist L)
{
if ( 0 == L.length)
{
return TRUE;
}
else
{
return FALSE;
}
}


Status ListInsert(Slist *L,int i,DataType e)
{
int j;
if ( MAX == L->length)
{
return FALSE;
}
if ( i<0 || i>L->length)
{
return FALSE;
}

L->Element[i] = e;
L->length++;
return TRUE;
}


Status input(DataType *e)
{
int age;
printf("请输入姓名:");
fgets(e->name,12,stdin);
printf("请输入地址:");
fgets(e->addr,40,stdin);
printf("请输入手机号:");
fgets(e->mphone,12,stdin);
printf("请输入电话号码:");
fgets(e->tel,14,stdin);
printf("请输入性别:");
scanf(" %c",&(e->sex));
printf("请输入年龄:");
scanf("%d",&age);
getchar();
e->age = (unsigned char)age;
return TRUE;
}


Status GetElem(Slist L,int i,DataType *e)
{
if ( i<0 || i>L.length)
{
return FALSE;
}
    *e = L.Element[i];
return TRUE;
}




Status output(DataType e)
{
char sex[3];
printf("姓名:%s",e.name);
printf("地址:%s",e.addr);
printf("手机号:%s",e.mphone);
printf("电话号码:%s",e.tel);
if ( '0' == e.sex )
{
strcpy(sex,"女");
}
else if ( '1' == e.sex)
{
strcpy(sex,"男");
}
else
{
strcpy(sex,"错");
}
printf("性别:%s\n",sex);
printf("年龄:%d\n",(int)e.age);
return TRUE;
}
Status ListDelete(Slist *L,int i,DataType *e)
{
int j;
if ( i<0 || i>L->length)
{
return FALSE;
}
    *e = L->Element[i]
L->length--;
return TRUE;
}
Status ClearList(Slist *L)
{
L->length = 0;
}
//flag=0 按姓名查询, flag=1 按手机号码查询
//str 传递查询的内容 flag=0 str就是要查询的姓名 flag=1 str就是要查询的手机号
int LocateList(Slist L, char *str, int flag)
{
int i;
switch (flag)
{
case 0:
{
for (i=0; i<L.length; i++)
{
if (!strcmp(str,L.Element[i].name)) return i;
}
return -1;
}
break;
case 1:
{
for (i=0; i<L.length; i++)
{
if (!strcmp(str,L.Element[i].mphone)) return i;
}
return -1;
}
break;
default:return -1;
}
}




Status query(Slist L)
{
char sel=1,name[12],mphone[12];
int i,j;
DataType e;
while('0' != sel)
{
sel = menu(menu_query1,6);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets(name,12,stdin);
j = LocateList(L,name,0);
if (-1 != j)
{
GetElem(L,j,&e);
output(e);
}
else
{
printf("查无此人\n");
}
} break;
case '2':
{
printf("请输入手机号:");
fgets(mphone,12,stdin);
j = LocateList(L,mphone,1);
if (-1 != j)
{
GetElem(L,j,&e);
output(e);
}
else
{
printf("查无此人\n");
}
} break;
case '3':
{
for(i=0; i<L.length; i++)
{
printf("第%d条记录\n",i+1);
output(L.Element[i]);
printf("\n");
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;

}

Status change(Slist * L)
{
char sel=1,name[12],mphone[12];
int i,j;
DataType e;
while('0' != sel)
{
sel = menu(menu_query2,6);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets(name,12,stdin);
j = LocateList(L,name,0);
if (-1 != j)
{

                                ListDelete(L,j,&e);

input(&e);

                                ListInsert(L,j,e);
}
else
{
printf("查无此人\n");
}
} break;
case '2':
{
printf("请输入手机号:");
fgets(mphone,12,stdin);
j = LocateList(L,mphone,1);
if (-1 != j)
{
GetElem(L,j,&e);
output(e);
}
else
{
printf("查无此人\n");
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}





Status delete(Slist *L)
{
char sel=1,name[12],mphone[12];
int i,j;
DataType e;
while('0' != sel)
{
sel = menu(menu_delete1,5);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets(name,12,stdin);
j = LocateList(*L,name,0);
if (-1 != j)
{
GetElem(*L,j,&e);
output(e);
ListDelete(L,j,&e);
printf("%s已删除\n",name);
}
else
{
printf("查无此人\n");
}
} break;
case '2':
{
printf("请输入手机号:");
fgets(mphone,12,stdin);
j = LocateList(*L,mphone,1);
if (-1 != j)
{
GetElem(*L,j,&e);
output(e);
ListDelete(L,j,&e);
printf("%s已删除\n",name);
}
else
{
printf("查无此人\n");
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}


int main(int argc,char *argv[])
{
Slist txl;
int k;
DataType e;
char sel;
InitList(&txl);
#if 0
printf("txl length = %d\n",ListLength(txl));
printf("txl is empty? = %d\n",ListEmpty(txl));
input(&e);
ListInsert(&txl,0,e);
printf("length=%d\n",ListLength(txl));
GetElem(txl,0,&e);
output(e);
ListDelete(&txl,0,&e);
output(e);
printf("length=%d\n",ListLength(txl));
    k = LocateList(txl,"feng",0);
printf("k=%d\n",k);
memset(&e,0,sizeof(DataType));
if( FALSE == GetElem(txl,k,&e))
{
printf("在位置%d处没有取到值\n",k);
}
output(e);
#endif
while (1)
{
sel = menu(menu_1,7);
switch (sel)
{
case '1':memset(&e,0,sizeof(DataType));input(&e);ListInsert(&txl,txl.length,e);break;
case '2':delete(&txl);break;
case '3':change(&txl);break;
case '4':query(txl);break;
case '0':exit(0);break;
default:printf("错误输入\n");break;
}
}
return 0;
}
发布了18 篇原创文章 · 获赞 5 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览