#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
class friend_node //节点类,定义,包括:姓,名和电话号码,
{ //以及记录的时间;
public:
char name[36];
char phone_num[12];
friend_node*next;
friend_node*last;
int year;
int month;
int day;
int hour;
int minute;
};
class action //定义双向链表的操作函数;
{
public:
void handle_choice(int choice);
void add_record();
void insert_node(friend_node*new_rec_ptr);
friend_node*position_insertion_point(char lastname[20]);
void make_node_new_head(friend_node*new_rec_ptr);
void add_node_to_end(friend_node*new_rec_ptr);
void edit_record();
void display_list();
void delete_record();
void delete_head_of_list();
void delete_end_of_list(friend_node*previous_ptr);
void delete_from_middle_of_list(friend_node*previous_ptr);
int verify_delete();
void delete_node(friend_node*previous_ptr);
void delete_list();
void search_by_lastname();
void write_list_to_file();
void write_list_to_file2();
void load_list_from_file();
void insert(friend_node*new_rec_ptr);
void load_list_from_file2();
int change(char*s);
tm*current_time;
time_t time_date;
void ccode(char*s);
void change_code();
void process();
};
friend_node*head_ptr; //全程变量,链头指针
friend_node*tail_ptr; //全程变量,链尾指针
friend_node*current_ptr;
friend_node*p0; //全程变量,用于指明当前在链表中的位置
char pause;
char Code[5];
int main() //主函数
{
action re_action;
char s[8];
char ch;
int choice;
head_ptr=NULL;
tail_ptr=NULL;
cout<<"\n\n\n\n"<<setw(49)<<"欢迎进入电话联系簿!\n"<<"\n\n"<<setw(59)<<"Welcome To Use This Telephone Book!\n";
cout<<'\n'<<setw(47)<<"键入Enter继续...";cin.get(pause);system("cls");
cout<<"\n\n\n"<<setw(60)<<"感谢所有支持并帮助我完善此程序的老师和同学们!\n";
cout<<'\n'<<setw(45)<<"如果没有他们的帮助...\n";
cout<<'\n'<<setw(43)<<"程序可能会延期完成!\n";
cout<<'\n'<<setw(53)<<"由于作者水平有限,加上时间仓促...\n";
cout<<'\n'<<setw(50)<<"程序中难免会有不足,还望海涵!\n";
cout<<'\n'<<setw(50)<<"作者:张建龙\t2006-09-21\n";
cout<<'\n'<<setw(40)<<"键入Enter继续..."; cin.get(pause);system("cls");
cout<<'\n';
int i=0,k=0; //密码验证
cout<<"\n"<<setw(56)<<"WARNING:密码连续错误输入不能超过3次!\n";
re_action.load_list_from_file2();
label: cout<<"\n\n\n\n\n"<<setw(36)<<"请输入密码:";
cout.flush();
ch=getch();
while(ch!=13)
{
s[i++]=ch;
cout<<"*";
cout.flush();
ch=getch();
}cout<<'\n';
s[i]=0;
for(i=0;i<5;i++)
{
if(s[i]!=Code[i])
{
cout<<"\n"<<setw(38)<<"密码不正确!\n";
cout<<'\n'<<setw(5)<<"您已经输入密码错误已有"<<k+1<<"次!";
cin.ignore(38,'\n');
system("cls");
k++;
if(k==3)exit(1);
goto label;
}
}
system("cls");
re_action.load_list_from_file(); //从文件当中读取链表信息
do{
cout<<'\n';cout<<setw(45)<<"WARNING:版权所有,侵权必究!\n";
cout<<'\n'<<setw(45)<<"如有雷同,纯属巧合!";
cout<<"\n\n"<<setw(50)<<"Windows友情提醒:请使用正版电话簿!\n";
cout<<"\n\n\n"<<setw(39)<<"~~~~~~~~~~~~~~~~~~\n";
cout<<setw(35)<<"1-增加记录\n";
cout<<setw(35)<<"2-显示记录\n";
cout<<setw(35)<<"3-查找记录\n";
cout<<setw(35)<<"4-删除记录\n";
cout<<setw(35)<<"5-修改记录\n";
cout<<setw(35)<<"6-程序信息\n";
cout<<setw(35)<<"7-修改密码\n";
cout<<setw(35)<<"0-退出程序\n";
cout<<setw(39)<<"~~~~~~~~~~~~~~~~~~\n";
cout<<setw(35)<<"请键入您的选择:"; //用户选择界面,用户通过提示进行操作
cin>>choice;
re_action.handle_choice(choice); //处理用户的选择
}while(choice!=0);
return 0;
}
void action::handle_choice(int choice) //对用户的选择进行操作
{
switch(choice)
{
case 1:
add_record(); //增加节点
break;
case 2:
display_list(); //显示所有记录
break;
case 3:
search_by_lastname(); //查找记录
break;
case 4:
delete_record(); //删除记录
break;
case 5:
edit_record(); //修改记录
break;
case 6:
process();
break;
case 7:
change_code();break; //修改密码
case 0:
write_list_to_file(); //将链表写入文件
if(head_ptr!=NULL)
delete_list(); //删除内存当中的链表
break;
default:
cout<<"\n\n\n"<<"键入错误!\n";
system("cls");
break;
}
}
void action::insert(friend_node*new_rec_ptr) //当调用源文件当中的数据时进行重新创建链表
{
system("cls");
friend_node*temp_ptr;
if(head_ptr==NULL)
{
new_rec_ptr->next=new_rec_ptr;
new_rec_ptr->last=new_rec_ptr;
head_ptr=new_rec_ptr;
tail_ptr=new_rec_ptr;
return;
}
if(head_ptr->next==head_ptr)
{
if(strcmp(new_rec_ptr->name,head_ptr->name)<0)
make_node_new_head(new_rec_ptr);
else
add_node_to_end(new_rec_ptr);
return;
}
if(head_ptr->next!=head_ptr)
{
current_ptr=head_ptr->next;
while((strcmp(new_rec_ptr->name,current_ptr->name)>0)&&(current_ptr!=head_ptr))
current_ptr=current_ptr->next;
if(current_ptr==head_ptr)
add_node_to_end(new_rec_ptr);
else
{
temp_ptr=current_ptr->last;
temp_ptr->next=new_rec_ptr;
new_rec_ptr->last=temp_ptr;
current_ptr->last=new_rec_ptr;
new_rec_ptr->next=current_ptr;
}
return;
}
}
void action::add_record() //增加节点
{
do{
friend_node*new_rec_ptr;
new_rec_ptr=new friend_node;
if(new_rec_ptr!=NULL)
{
system("cls");
cin.ignore(36,'\n');
cout<<'\n'<<setw(42)<<"~~~~~~~~~~~~~~~\n";
cout<<setw(35)<<"姓名:";
cin.get(new_rec_ptr->name,36);
cin.ignore(36,'\n');
cout<<setw(39)<<"电话号码:";
cin.get(new_rec_ptr->phone_num,12);
cin.ignore(20,'\n');
time_date=time(0);
current_time=localtime(&time_date);
new_rec_ptr->year=current_time->tm_year+1900;
new_rec_ptr->month=current_time->tm_mon+1;
new_rec_ptr->day=current_time->tm_mday;
new_rec_ptr->hour=current_time->tm_hour;
new_rec_ptr->minute=current_time->tm_min;
insert_node(new_rec_ptr);
}
else
{
cout<<"Warning:存储失败!新的记录不能增加进去!\n";
}
system("cls");
cout<<"\n\n\n\n"<<setw(36)<<"需要继续增加记录...\n";
}while(verify_delete());
system("cls");
}
void action::insert_node(friend_node*new_rec_ptr) //在动态增加节点时所调用的增加节点函数
{
system("cls");
friend_node*p1;
if(head_ptr==NULL)
{
head_ptr=new_rec_ptr;
new_rec_ptr->next=head_ptr;
new_rec_ptr->last=head_ptr;
tail_ptr=new_rec_ptr;
return;
}
if(head_ptr->next==head_ptr)
{
if(strcmp(head_ptr->name,new_rec_ptr->name)>0)
make_node_new_head(new_rec_ptr);
else
add_node_to_end(new_rec_ptr);
return;
}
if(head_ptr->next!=head_ptr)
{
if(strcmp(head_ptr->name,new_rec_ptr->name)<0)
{
current_ptr=position_insertion_point(new_rec_ptr->name);
p1=current_ptr->last;
new_rec_ptr->next=current_ptr;
new_rec_ptr->last=p1;
p1->next=new_rec_ptr;
current_ptr->last=new_rec_ptr;
return;
}
else
make_node_new_head(new_rec_ptr);
}
}
void action::make_node_new_head(friend_node*new_rec_ptr) //重新给链表创建头节点
{
head_ptr->last=new_rec_ptr;
new_rec_ptr->next=head_ptr;
new_rec_ptr->last=tail_ptr;
head_ptr=new_rec_ptr;
tail_ptr->next=new_rec_ptr;
}
void action::add_node_to_end(friend_node*new_rec_ptr) //为链表重新创建尾节点
{
new_rec_ptr->next=head_ptr;
tail_ptr->next=new_rec_ptr;
new_rec_ptr->last=tail_ptr;
head_ptr->last=new_rec_ptr;
tail_ptr=new_rec_ptr;
}
friend_node*action::position_insertion_point(char name[36]) //使的current_ptr指向所需要到的那个节点之上
{
char temp_name[36];
int tempint;
current_ptr=head_ptr->next;
strcpy(temp_name,current_ptr->name);
tempint=strcmp(name,temp_name);
while((tempint>0)&&(current_ptr->next!=head_ptr))
{
current_ptr=current_ptr->next;
strcpy(temp_name,current_p