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

📄 wangjianwangcheng.cpp

📁 由两个文件组成的带密码的电话簿管理
💻 CPP
📖 第 1 页 / 共 2 页
字号:
						<<"--1.确定    2.取消--\n";
				    cin>>optionB;
					if (optionB==1)
					{
						Remove(index);
						cout<<"---条目已删除!---\n";
					}
				    break;

			default: break;
		}
	}
}


void DoubleLinkList::SearchByName(char *InName)		//根据姓名搜索并改动
{
	system("cls");
	Node *CNode=FirstNode;
	int option=3, count=0, ChooseIndex=0,optionB=2,optionC;
	strcpy(LowedIn,InName);
	strlwr(LowedIn);
	cout<<"*******匹配项*******\n\n";
	for (int index=1; index<=LengthOfList(); index++)
	{		
		strcpy(LowedName,CNode->Data.Name);
		strlwr(LowedName);
		if (CompName(LowedIn,LowedName))
		{
			count++;
			cout<<"序号-"<<index<<"-\n";
			CNode->show();
			if (count==1)	ChooseIndex=index;
		}
		CNode=CNode->Next;
	}
	switch (count)
	{
		case 0: cout<<"---WARNING: 无匹配结果!---\n";
				break;
				
		case 1: cout<<"--1.修改    2.删除    3.取消--\n";
				cin>>option;
				switch (option)
				{
					case 1: cout<<"--1.全部修改    2.只修改姓名   3.只修改电话号码--\n";
						cin>>optionC;
						switch(optionC)
						{case 1:Edit(ChooseIndex);
						        break;
						 case 2:EditName(ChooseIndex);
							    break;
						 case 3:EditTel(ChooseIndex);
							    break;
						 default :break;
						}
				
                         break;

		            case 2: Remove(ChooseIndex);
							cout<<"---确定删除该条目?---\n"
								<<"--1.确定    2.取消--\n";
						    cin>>optionB;
							if (optionB==1)
							{
								Remove(index);
							 	cout<<"---条目已删除!---\n";
							}
					         break;

					default: break;
				}
				break;

		default: cout<<"---有多个匹配结果,请选择所查找条目的序号: ---\n";
				 cin>>ChooseIndex;
				 system("cls");
				 SearchByIndex(ChooseIndex);
				 break;
	}
}

bool DoubleLinkList::FileInDic()	//查找book.txt是否存在,以决定是否在开始程序时询问打开文件
{
	ifstream checker;
	checker.open("book.txt",ios::in|ios::nocreate);
	if (checker)	return true;
	else	return false;
}

void DoubleLinkList::SaveToFile()	//保存到book.txt
{
	Node *CNode=FirstNode;
	ofstream outfile;
	outfile.open("book.txt",ios::out|ios::trunc);	//清空文件
	if (outfile)
	{
		
		if (!IsEmpty())
		{
			do
			{
				outfile<<CNode->Data.Name<<endl;
				outfile<<CNode->Data.Tel<<endl;
				outfile<<CNode->Data.Time<<endl;
				CNode=CNode->Next;
			}
			while(CNode);		
			outfile<<"END OF FILE"<<endl;
			outfile.close();
			cerr<<"\n---通讯录已保存---\n";
		}
		else
		{
			cerr<<"\n---提示:通讯录无内容!---\n";
		}
	}

	else
		{
			cout<<"\n---WARNING:无法打开文件!---\n";
		}	
	
}


void DoubleLinkList::LoadFromFile()		//从文件读取
{
	Entry NewEntry;
	ifstream infile;
	int End=0;
	infile.open("book.txt",ios::in);
	if (infile)
	{
		do
		{	
			strcpy(NewEntry.Name,"");
			strcpy(NewEntry.Tel,"");
			strcpy(NewEntry.Time,"");
			infile.get(NewEntry.Name,20);
			infile.ignore(20,'\n');
			if (strcmp(NewEntry.Name,"END OF FILE"))
			{
				infile.get(NewEntry.Tel,20);
				infile.ignore(20,'\n');
				infile.get(NewEntry.Time,30);
				infile.ignore(30,'\n');
				Insert(NewEntry);
			}
			else	End=1;
		}while(End==0);
		infile.close();
		cerr<<"\n---已从硬盘读取通讯录---\n";
	}
	else
	{
		cout<<"\n---WARNING:打开文件失败!---\n";
	}
}

