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 Product { int id; string name; double price; int quantity; };
  35. Product* findProduct(Product* products,int n,int id) {
  36. for(int i=0;i<n;++i) if(products[i].id==id) return &products[i];
  37. return nullptr;
  38. }
  39. void input(Product* ds,int n) {
  40. for(int i=0;i<n;++i) {
  41. cout<<"\nSan pham "<<i+1<<":\n";
  42. while(true) {
  43. if(!readInt("ID: ",ds[i].id)) return;
  44. if(findProduct(ds,i,ds[i].id)==nullptr) break;
  45. cout<<"ID trung!\n";
  46. }
  47. ds[i].name=readLine("Ten: ");
  48. do {if(!readDouble("Gia: ",ds[i].price)) return;
  49. if(ds[i].price<0)cout<<"Gia khong duoc am!\n";
  50. }while(ds[i].price<0);
  51. do {if(!readInt("So luong: ",ds[i].quantity)) return;
  52. if(ds[i].quantity<0)cout<<"So luong khong duoc am!\n";
  53. }while(ds[i].quantity<0);
  54. }
  55. }
  56. void show(Product* ds,int n) {
  57. for(int i=0;i<n;++i) cout<<ds[i].id<<" | "<<ds[i].name
  58. <<" | Gia: "<<ds[i].price<<" | Ton: "<<ds[i].quantity<<'\n';
  59. }
  60. void search(Product* ds,int n) {
  61. int id;if(!readInt("ID tim: ",id))return;
  62. Product* p=findProduct(ds,n,id);
  63. if(!p){cout<<"Khong tim thay!\n";return;}
  64. cout<<p->id<<" | "<<p->name<<" | "<<p->price<<" | "<<p->quantity<<'\n';
  65. }
  66. void buy(Product* ds,int n) {
  67. int id,k;if(!readInt("ID mua: ",id))return;
  68. Product* p=findProduct(ds,n,id);
  69. if(p==nullptr){cout<<"San pham khong ton tai!\n";return;}
  70. if(!readInt("So luong mua k: ",k))return;
  71. if(k<=0||k>p->quantity){cout<<"So luong mua khong hop le!\n";return;}
  72. p->quantity-=k;
  73. cout<<"Mua thanh cong! Con "<<p->quantity<<'\n';
  74. }
  75. void setPrice(Product* ds,int n,bool isManager) {
  76. if(!isManager){cout<<"User khong duoc doi gia!\n";return;}
  77. int id;if(!readInt("ID sua gia: ",id))return;
  78. Product* p=findProduct(ds,n,id);
  79. if(!p){cout<<"Khong tim thay!\n";return;}
  80. double price;if(!readDouble("Gia moi: ",price))return;
  81. if(price<0){cout<<"Gia khong hop le!\n";return;}
  82. p->price=price;cout<<"Da cap nhat gia!\n";
  83. }
  84. bool login(bool& isManager) {
  85. int role;if(!readInt("1.Manager 2.User: ",role))return false;
  86. if(role==2){isManager=false;return true;}
  87. if(role==1){string pass=readLine("Mat khau manager: ");
  88. if(pass=="M123"){isManager=true;return true;}}
  89. cout<<"Dang nhap that bai!\n";return false;
  90. }
  91. int main(){
  92. int n;if(!readInt("Nhap n (1..1000): ",n)||n<1||n>1000)return 0;
  93. Product* ds=new Product[n];input(ds,n);
  94. int mainChoice=-1;
  95. do {
  96. if(!readInt("\n1.Dang nhap 0.Thoat: ",mainChoice))break;
  97. if(mainChoice==1){bool isManager=false;
  98. if(!login(isManager))continue;
  99. int choice=-1;
  100. do{
  101. if(!readInt("\n1.Hien thi 2.Tim 3.Mua 4.Sua gia 0.Dang xuat: ",choice))break;
  102. switch(choice){
  103. case 1:show(ds,n);break;
  104. case 2:search(ds,n);break;
  105. case 3:buy(ds,n);break;
  106. case 4:setPrice(ds,n,isManager);break;
  107. case 0:break;
  108. default:cout<<"Lua chon sai!\n";
  109. }
  110. }while(choice!=0&&!cin.eof());
  111. }else if(mainChoice!=0)cout<<"Lua chon sai!\n";
  112. }while(mainChoice!=0&&!cin.eof());
  113. delete[] ds;ds=nullptr;return 0;
  114. }
  115.  
  116.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Nhap n (1..1000):