C ++ proxy mode to achieve verification Login

C ++ proxy mode to achieve verification Login

#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <iomanip>
#include <algorithm>

using namespace std;

class Stream //系统类(建立类让代理类和类产生关联)
{
public:
	virtual void Run() = 0;
};

class AgencyStream :public Stream//真正要访问的类
{
public:
	void Run() {
		cout << "系统启动" << endl;
	}
};

class Verification :public Stream
{
public:
	Verification(string UserName, string Password) :UserName(UserName), Password(Password) {
		this->stream = new AgencyStream;
	}


	void Run() {
		if (Login()==true)
		{
			this->stream->Run();
		}
		else
		{
			//cout << "账号或者密码错误,系统启动失败" << endl;
		}
	}

private:
	bool Login() {
		ifstream ifs;
		ifs.open("Login.txt", ios::in);
		char title[256];
		ifs.getline(title, 256);

		//cout << title << endl;
		char str[64];
		string username;
		string password;
		map<string, string>user;

		while (ifs.getline(str, 64))
		{
			istringstream ist(str);
			ist >> username >> password;
			ist.clear();
			user.insert(make_pair(username, password));
		}

		map<string, string>::iterator it = user.find(UserName);
		if (it!= user.end())
		{
			if ((*it).second== Password)
			{
				return true;
			}
			else
			{
				cout << "密码错误" << endl;
			}			
		}
		else
		{
			cout << "账号不存在" << endl;
			return false;
		}
	}
	~Verification() {
		if (stream)
		{
			delete stream;
		}
	}

private:
	AgencyStream* stream;
	string UserName;
	string Password;
};
void test() {
	string UserName;
	string Password;
	cout << "请输入账号" << endl;
	cin >> UserName;
	cout << "请输入密码" << endl;
	cin >> Password;
	Verification*my = new Verification(UserName, Password);
	my->Run();
}

void main() {
	test();
}

Renderings:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44567289/article/details/91354425