fork download
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <limits>
  5. using namespace std;
  6.  
  7. bool readInt(const string& msg, int& x) {
  8. while (true) {
  9. cout << msg;
  10. if (cin >> x) return true;
  11. if (cin.eof()) return false;
  12. cin.clear();
  13. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  14. cout << "Phai nhap so nguyen!\n";
  15. }
  16. }
  17. bool readDouble(const string& msg, double& x) {
  18. while (true) {
  19. cout << msg;
  20. if (cin >> x) return true;
  21. if (cin.eof()) return false;
  22. cin.clear();
  23. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  24. cout << "Phai nhap so!\n";
  25. }
  26. }
  27. string readLine(const string& msg) {
  28. cout << msg;
  29. string s;
  30. getline(cin >> ws, s);
  31. return s;
  32. }
  33.  
  34. // Theo de struct Account co 4 truong; them mang locked[] de luu trang thai khoa.
  35. struct Account { int id; string username, password, role; };
  36. int findIndex(Account* ds,int n,int id){
  37. for(int i=0;i<n;++i)if(ds[i].id==id)return i;
  38. return -1;
  39. }
  40. void input(Account* ds,bool* locked,int n){
  41. for(int i=0;i<n;++i){
  42. cout<<"\nTai khoan "<<i+1<<":\n";
  43. while(true){if(!readInt("ID: ",ds[i].id))return;
  44. if(findIndex(ds,i,ds[i].id)==-1)break;
  45. cout<<"ID trung!\n";}
  46. while(true){
  47. ds[i].username=readLine("Username: ");
  48. bool duplicate=false;
  49. for(int j=0;j<i;++j)if(ds[j].username==ds[i].username)duplicate=true;
  50. if(!duplicate&&!ds[i].username.empty())break;
  51. cout<<"Username trung hoac rong!\n";
  52. }
  53. ds[i].password=readLine("Password (demo): ");
  54. while(true){ds[i].role=readLine("Role (admin/user): ");
  55. if(ds[i].role=="admin"||ds[i].role=="user")break;
  56. cout<<"Role khong hop le!\n";}
  57. locked[i]=false;
  58. }
  59. }
  60. int login(Account* ds,bool* locked,int n){
  61. string username=readLine("Username: ");
  62. string password=readLine("Password: ");
  63. for(int i=0;i<n;++i){
  64. if(ds[i].username==username&&ds[i].password==password){
  65. if(locked[i]){cout<<"Tai khoan da bi khoa!\n";return -1;}
  66. return i;
  67. }
  68. }
  69. cout<<"Dang nhap that bai!\n";return -1;
  70. }
  71. void showAll(Account* ds,bool* locked,int n,int currentIndex){
  72. if(ds[currentIndex].role!="admin"){
  73. cout<<"Chi admin xem danh sach tai khoan!\n";return;
  74. }
  75. for(int i=0;i<n;++i){
  76. cout<<ds[i].id<<" | "<<ds[i].username<<" | "<<ds[i].role
  77. <<" | "<<(locked[i]?"LOCKED":"ACTIVE")<<'\n';
  78. // Khong hien thi password.
  79. }
  80. }
  81. void viewAccount(Account* ds,int n,int currentIndex){
  82. int id;if(!readInt("ID can xem: ",id))return;
  83. int idx=findIndex(ds,n,id);
  84. if(idx==-1){cout<<"Khong tim thay!\n";return;}
  85. if(ds[currentIndex].role!="admin"&&idx!=currentIndex){
  86. cout<<"Khong duoc xem tai khoan nguoi khac!\n";return;
  87. }
  88. cout<<"ID: "<<ds[idx].id<<" | Username: "<<ds[idx].username
  89. <<" | Role: "<<ds[idx].role<<'\n';
  90. }
  91. void changePassword(Account* ds,int currentIndex){
  92. // User va admin chi duoc doi mat khau cua CHINH MINH.
  93. string old=readLine("Mat khau hien tai: ");
  94. if(old!=ds[currentIndex].password){cout<<"Mat khau cu sai!\n";return;}
  95. string fresh=readLine("Mat khau moi: ");
  96. if(fresh.empty()){cout<<"Khong duoc rong!\n";return;}
  97. ds[currentIndex].password=fresh;
  98. cout<<"Da doi mat khau!\n";
  99. }
  100. void lockAccount(Account* ds,bool* locked,int n,int currentIndex){
  101. if(ds[currentIndex].role!="admin"){
  102. cout<<"Chi admin duoc khoa tai khoan!\n";return;
  103. }
  104. int id;if(!readInt("ID can khoa: ",id))return;
  105. int idx=findIndex(ds,n,id);
  106. if(idx==-1){cout<<"Khong tim thay!\n";return;}
  107. if(idx==currentIndex){cout<<"Khong khoa tai khoan dang su dung!\n";return;}
  108. locked[idx]=true;cout<<"Da khoa tai khoan!\n";
  109. }
  110. int main(){
  111. int n;if(!readInt("Nhap n (1..1000): ",n)||n<1||n>1000)return 0;
  112. Account* ds=new Account[n];bool* locked=new bool[n];
  113. input(ds,locked,n);
  114. int mainChoice=-1;
  115. do{
  116. if(!readInt("\n1.Dang nhap 0.Thoat: ",mainChoice))break;
  117. if(mainChoice==1){
  118. int currentIndex=login(ds,locked,n);
  119. if(currentIndex<0)continue;
  120. int choice=-1;
  121. do{
  122. if(!readInt("\n1.Danh sach 2.Xem tai khoan 3.Doi password 4.Khoa tai khoan 0.Dang xuat: ",choice))break;
  123. switch(choice){
  124. case 1:showAll(ds,locked,n,currentIndex);break;
  125. case 2:viewAccount(ds,n,currentIndex);break;
  126. case 3:changePassword(ds,currentIndex);break;
  127. case 4:lockAccount(ds,locked,n,currentIndex);break;
  128. case 0:break;
  129. default:cout<<"Lua chon sai!\n";
  130. }
  131. }while(choice!=0&&!cin.eof());
  132. }else if(mainChoice!=0)cout<<"Lua chon sai!\n";
  133. }while(mainChoice!=0&&!cin.eof());
  134. delete[] ds;ds=nullptr;
  135. delete[] locked;locked=nullptr;
  136. return 0;
  137. }
  138.  
  139.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Nhap n (1..1000):