void DoubleLinkList::Helpme()
{
help:
	int choice2;
	char pause;
	system("cls");
	cout << "欢迎进入帮助菜单!\n";
	cout << "1: 关于我的记录\n";
	cout << "2: 我怎样清除所有记录\n";
	cout << "3: 什么时候出新版本\n";
	cout << "4: 退出\n";
	cin >>  choice2;
	switch(choice2)
	{
	case 1:
		cout << "存储在一个文件Friends.dat中\n";
		cout << "如果它没在这里,那么它就使用原来的文件Friends.dat\n\n\n";
		pause=cin.get();
		system("cls");
		goto help;
		break;
	case 2:
		cout << "删除文件Friends.dat\n\n\n";
		pause=cin.get();
		system("cls");
		goto help;
		break;
	case 3:
		cout << "当我有时间后\n\n\n";
		pause=cin.get();
		system("cls");
		goto help;
		break;
	default:
		system("cls");
		break;
	}
} 
 
 
void  DoubleLinkList::EditName(int index)
{
	int optionB=0,position=1;
	Entry NewEntry;
	Node *CNode=FirstNode;
	do { cout<<"输入姓名:";
	     cin>>NewEntry.Name;
	}while(!IsValid(NewEntry.Name));
    system("cls");
	cout<<"\n确定要将\n";
	while(position!=index)
	{
	CNode=CNode->Next;position++;
	}
	cout<<CNode->Data.Name<<"\n\n";
	cout<<"替换为:\n";
	cout<<"姓名:"<<NewEntry.Name<<"\n?\n\n";
	cout<<"--1.确定   2.取消--\n";
Ten: cin>>optionB;
	 cout<<"   ";
	 switch(optionB)
	 {case 1:strcpy(CNode->Data.Name,NewEntry.Name);
	         cout<<"---姓名已修改---";
			 break;
	  case 2:break;
      default:cout<<"输入错误,重新输入\n";
		    goto Ten;
	 }
}

void  DoubleLinkList::ZNSearch(char *FirstLetter)  //根据首字母智能查找
{    system("cls");
     Node *CNode=FirstNode;
	 int count=0;
	 strcpy(LowedIn,FirstLetter);
	 strlwr(LowedIn);
	 cout<<"*******匹配项*******\n\n";
	for (int index=1; index<=LengthOfList(); index++)
	{		
		strcpy(LowedName,CNode->Data.Name);
		strlwr(LowedName);
		if (!strncmp(LowedIn,LowedName,1))
		{ count++;
		  cout<<"序号-"<<index<<"-\n";
          CNode->show();
		}
		  CNode=CNode->Next;
	}
	if (count==0)  cout<<"-----没有与之匹配项.-----\n";
	else cout<<"共有"<<count<<"个与之相匹配的项.\n";
}
		

void  DoubleLinkList::EditTel(int index)
{
	int optionB=0,position=1;
	Entry NewEntry;
	Node *CNode=FirstNode;
	do {cout<<"输入电话号码:";
	cin>>NewEntry.Tel;}
	while(NewEntry.Tel==0);
    system("cls");
	cout<<"\n确定要将\n";
	while(position!=index)
	{CNode=CNode->Next;position++;}
	cout<<CNode->Data.Tel<<"\n\n";
	cout<<"替换为:\n";
	cout<<"电话号码:"<<NewEntry.Tel<<"\n?\n\n";
	cout<<"--1.确定   2.取消--\n";
Ten: cin>>optionB;
	 cout<<"   ";
	 switch(optionB)
	 {case 1: strcpy(CNode->Data.Tel,NewEntry.Tel);            
	         cout<<"---电话号码已修改---";
			 break;
	  case 2:break;
      default:cout<<"输入错误,重新输入\n";
		    goto Ten;
	 }
}


char  *DoubleLinkList::gettime()
{   time_t tval;
	struct tm *now;   // 获取当前的日期和时间
	tval=time(NULL);
	now=localtime(&tval);
    return ctime(&tval);
	}






/*主函数**************************************/

void main()
{
	int password;
  cout<<"请输入密码";
  cin>>password;
  system("cls");
  if (password==mima())
  {
	DoubleLinkList List;
	Entry NewEntry;
	int option, index;
	char InName[20],FirstLetter[5];
	if(List.FileInDic())
	{
		cerr<<"   *****通讯录管理 V0.2 beta*****\n"
			<<"\n---是否读取保存在硬盘上的通讯录资料?---\n"
			<<"--1.是    2.否--\n";
		cin>>option;
		if (option==1) 
		{
			List.LoadFromFile();
			cerr<<"\nLoading...\n";
		}
	}
	do
	{   system("cls");
		cout<<"    *****通讯录管理 V0.2 beta*****\n"
		<<"  1. 查看全部条目...\n"
		<<"  2. 添加新条目...\n"
		<<"  3. 根据姓名查找并改动条目...\n"
		<<"  4. 根据序号查找并改动条目...\n"
		<<"  5. 根据首字母智能查找...\n"
		<<"  6. 将通讯录保存到硬盘...\n"
		<<"  7. 从硬盘中读取通讯录...\n"
		<<"  8. 帮助...\n"
		<<"  9. 关于...\n"
		<<"  10. 退出...\n\n"
		<<"选择功能序号:  ";
		cin>>option;
		switch(option)
		{	
			case 1: List.ShowAll();
					break;

			case 2: do
					{
						cout<<"\n输入姓名:";
						cin>>NewEntry.Name;
					}
					while (!List.IsValid(NewEntry.Name));
					cout<<"电话:";
					cin>>NewEntry.Tel;
					strcpy(NewEntry.Time,List.gettime());
					List.Insert(NewEntry);
					cerr<<"\n---添加成功!按Enter键返回---\n";
					getch();
					break;

			case 3: system("cls");
				    cout<<"---输入欲查找的姓名,支持通配符'*','?':---\n";
				    cin>>InName;					
					List.SearchByName(InName);
					cout<<"\n---按Enter键返回---\n";
					getch();
					break;

			case 4: system("cls");
				    cout<<"---输入欲查找的序号---\n";
				    cin>>index;
					List.SearchByIndex(index);
					cout<<"\n---按Enter键返回---\n";
					getch();
					break;
			case 5: system("cls");
				    cout<<"---输入名字的首字母---\n";
				    cin>>FirstLetter;
					List.ZNSearch(FirstLetter);
					cout<<"\n---按Enter键返回---\n";
					getch();
					break; 
			case 6: system("cls");
				    List.SaveToFile();
				    cout<<"\n---按Enter键返回---n";
					getch();
					break;

			case 7: system("cls");
				    List.LoadFromFile();
					cout<<"\n---按Enter键返回---\n";
					getch();
					break;
			case 8: system("cls");
				    List.Helpme();
				    break;
			case 9: system("cls");
					cout<<"**** VC++课程设计  通讯录管理 ****\n"
						<<"   原代码作者: Vinayak Marwah      \n"
						<<"   改编: 王建0510200254 江琳0510200255 王子瑜0510200261 \n"
						<<"      南京理工大·自动化学院\n"
						<<"**********************************\n";
					cout<<"\n---按Enter键返回---\n";
					getch();

					break;

			default: break;
		}
	}while(option!=10);
	cout<<"\n---退出程序前是否保存通讯录?---\n"
		<<"--1.保存    2.放弃--\n";
	cin>>option;
	if (option==1) List.SaveToFile();
	}
	else 
	{
     cout<<"密码错误,系统将自动关闭";
     cin.get();
	 cout<<endl;
	}
}

⌨️ 快捷键说明

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