Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string>
- #include <set>
- #include <map>
- #include <algorithm>
- #include <string>
- #include <math.h>
- #include <vector>
- using namespace std;
- struct profil
- {
- string name, password;
- };
- bool operator==(profil data1, profil data2)
- {
- return data1.name == data2.name;
- }
- bool findbool(vector <profil> data, string name)
- {
- auto i = data.begin();
- bool p = false;
- for (i; i != data.end(); i++)
- {
- if (i->name == name)
- {
- p = true;
- break;
- }
- }
- return p;
- }
- bool findboolstr(vector <string> data, string name)
- {
- bool p = false;
- auto i = data.begin();
- for (i; i != data.end(); i++)
- {
- if (*i == name)
- {
- p = true;
- break;
- }
- }
- return p;
- }
- vector<profil>::iterator find(vector <profil> &data, string name)
- {
- auto i = data.begin();
- for (i; i != data.end(); i++)
- {
- if (i->name == name)
- {
- break;
- }
- }
- return i;
- }
- vector<string>::iterator findstr(vector <string> &data, string name)
- {
- auto i= data.begin();
- for (i; i != data.end(); i++)
- {
- if (*i == name)
- {
- break;
- }
- }
- return i;
- }
- vector<profil> reg(vector<profil> data)
- {
- string name, password;
- cin >> name >> password;
- if (findbool(data, name))
- {
- cout << "fail: user already exists" << endl;
- return data;
- }
- else
- {
- profil prom;
- prom.name = name;
- prom.password = password;
- data.push_back(prom);
- cout << "success: new user added" << endl;
- return data;
- }
- }
- vector <string> login(vector<profil> data, vector <string> online)
- {
- string name, password;
- auto num = data.begin();
- cin >> name >> password;
- if (findbool(data, name))
- {
- num = find(data, name);
- if (num->password == password)
- {
- if (findboolstr(online, name))
- {
- cout << "fail: already logged in" << endl;
- return online;
- }
- else
- {
- cout << "success: user logged in" << endl;
- online.push_back(name);
- return online;
- }
- }
- else
- {
- if (num->password != password)
- {
- cout << "fail: incorrect password" << endl;
- return online;
- }
- else
- if (findboolstr(online, name))
- {
- cout << "fail: already logged in" << endl;
- return online;
- }
- }
- }
- else
- {
- cout << "fail: no such user" << endl;
- return online;
- }
- }
- vector <string> logout(vector<profil> data, vector <string> online)
- {
- string name;
- auto num = online.begin();
- cin >> name;
- if(findbool(data, name))
- if (findboolstr(online, name))
- {
- num = findstr(online, name);
- online.erase(num);
- cout << "success: user logged out" << endl;
- return online;
- }
- else
- {
- cout << "fail: already logged out" << endl;
- return online;
- }
- else
- {
- cout << "fail: no such user" << endl;
- return online;
- }
- }
- int main()
- {
- vector <string> online;
- vector <profil> data;
- string comand;
- int n;
- cin >> n;
- for (int i = 1; i <= n; i++)
- {
- cin >> comand;
- if (comand == "register")
- data = reg(data);
- else
- if (comand == "login")
- online = login(data, online);
- else
- online = logout(data, online);
- }
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment