⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 我的密码.cpp

📁 通讯录是一个完美的程序希望大家能够喜欢多多下载
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#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_ptr->name);
		tempint=strcmp(name,temp_name);
	}
	return(current_ptr);
}
void action::display_list()        //显示所有记录
{
	system("cls");
    char fullname[36];  
    current_ptr=head_ptr;   
    if(current_ptr!=NULL)
    {
		int n,m=0;
		cout<<endl;
		cout<<"\n请键入电话簿记录每屏显示的数目";
		cout<<"(为方便起见,键入的数字应小于20):\n";
		cin>>n;
		cin.ignore(1,'\n');
		int i;
		do
		{
			i=1;
            cout<<"名字:\t\t\t联系号码:\t\t创立时间:\n";
	        cout<<"~~~~~~~~~~~~~~~\t\t~~~~~~~~~~~~~~~\t\t~~~~~~~~~~~~~~~\n";
			do{strcpy(fullname,"");strcat(fullname,current_ptr->name);cout.setf(ios::left);
                cout<<setw(15)<<fullname;cout.unsetf(ios::left);
                cout<<"\t"<<setw(20)<<current_ptr->phone_num <<"\t\t"; 
	            cout<<current_ptr->year<<'-'<<current_ptr->month<<'-';
	            cout<<current_ptr->day<<'\t'<<current_ptr->hour<<':'<<current_ptr->minute<<endl;
                current_ptr=current_ptr->next;i++;m++;cout<<endl;
			}while(current_ptr!=head_ptr&&(i<=n));
		    if(current_ptr!=head_ptr)
			{
				cout<<"~~~~~~~~~\n键入Enter继续...\n";cin.get(pause);system("cls");
			}
			else
				cout<<"~~~~~~~~~\n总共"<<m<<"个记录!\n显示结束!\n~~~~~~~~~\n";
		}while(current_ptr!=head_ptr);
			
	}
    else  
    {
		cout<<setw(39)<<"*********\n"<<setw(37)<<"SORRY!\n"<<setw(40)<<"电话簿为空!\n"<<setw(39)<<"*********\n";cout<<setw(40)<<"键入Enter继续...";
	    cin.get(pause);cin.get(pause);system("cls");return;
	}
	cout<<"~~~~~~~~~\n键入Enter继续...\n";cin.get(pause);system("cls");
}
void action::search_by_lastname()       //查找记录
{
	if(head_ptr==NULL)
	{cin.ignore(1,'\n');system("cls");cout<<setw(39)<<"*********\n"<<setw(37)<<"SORRY!\n"<<setw(40)<<"电话簿为空!\n";
	cout<<setw(41)<<"无记录可查询!\n"<<setw(39)<<"*********\n";cout<<setw(40)<<"键入Enter继续...";
    cin.get(pause);system("cls");}
	else
	{
		do{
	system("cls");
	char search_string[36];
	current_ptr=head_ptr;
	cin.ignore(36,'\n');
	cout<<"\n\n\n"<<setw(45)<<"请键入要查询的人的姓名:";
	cin.get(search_string,36);
	cin.ignore(36,'\n');
	if(strcmp(head_ptr->name,search_string)==0){
		cout<<"\n\n"<<setw(35)<<"找到记录\n"<<setw(38)<<"************\n";
		cout<<setw(30)<<"姓名:"<<"\t\t"<<current_ptr->name<<endl;
		cout<<setw(35)<<"电话号码:\t"<<current_ptr->phone_num<<endl; 
	    cout<<setw(35)<<"创建时间:\t"<<current_ptr->year<<'-'<<current_ptr->month<<'-';
	    cout<<current_ptr->day<<'\t'<<current_ptr->hour<<':';
		cout<<current_ptr->minute<<endl<<setw(38)<<"************\n";}
	else
	{  current_ptr=current_ptr->next;
	while((current_ptr!=head_ptr)&&(strcmp(current_ptr->name,search_string)!=0))
		current_ptr=current_ptr->next;
	if(current_ptr!=head_ptr)
	{
				cout<<"\n\n"<<setw(35)<<"找到记录\n"<<setw(38)<<"************\n";
		cout<<setw(30)<<"姓名:"<<"\t\t"<<current_ptr->name<<endl;
		cout<<setw(35)<<"电话号码:\t"<<current_ptr->phone_num<<endl; 
	    cout<<setw(35)<<"创建时间:\t"<<current_ptr->year<<'-'<<current_ptr->month<<'-';
	    cout<<current_ptr->day<<'\t'<<current_ptr->hour<<':';
		cout<<current_ptr->minute<<endl<<setw(38)<<"************\n";
	}
	else
		cout<<"\n\n\n"<<setw(29)<<"SORRY!\n"<<"\n\n"<<setw(40)<<"电话簿没有该记录!\n";
	}cout<<setw(41)<<"键入Enter继续...";
		cin.get(pause);system("cls");
		cout<<"\n\n\n\n"<<setw(36)<<"需要继续查询记录...\n";
		}while(verify_delete());
		system("cls");
		}
}
void action::delete_record()  //删除节点
{
	system("cls");
	char search_string[36];
	friend_node*p1;
	p1=NULL;
	current_ptr=head_ptr;
	cin.ignore(36,'\n');
    if(current_ptr!=NULL)
	{
		do{
			cin.ignore(1,'\n');
			system("cls");
		cout<<"\n\n\n"<<setw(48)<<"请键入您要删除的联系人的姓氏:";
	    cin.get(search_string,36);
	    cin.ignore(36,'\n');
		if(strcmp(head_ptr->name,search_string)==0)
		{
			cout<<"\n\n"<<setw(35)<<"找到记录\n"<<setw(38)<<"************\n";
		cout<<setw(30)<<"姓名:"<<"\t\t"<<current_ptr->name<<endl;
		cout<<setw(35)<<"电话号码:\t"<<current_ptr->phone_num<<endl; 
	    cout<<setw(35)<<"创建时间:\t"<<current_ptr->year<<'-'<<current_ptr->month<<'-';
	    cout<<current_ptr->day<<'\t'<<current_ptr->hour<<':';
		cout<<current_ptr->minute<<endl<<setw(38)<<"************\n";
		    if(verify_delete())
			{
				cin.ignore(1,'\n');
				delete_head_of_list();            //
			    cout<<"\n\n"<<setw(38)<<"记录已被删除!\n";
			}
			else{cin.ignore(1,'\n');
			cout<<"\n\n"<<setw(38)<<"记录未被删除!\n";}
			goto label0;

		}
		if(head_ptr!=NULL&&head_ptr->next!=head_ptr)
		{
			current_ptr=current_ptr->next;
		    while((current_ptr!=head_ptr)&&(strcmp(current_ptr->name,search_string)!=0))
		    current_ptr=current_ptr->next;
	        if(current_ptr!=head_ptr)
			{
		       system("cls");
		       cout<<"\n\n"<<setw(35)<<"找到记录\n"<<setw(38)<<"************\n";
		       cout<<setw(30)<<"姓名:"<<"\t\t"<<current_ptr->name<<endl;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -