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. struct Patient { int id; string name; int age; string diagnosis; };
  35. Patient* findPatient(Patient* ds,int n,int id){
  36. for(int i=0;i<n;++i)if(ds[i].id==id)return &ds[i];
  37. return nullptr;
  38. }
  39. void input(Patient* ds,int n){
  40. for(int i=0;i<n;++i){
  41. cout<<"\nBenh nhan "<<i+1<<":\n";
  42. while(true){if(!readInt("ID: ",ds[i].id))return;
  43. if(findPatient(ds,i,ds[i].id)==nullptr)break;
  44. cout<<"ID trung!\n";}
  45. ds[i].name=readLine("Ho ten: ");
  46. do {if(!readInt("Tuoi: ",ds[i].age))return;
  47. if(ds[i].age<0||ds[i].age>150)cout<<"Tuoi khong hop le!\n";
  48. }while(ds[i].age<0||ds[i].age>150);
  49. ds[i].diagnosis=readLine("Chan doan: ");
  50. }
  51. }
  52. void findAndShow(Patient* ds,int n,bool isDoctor){
  53. int id;if(!readInt("ID can tim: ",id))return;
  54. Patient* p=findPatient(ds,n,id);
  55. if(p==nullptr){cout<<"Khong tim thay!\n";return;}
  56. cout<<"ID: "<<p->id<<" | Ten: "<<p->name<<" | Tuoi: "<<p->age<<'\n';
  57. if(isDoctor)cout<<"Diagnosis: "<<p->diagnosis<<'\n';
  58. }
  59. void updateDiagnosis(Patient* ds,int n,bool isDoctor){
  60. if(!isDoctor){cout<<"Chi doctor duoc sua diagnosis!\n";return;}
  61. int id;if(!readInt("ID can sua: ",id))return;
  62. Patient* p=findPatient(ds,n,id);
  63. if(!p){cout<<"Khong tim thay!\n";return;}
  64. p->diagnosis=readLine("Diagnosis moi: ");
  65. cout<<"Da cap nhat!\n";
  66. }
  67. bool login(bool& isDoctor){
  68. int role;if(!readInt("1.Doctor 2.User: ",role))return false;
  69. if(role==2){isDoctor=false;return true;}
  70. if(role==1){string pass=readLine("Mat khau doctor: ");
  71. if(pass=="D123"){isDoctor=true;return true;}}
  72. cout<<"Dang nhap that bai!\n";return false;
  73. }
  74. int main(){
  75. int n;if(!readInt("Nhap n (1..1000): ",n)||n<1||n>1000)return 0;
  76. Patient* ds=new Patient[n];input(ds,n);
  77. int mainChoice=-1;
  78. do{
  79. if(!readInt("\n1.Dang nhap 0.Thoat: ",mainChoice))break;
  80. if(mainChoice==1){bool isDoctor=false;
  81. if(!login(isDoctor))continue;
  82. int choice=-1;
  83. do{
  84. if(!readInt("\n1.Tim/xem benh nhan 2.Cap nhat diagnosis 0.Dang xuat: ",choice))break;
  85. switch(choice){
  86. case 1:findAndShow(ds,n,isDoctor);break;
  87. case 2:updateDiagnosis(ds,n,isDoctor);break;
  88. case 0:break;
  89. default:cout<<"Lua chon sai!\n";
  90. }
  91. }while(choice!=0&&!cin.eof());
  92. }else if(mainChoice!=0)cout<<"Lua chon sai!\n";
  93. }while(mainChoice!=0&&!cin.eof());
  94. delete[] ds;ds=nullptr;return 0;
  95. }
  96.  
  97.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Nhap n (1..1000):