Advertisement

c++实现医院排队看病系统

阅读量:

使用(数据结构,链表)

如何实现病人到医院看病,需要排队等候,先到先看。请编写程序模拟病人看病的过程。

(1)后到的病人必修排在最后面;

(2)排队过程中的病人可以随时查看自己前面还有多少病人等待看病;

(3)系统能提示正在看病的后面一个病人作好准备。

以下是使用C++语言实现医院排队看病系统的示例代码。代码中使用了链表数据结构来存储病人信息,并按照先进先出的顺序排列。具体实现包括病人结构体、链表节点结构体、链表类以及主函数等部分。

复制代码
 #include <iostream>

    
 #include <string>
    
  
    
 using namespace std;
    
  
    
 // 病人结构体
    
 struct Patient {
    
     string name;
    
     string condition;
    
     int waitTime;
    
 };
    
  
    
 // 链表节点结构体
    
 struct ListNode {
    
     Patient* patient;
    
     ListNode* next;
    
 };
    
  
    
 // 链表类
    
 class LinkedList {
    
 public:
    
     LinkedList() {
    
     head = nullptr;
    
     length = 0;
    
     }
    
  
    
     ~LinkedList() {
    
     ListNode* p = head;
    
     while (p != nullptr) {
    
         ListNode* temp = p;
    
         p = p->next;
    
         delete temp->patient;
    
         delete temp;
    
     }
    
     }
    
  
    
     void addPatient(Patient* patient) {
    
     ListNode* newNode = new ListNode();
    
     newNode->patient = patient;
    
     newNode->next = nullptr;
    
     if (head == nullptr) {
    
         head = newNode;
    
     } else {
    
         ListNode* p = head;
    
         while (p->next != nullptr) {
    
             p = p->next;
    
         }
    
         p->next = newNode;
    
     }
    
     length++;
    
     }
    
  
    
     Patient* removePatient() {
    
     if (head == nullptr) {
    
         return nullptr;
    
     }
    
     ListNode* temp = head;
    
     head = head->next;
    
     Patient* patient = temp->patient;
    
     delete temp;
    
     length--;
    
     return patient;
    
     }
    
  
    
     int getLength() {
    
     return length;
    
     }
    
  
    
     Patient* getPatient(int index) {
    
     if (index < 0 || index >= length) {
    
         return nullptr;
    
     }
    
     ListNode* p = head;
    
     for (int i = 0; i < index; i++) {
    
         p = p->next;
    
     }
    
     return p->patient;
    
     }
    
  
    
     void printPatients() {
    
     ListNode* p = head;
    
     while (p != nullptr) {
    
         cout << "Name: " << p->patient->name << ", Condition: " << p->patient->condition << ", Wait Time: " << p->patient->waitTime << endl;
    
         p = p->next;
    
     }
    
     }
    
  
    
 private:
    
     ListNode* head;
    
     int length;
    
 };
    
  
    
 int main() {
    
     LinkedList queue;
    
  
    
     while (true) {
    
     cout << "Enter command (add, remove, length, show, exit): ";
    
     string command;
    
     cin >> command;
    
   101.         if (command == "add") {
    
         cout << "Enter name: ";
    
         string name;
    
         cin >> name;
    
         cout << "Enter condition: ";
    
         string condition;
    
         cin >> condition;
    
         Patient* patient = new Patient();
    
         patient->name = name;
    
         patient->condition = condition;
    
         patient->waitTime = queue.getLength();
    
         queue.addPatient(patient);
    
     } else if (command == "remove") {
    
         Patient* patient = queue.removePatient();
    
         if (patient == nullptr) {
    
             cout << "Queue is empty" << endl;
    
         } else {
    
             cout << "Name: " << patient->name << ", Condition: " << patient->condition << ", Wait Time: " << patient->waitTime << endl;
    
             delete patient;
    
         }
    
     } else if (command == "length") {
    
         cout << "Queue length: " << queue.getLength() << endl;
    
     } else if (command == "show") {
    
         queue.printPatients();
    
     } else if (command == "exit") {
    
         break;
    
     } else {
    
         cout << "Invalid command" << endl;
    
     }
    
     }
    
  
    
     return 0;
    
 }
    
    
    
    
    代码解读

在主函数中,使用循环来不断读取用户输入,并根据用户输入的指令调用链表类中相应的方法来完成相应的操作。同时,还考虑了一些边界情况,例如链表为空时无法删除病人等。

全部评论 (0)

还没有任何评论哟~