vencinachev

Zad2

Apr 19th, 2021 (edited)
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class User
  7. {
  8. public:
  9.     User();
  10.     User(const User& u);
  11.     User(const char* uname, const char* pass);
  12.     ~User();
  13.     int getCnt() const;
  14.     void setCnt(int cnt);
  15.     friend bool operator==(const User& u1, const User& u2);
  16.     friend bool passCompare(const User& u1, const User& u2);
  17. private:
  18.     char *username;
  19.     char *password;
  20.     int cnt;
  21. };
  22.  
  23. User::User()
  24. {
  25.     this->cnt = 0;
  26. }
  27. User::User(const char* uname, const char* pass)
  28. {
  29.     this->username = new char[strlen(uname) + 1];
  30.     strcpy(this->username, uname);
  31.     this->password = new char[strlen(pass) + 1];
  32.     strcpy(this->password, pass);
  33.     this->cnt = 0;
  34. }
  35.  
  36. bool operator==(const User& u1, const User& u2)
  37. {
  38.     if (strcmp(u1.username, u2.username) == 0)
  39.     {
  40.         return true;
  41.     }
  42.     return false;
  43. }
  44.  
  45. bool passCompare(const User& u1, const User& u2)
  46. {
  47.     if (strcmp(u1.username, u2.username) == 0 && strcmp(u1.password, u2.password) == 0)
  48.     {
  49.         return true;
  50.     }
  51.     return false;
  52. }
  53.  User::~User()
  54.  {
  55.      delete[] this->username;
  56.      delete[] this->password;
  57.  }
  58.  
  59.  User::User(const User& u)
  60.  {
  61.     this->username = new char[strlen(u.username) + 1];
  62.     strcpy(this->username, u.username);
  63.     this->password = new char[strlen(u.password) + 1];
  64.     strcpy(this->password, u.password);
  65.     this->cnt = u.cnt;
  66.  }
  67.  
  68. int User::getCnt() const
  69. {
  70.      return this->cnt;
  71. }
  72.  
  73. void User::setCnt(int cnt)
  74. {
  75.     this->cnt = cnt;
  76. }
  77.  
  78. bool signIn(User users[], int n, char *uname, char *pass)
  79. {
  80.     User usr(uname, pass);
  81.     for (int i = 0; i < n; i++)
  82.     {
  83.         if (users[i] == usr)
  84.         {
  85.             if (passCompare(users[i], usr))
  86.             {
  87.                 return true;
  88.             }
  89.             users[i].setCnt(users[i].getCnt() + 1);
  90.         }
  91.     }
  92.     return false;
  93. }
  94.  
  95. int main()
  96. {
  97.     User users[3];
  98.     users[0] = User("Pesho", "1234");
  99.     users[1] = User("Pesho", "12345");
  100.     users[2] = User("Peshoo", "123456");
  101.     if (signIn(users, 3, "Pesho", "12345"))
  102.     {
  103.         cout << "Login: Yes";
  104.     }
  105.     else
  106.     {
  107.         cout << "Login: No";
  108.     }
  109.     return 0;
  110. }
  111.  
Add Comment
Please, Sign In to add comment