📄 head.cpp
字号:
#include <iostream>
#include <stdio.h>
#include "string.h"
#include "fstream"
#define NULL 0
unsigned int key;
unsigned int key2;
int *p;
char num[11];
char name[8];
int jixu();
void zaiha();
int m,n;
int ai[20];
//*****************************************************************************
struct node //建节点
{
char name[8],address[20];
char num[11];
node * next;
};
//*****************************************************************************
typedef node *pnode;
typedef node *mingzi;
node **phone;
node **nam;
node *a;
using namespace std; //使用名称空间
//*****************************************************************************
void hash(char num[11]) //哈希函数
{
int i = 3;
key=(int)num[2];
while(num[i]!=NULL)
{
key=key+(int)num[i];
i++;
}
key=key%20;
}
//*****************************************************************************
void hash2(char name[8]) //哈希函数
{
int i = 1;
key2=(int)name[0];
while(name[i]!=NULL)
{
key2=key2+(int)name[i];
i++;
}
key2=key2%20;
}
//*****************************************************************************
void zaiha()
{int i=1;
key=(key+m)%20;
m++;
key2=(key2+n)%20;
n++;
}
//*****************************************************************************
node* input() //输入节点
{
node *temp;
temp = new node;
temp->next=NULL;
cout<<"输入姓名:"<<endl;
cin>>temp->name;
cout<<"输入地址:"<<endl;
cin>>temp->address;
cout<<"输入电话:"<<endl;
cin>>temp->num;
return temp;
}
//*****************************************************************************
int apend() //添加节点
{
node *newphone;
node *newname;
newphone=input(); //将新输入的数值送给newphone 和newname中
newname=newphone;
newphone->next=NULL;
newname->next=NULL;
//***************************************************
hash(newphone->num);
hash2(newname->name);
while(phone[key]->next!=NULL)
zaiha();
newphone->next = phone[key]->next;
phone[key]->next=newphone;
newname->next = nam[key2]->next;
nam[key2]->next=newname;
return 0;
}
//*****************************************************************************
void create() //新建节点
{
int i;
phone=new pnode[20];
for(i=0;i<20;i++)
{
phone[i]=new node;
phone[i]->next=NULL;
}
}
//*****************************************************************************
void create2() //新建节点
{
int i;
nam=new mingzi[20];
for(i=0;i<20;i++)
{
nam[i]=new node;
nam[i]->next=NULL;
}
}
//*****************************************************************************
void list2() //显示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=nam[i]->next;
p=phone[i]->next;
while(p)
{
cout<<"姓名:"<<p->name<<" "<<"地址:" <<p->address<<" "<<"电话号码:"<<p->num<<endl;
p=p->next;
}
}
}
//*****************************************************************************
void find(char num[11]) //查找用户信息
{
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num)==0)
break;
q=q->next;
}
if(q)
cout<<"姓名:"<<q->name<<" "<<"地址:" <<q->address<<" "<<"电话号码:"<<q->num<<endl;
else cout<<"无此记录"<<endl;
}
void find2(char name[8]) //查找用户信息
{
hash2(name);
node *q=nam[key2]->next;
while(q!= NULL)
{
if(strcmp(name,q->name)==0)
break;
q=q->next;
}
if(q)
cout<<"姓名:"<<q->name<<" "<<"地址:" <<q->address<<" "<<"电话号码:"<<q->num<<endl;
else cout<<"无此记录"<<endl;
}
//*****************************************************************************
del(char num[11])
//删除用户信息
{
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num)==0)
break;
q=q->next;
}
if(q)
{
phone[key]->next=q->next;
q=q->next;
}
else cout<<"无此记录"<<endl;
return 0;
}
//*****************************************************************************
void save() //保存用户信息
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
fstream iiout("out.txt", ios::out);
iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;
p=p->next;
}
}
}
void menu() //菜单
{
cout<<endl;
cout<<endl;
cout<<endl;cout<<endl;
cout<<endl;
cout<<endl;
cout<<" ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★"<<endl;
cout<<" ★ 欢迎使用电话簿程序:请输入具体操作(0-5).. ★"<<endl;
cout<<" ★ 0.添 加 记 录 ★"<<endl;
cout<<" ★ 1.查 找 记 录 ★"<<endl;
cout<<" ★ 2.全 部 输 出 ★"<<endl;
cout<<" ★ 3.清 空 记 录 ★"<<endl;
cout<<" ★ 4.文 件 输 出 ★"<<endl;
cout<<" ★ 5.退 出 系 统 ★"<<endl;
cout<<" ★ 6.清 屏 函 数 ★"<<endl;
cout<<" ★ 7.删 除 记 录 ★"<<endl;
cout<<" ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★"<<endl;
}
int main()
{
create();
create2() ;
char sel;
while(1)
{
menu();
cin>>sel;
if(sel=='7')
{
cout<<"请输入电话号码:"<<endl;
cin>>num;
del(num);
}
if(sel=='0')
{
cout<<"请输入要添加的内容:"<<endl;
apend();
}
if(sel=='1')
{ cout<<"0.号码查询,1.姓名查询"<<endl;
int b;
cin>>b;
if(b==0)
{ cout<<"请输入电话号码:"<<endl;
cin >>num;
cout<<"输出查找的信息:"<<endl;
find(num);
}
else if(b==1)
{ cout<<"请输入姓名:"<<endl;
cin >>name;
cout<<"输出查找的信息:"<<endl;
find2(name);}
}
if(sel=='2')
{ cout<<"查询所有记录:"<<endl;
list2();
}
if(sel=='6')
system("cls");
if(sel=='3')
{ cout<<"列表已清空:"<<endl;
create();
create2();
}
if(sel=='4')
{ cout<<"通信录已保存:"<<endl;
save();
}
if(sel=='5') return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -