#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
int count=0;
typedef struct /*个人信息纪录节点*/
{
char name[10];
char tel[20];
char addr[50];
}personnode;
personnode paixu(personnode person[MAX])
{
int j,exchange,bound;
personnode key;
exchange=count;
while(exchange)
{
bound=exchange;
exchange=0;
for(j=0; j<bound; j++)
{
if(strcmp(person[j].name,person[j+1].name)>0)
{
key=person[j];
person[j]=person[j+1];
person[j+1]=key;
exchange=j;
}
}
}
printf("排序成功!/n");
return person[MAX];
}
personnode Create(personnode person[MAX]) /*创建电话号码查询系统*/
{
printf("/n请输入姓名,电话号码及住址(输入quit结束创建新数据库):");
count++;
while(count<=MAX)
{
scanf("%s",person[count].name);
if(strcmp(person[count].name,"quit")==0)break;
scanf("%s%s",person[count].tel,person[count].addr);
printf("%s-%s-%s/n",person[count].name,person[count].tel,person[count].addr);
count++;
printf("/n请输入姓名,电话号码及住址(输入quit结束创建新数据库):");
}
count--;
paixu(person);
printf("系统创建成功!/n");
return person[MAX];
}
int Search_Bin(personnode person[MAX],char key[])
{
int low=1, high=count,mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(key,person[mid-1].name)==0)
return mid;
else if(strcmp(person[mid-1].name,key)>0)
high=mid-1;
else
low=mid+1;
}
return 0;
}
void Search(personnode person[MAX]) /*查找信息*/
{
int i=0;
char namekey[10];
printf("/n请输入要查询的姓名: /n");
scanf("%s",&namekey);
i=Search_Bin(person,namekey);
if(i!=0)
{
printf("您所要查找的人的信息为:");
printf("%s-%s-%s",person[i-1].name,person[i-1].tel,person[i-1].addr);
}
else
printf("输入的姓名不存在!/n");
}
personnode Modify(personnode person[MAX]) /*修改个人信息*/
{
int i;
personnode personkey;
int Search_Bin(personnode person[MAX],char key[10]);
printf("/n请输入要修改人的信息: /n");
scanf("%s%s%s",&personkey.name,&personkey.tel,&personkey.addr);
i=Search_Bin(person,personkey.name);
if(i!=0)
{
printf("/n 所修改人的原信息: /n");
printf("%s-%s-%s",person[i-1].name,person[i-1].tel,person[i-1].addr);
person[i-1]=personkey;
printf("修改成功!");
}
else
printf("输入的姓名不存在!");
paixu(person);
return person[MAX];
}
personnode Deleterecord(personnode person[MAX]) /*删除个人信息*/
{
int i,j;
char ch;
char namekey[10];
int Search_Bin(personnode person[MAX],char key[10]);
printf("/n请输入要删除人的姓名: /n");
scanf("%s",&namekey);
i=Search_Bin(person,namekey);
if(i!=0)
{
printf("已查到,记录为:");
printf("/n%s-%s-%s/n",person[i-1].name,person[i-1].tel,person[i-1].addr);
printf("确实要删掉吗,'y'or'n'?/n");
getchar();
scanf("%c",&ch);
if(ch=='y')
{
for(j=i-1;j<=count;j++)
person[j]=person[j+1];
printf("删除成功!");
count--;
// printf("%d",count);
}
}
else
printf("输入的姓名不存在!");
paixu(person);
return person[MAX];
}
void Output(personnode person[MAX]) /*浏览整个查询系统*/
{
int i;
printf("/n-------------------------------------/n");
printf(" 电话号码查询系统 /n");
printf("姓名-电话号码-住址/n");
printf("%d/n",count);
for(i=0;i<count;i++)
{
printf("%s-%s-%s/n",person[i].name,person[i].tel,person[i].addr);
}
printf("-----------------------------------/n");
}
personnode Append(personnode person[MAX]) /*添加新信息*/
{
int i=count;
personnode key;
printf("/n请输入要添加的姓名,电话和住址: /n");
scanf("%s%s%s",&key.name,&key.tel,&key.addr);
while(strcmp(person[i].name,key.name)>0)
{
person[i+1]=person[i];
i--;
}
person[i+1]=key;
printf("%s-%s-%s/n",person[i+1].name,person[i+1].tel,person[i+1].addr);
printf("添加成功!");
count=count+1;
// printf("%d",count);
paixu(person);
return person[MAX];
}
int menu() /*菜单打印函数*/
{
int i;
printf("---------电话号码查询系统-----------/n/n");
printf(" | 1: 创建新数据库 | /n");
printf(" | 2: 添加新号码 | /n");
printf(" | 3: 查找 | /n");
printf(" | 4: 修改 | /n");
printf(" | 5: 删除 | /n");
printf(" | 6: 浏览 | /n");
printf(" | 7: 退出 ! | /n");
printf("------------------------------------/n");
printf("/n 请输入你要执行的操作: /n");
scanf("%d",&i); /* 接收用户的命令 */
return i;
}
void main()
{
personnode person[MAX];
int quit=0;
while(!quit)
{
switch(menu())
{
case 1: Create(person);break;
case 2: Append(person);break;
case 3: Search(person);break;
case 4: Modify(person);break;
case 5: Deleterecord(person);break;
case 6: Output(person);break;
case 7: quit=1;break;
default:printf("输入错误!/n");break;
}
}
}