?? 三電話號碼查詢系統.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 + -