artemgf

Тестовое задание

Mar 17th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. #include <set>
  5. #include <map>
  6. #include <algorithm>
  7. #include <string>
  8. #include <math.h>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. struct profil
  14. {
  15.     string name, password;
  16. };
  17.  
  18. bool operator==(profil data1, profil data2)
  19. {
  20.     return data1.name == data2.name;
  21. }
  22.  
  23. bool findbool(vector <profil> data, string name)
  24. {
  25.     auto i = data.begin();
  26.     bool p = false;
  27.     for (i; i != data.end(); i++)
  28.     {
  29.         if (i->name == name)
  30.         {
  31.             p = true;
  32.             break;
  33.         }
  34.     }
  35.     return p;
  36. }
  37.  
  38. bool findboolstr(vector <string> data, string name)
  39. {
  40.     bool p = false;
  41.     auto i = data.begin();
  42.     for (i; i != data.end(); i++)
  43.     {
  44.         if (*i == name)
  45.         {
  46.             p = true;
  47.             break;
  48.         }
  49.     }
  50.     return p;
  51. }
  52.  
  53. vector<profil>::iterator find(vector <profil> &data, string name)
  54. {
  55.     auto i = data.begin();
  56.     for (i; i != data.end(); i++)
  57.     {
  58.         if (i->name == name)
  59.         {
  60.             break;
  61.         }
  62.     }
  63.  
  64.     return i;
  65. }
  66.  
  67. vector<string>::iterator findstr(vector <string> &data, string name)
  68. {
  69.     auto i= data.begin();
  70.     for (i; i != data.end(); i++)
  71.     {
  72.         if (*i == name)
  73.         {
  74.             break;
  75.         }
  76.     }
  77.     return i;
  78. }
  79.  
  80. vector<profil> reg(vector<profil> data)
  81. {
  82.     string name, password;
  83.     cin >> name >> password;
  84.  
  85.     if (findbool(data, name))
  86.     {
  87.         cout << "fail: user already exists" << endl;
  88.         return data;
  89.     }
  90.     else
  91.     {
  92.         profil prom;
  93.         prom.name = name;
  94.         prom.password = password;
  95.         data.push_back(prom);
  96.         cout << "success: new user added" << endl;
  97.         return data;
  98.     }
  99. }
  100.  
  101. vector <string> login(vector<profil> data, vector <string> online)
  102. {
  103.     string name, password;
  104.     auto num = data.begin();
  105.     cin >> name >> password;
  106.  
  107.     if (findbool(data, name))
  108.     {
  109.         num = find(data, name);
  110.         if (num->password == password)
  111.         {
  112.             if (findboolstr(online, name))
  113.             {
  114.                 cout << "fail: already logged in" << endl;
  115.                 return online;
  116.             }
  117.             else
  118.             {
  119.                 cout << "success: user logged in" << endl;
  120.                 online.push_back(name);
  121.                 return online;
  122.             }
  123.         }
  124.         else
  125.         {
  126.             if (num->password != password)
  127.             {
  128.                 cout << "fail: incorrect password" << endl;
  129.                 return online;
  130.             }
  131.             else
  132.                 if (findboolstr(online, name))
  133.                 {
  134.                     cout << "fail: already logged in" << endl;
  135.                     return online;
  136.                 }
  137.         }
  138.  
  139.     }
  140.     else
  141.     {
  142.         cout << "fail: no such user" << endl;
  143.         return online;
  144.     }
  145. }
  146.  
  147. vector <string> logout(vector<profil> data, vector <string> online)
  148. {
  149.     string name;
  150.     auto num = online.begin();
  151.     cin >> name;
  152.     if(findbool(data, name))
  153.         if (findboolstr(online, name))
  154.         {
  155.             num = findstr(online, name);
  156.             online.erase(num);
  157.             cout << "success: user logged out" << endl;
  158.             return online;
  159.         }
  160.         else
  161.         {
  162.             cout << "fail: already logged out" << endl;
  163.             return online;
  164.         }
  165.     else
  166.     {
  167.         cout << "fail: no such user" << endl;
  168.         return online;
  169.     }
  170. }
  171.  
  172. int main()
  173. {
  174.     vector <string> online;
  175.     vector <profil> data;
  176.     string comand;
  177.     int n;
  178.  
  179.     cin >> n;
  180.  
  181.     for (int i = 1; i <= n; i++)
  182.     {
  183.         cin >> comand;
  184.         if (comand == "register")
  185.             data = reg(data);
  186.         else
  187.             if (comand == "login")
  188.                 online = login(data, online);
  189.             else
  190.                 online = logout(data, online);
  191.     }
  192.  
  193.     _getch();
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment