2021.10.15(用戶登錄C++)


c++源代碼:

#include<iostream>
using namespace std;

class Information
{
private:char* Users;//用戶名
		char* Password;//密碼
		char* Password1;//第二次輸入的密碼
		char* users;//登錄時輸入的用戶名
		char* password;//登錄時輸入的密碼
public:
	Information(char* Users = "1234567", char* Password = "qwer1234")
	{ //構造函數
		this->Users = Users, this->Password = Password;
	}

	~Information() {} //析構函數

	void deleteusers() {//用戶名重復時刪除該注冊
		Users = "______";
		Password = "______";
	}

	int Login(char* users, char* password) {//登錄
		int b = 1;//控制登錄的成功或失敗
		for (int o = 0, p = 0; o < strlen(Users) && o < strlen(users) && p < strlen(Password) && p < strlen(password); o++, p++) {
			if ((*(Users + o) == *(users + o)) && (*(Password + p) == *(password + p)))   b = 0;//成功為零,失敗為一
			else { b = 1; break; }
		}
		return b;
	}

	int setinformation() {//注冊
		int judge1 = 0;
		cout << "請設置您的用戶名:";
		Users = (char*)malloc(20 * sizeof(char));
		cin >> Users;
		cout << "請設置您的密碼:";
		Password = (char*)malloc(20 * sizeof(char));
		cin >> Password;
		cout << "請再次輸入您設置的密碼:";
		Password1 = (char*)malloc(20 * sizeof(char));
		cin >> Password1;//用戶名密碼輸入
		if (strlen(Password) == strlen(Password1))
		{
			int p;
			for (p = 0; p < strlen(Password); p++)
			{
				if (*(Password + p) == *(Password1 + p))  judge1 = judge1 + 1; //判斷兩次輸入的密碼是否相等
			}
			if (judge1 == p) return 1;
			else return 0;
		}
		else return 0;
	}

	char* getUsers() { return Users; }//返回用戶名以判斷注冊的用戶名是否重復
};

int main() {
	Information inf[100];//用戶信息的數組
	int a = 0;//用戶的個數
	int c, e;//功能選擇
	char* u;//登錄時用戶名輸入
	char* p;//登錄時密碼輸入
	int a1, b1;//控制判斷條件
	for (int d = 0; d < 100; d++) {
		cout << "1-注冊;2-登錄:";//功能選擇
		cin >> c;
		switch (c)
		{
		case 1:
		{
				  for (int i = 0; i <= 100; i++)
				  {
					  int con = inf[i].setinformation();//用戶注冊
					  a1 = 0;
					  for (int j = 0; j < a; j++)
					  {
						  char* f = inf[j].getUsers();
						  char* h = inf[a].getUsers();
						  if (strlen(f) == strlen(h))
						  {
							  int judge2 = 0;
							  int o;
							  for (o = 0; o < strlen(f); o++)
							  {
								  if (*(f + o) == *(h + o))   judge2 = judge2 + 1;
							  }
							  if (judge2 == o)
							  {
								  cout << "用戶名已存在,請重新設置" << endl; i--;
								  a1 = 1;
								  inf[a].deleteusers();
								  a--;
								  break;
							  }
						  }
					  }
					  if (con == 0 && a1 == 0)
					  {
						  cout << "兩次輸入密碼不相同,請重新設置" << endl;
						  i--;
						  inf[a].deleteusers();
						  a--;
					  }
					  else if (con == 1 && a1 == 0)  cout << "設置成功!" << endl;
					  a = a + 1;//個數加一
					  cout << "'1'繼續,'2'返回" << endl;//是否繼續注冊
					  cin >> e;
					  if (e == 2) { break; }
					  else if (e == 1)
					  {
						  continue;
					  }
					  else if (e != 1 && e != 2)
					  {
						  cout << "輸入無效" << endl;
						  break;
					  }
				  }
				  break;
		}
		case 2: {
					cout << "請輸入用戶名: ";
					u = (char*)malloc(20 * sizeof(char));
					cin >> u;
					cout << "請輸入密碼:  ";
					p = (char*)malloc(20 * sizeof(char));
					cin >> p;
					for (int z = 0; z <= a; z++)
					{
						b1 = inf[z].Login(u, p);
						if (b1 == 0)
						{
							cout << "Successfully loging in." << endl << "Welcome to my world!" << endl;
							break;
						}//登錄成功
					}
					if (b1 == 1) cout << "The user is not exist or the password is wrong." << endl;//登錄失敗
					break;
		}
		default: cout << "abnormal input" << endl;
		}
	}
	return 0;
}

實現截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM