📄 三电话号码查询系统.cpp
字号:
// 三电话号码查询系统.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define max 11
typedef struct node
{
int phone;
char *name;
char *add;
struct node*next;
}tnode;
typedef struct pnode
{
tnode *first;
}lnode;
typedef lnode link[max];
tnode*creat() //将所有用户的记录放入单链表中以待处理
{
tnode*h,*p,*q;
int x;
char *ch1,*ch2;
ch1=(char*)malloc(20*sizeof(char));
ch2=(char*)malloc(20*sizeof(char));
h=(tnode*)malloc(sizeof(tnode));
h->next=NULL;
q=h;
printf("请输入所有用户的记录:\n");
scanf("%d%s%s",&x,ch1,ch2);
while(x!=0)
{
p=(tnode*)malloc(sizeof(tnode));
p->phone=x;
p->name=ch1;
p->add=ch2;
p->next=NULL;
q->next=p;
q=p;
ch1=(char*)malloc(20*sizeof(char));
ch2=(char*)malloc(20*sizeof(char));
scanf("%d%s%s",&x,ch1,ch2);
}
return(h);
}
void telephone(tnode*h,link a) //以电话号码为关键字建立散列表
{
int i,y;
tnode *p,*t,*q;
for(i=0;i<max;i++)
a[i].first=NULL;
p=h->next;
while(p!=NULL)
{
q=p;
p=p->next;
y=q->phone%11;
t=a[y].first; //数组中各个单链表以头插法建立
a[y].first=q;
q->next=t;
}
}
void names(tnode*h,link a) //以用户名为关键字建立散列表
{
tnode*p,*q,*t;
int i,y;
for(i=0;i<max;i++)
a[i].first=NULL;
p=h->next;
while(p!=NULL)
{
q=p;
p=p->next;
y=strlen(q->name)%11;
t=a[y].first;
a[y].first=q;
q->next=t;
}
}
void fun(link a,int number) //查找并显示给定电话号码的记录
{
int y;
tnode *p;
y=number%11;
p=a[y].first;
if(p!=NULL)
while(p!=NULL)
{
if(number==p->phone)
{
printf("该用户的记录为:\n");
printf("姓名:%s 地址:%s 电话:%d\n",p->name,p->add,p->phone);
}
else
printf("不存在给定的电话号码!\n");
p=p->next;
}
else
printf("不存在给定的电话号码!\n");
}
void chaxun(link a,char *ch) //查询并显示给定用户名的记录
{
int y;
tnode *p;
y=strlen(ch)%11;
p=a[y].first;
while(p!=NULL)
{
if(strcmp(ch,p->name)==0)
{
printf("该用户的记录为:\n");
printf("姓名:%s 地址:%s 电话:%d\n",p->name,p->add,p->phone);
}
p=p->next;
}
}
void fum(link a,char *ch)
{
int i;
tnode *p;
for(i=0;i<11;i++)
{
p=a[i].first;
while(p!=NULL)
{
if(strcmp(ch,p->name)==0)
{
printf("该用户的记录为:\n");
printf("姓名:%s 地址:%s 电话:%d\n",p->name,p->add,p->phone);
}
p=p->next;
}
}
}
void show1()
{
printf("*********************************\n");
printf(" 请选择查询的条件 :\n");
printf(" 1.按电话号码查询\n");
printf(" 2.按用户姓名查询\n");
printf(" 3.查询结束\n");
printf("*********************************\n");
printf("请输入您选择的查询条件的代码:\n");
}
void show2(link a,link b)
{
int t,number;
char *ch;
scanf("%d",&t);
if(t==1)
{
printf("请输入要查询的电话号码:\n");
scanf("%d",&number);
fun(a,number);
show1();
show2(a,b);
}
else if(t==2)
{
ch=(char*)malloc(20*sizeof(char));
printf("请输入要查询的用户姓名:\n");
scanf("%s",ch);
fum(a,ch);
show1();
show2(a,b);
}
else if(t==3)
printf("查询结束!\n");
else
{
printf("输入代码错误:\n");
show1();
show2(a,b);
}
}
void putout(tnode*h)
{
tnode*p;
p=h->next;
while(p!=NULL)
{
printf("姓名:%s 地址:%s 电话:%d\n",p->name,p->add,p->phone);
p=p->next;
}
}
void main()
{
link a,b;
tnode*h;
printf("把所有用户的记录存放入单链表中:\n");
h=creat();
putout(h);
telephone(h,a); //以电话号码为主序建立散列表
names(h,b); //以用户姓名为主序建立散列表
show1();
show2(a,b);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -