From f2a2746eab0edfc34261e8217f316d52cb7a86fc Mon Sep 17 00:00:00 2001 From: MasihBa Date: Tue, 29 Apr 2025 16:54:25 +0330 Subject: [PATCH 1/2] completed --- q1.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 q1.cpp diff --git a/q1.cpp b/q1.cpp new file mode 100644 index 0000000..a1a4ec5 --- /dev/null +++ b/q1.cpp @@ -0,0 +1,170 @@ +#include + #include + using namespace std; + const int MAX = 100; + + template + int myfind(T* begin, T* end, F f) { + for (T* itr = begin; itr != end; itr++) { + if (f(*itr)) { + return itr - begin;0 + } + } + return -1; +} + + class AbstractUser { + public: + AbstractUser(const string& username, const string& password) + : username(username), password(password) {} + + virtual ~AbstractUser() = default; + + virtual void displayRole() const = 0; + virtual bool hasAccessToSee(AbstractUser*) const = 0; + + string getUsername() const { + return username; + } + bool isPasswordCorrect(const string& pass) { + return (pass == password); + } + + protected: + string username; + string password; + }; + + class Student : public AbstractUser { + public: + Student(const string& username, const string& password, int grade) + : AbstractUser(username, password), grade(grade) {} + + void displayRole() const override { + cout << "Student: " << username << ", Grade: " << grade << endl; + } + bool hasAccessToSee(AbstractUser* user) const override { + return false; + } + + private: + int grade; + }; + + class Staff : public AbstractUser { + public: + Staff(const string& username, const string& password, int salary) + : AbstractUser(username, password), salary(salary) {} + + void displayRole() const override { + cout << "Staff: " << username << ", Salary: " << salary << endl; + } + bool hasAccessToSee(AbstractUser* user) const override { + if (dynamic_cast(user) != nullptr) { + return true; + } + } + + private: + int salary; + }; + + class Admin : public Staff { + public: + Admin(const string& username, const string& password, int salary) + : Staff(username, password, salary) {} + + void displayRole() const override { + + cout << "Admin "; + Staff::displayRole(); + } + + bool hasAccessToSee(AbstractUser* user) const override { + if (dynamic_cast(user) != nullptr) { + return true; + } + + if(dynamic_cast(user) != nullptr) { + return true; + } + } + + }; + + AbstractUser* current_user; + AbstractUser* users[100]; + int user_cnt; + + void print() { + if (!current_user) { + return; + } + for (AbstractUser* user : users) { + if (current_user->hasAccessToSee(user)) { + user->displayRole(); + } + } + } + + void addStudent(string username, string password, int grade) { + int index = myfind(users, users + user_cnt, [&](AbstractUser* u) { + return u->getUsername() == username;}); + + if (index != -1) { + return; + } + if (dynamic_cast(current_user) == nullptr){ + cout << "permision denied"<> username; + cout << "password:"; + cin >> password; + int index = myfind(users, users + user_cnt, [&](AbstractUser* u) {return u->getUsername() == username && u->isPasswordCorrect(password);}); + if (index!=-1) { + current_user = users[index]; + break; + } + + } + int choice; + while (1) { + cout << "1.print" << endl; + cout<<"2.add student"<> choice; + if (choice == 1) { + print(); + } + else { + string user, pass; + int grade; + cin >> user >> pass >> grade; + addStudent(user, pass, grade); + + } + + } + + return 0; + } \ No newline at end of file From fb50959244ab129064dc00ac6d15dbfb084cc593 Mon Sep 17 00:00:00 2001 From: MasihBa Date: Tue, 29 Apr 2025 17:17:24 +0330 Subject: [PATCH 2/2] masih bagheri_40319843_1 --- q1.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/q1.cpp b/q1.cpp index a1a4ec5..90ecae7 100644 --- a/q1.cpp +++ b/q1.cpp @@ -7,7 +7,7 @@ int myfind(T* begin, T* end, F f) { for (T* itr = begin; itr != end; itr++) { if (f(*itr)) { - return itr - begin;0 + return itr - begin; } } return -1;