#include<iostream>
#include<string>
using namespace std;
#include<fstream>
typedef struct node//information contained in each patient(每个病人所包含的信息)
{
string name;//姓名
int sex;//性别
int no;//medical record number(病历号)
node* next;
}node;
//queueed patients are stored in queues(排队的病人用队列储存)
typedef struct
{
node data;//patient information(病人的信息)
node* front;//队头
node* rear;//队尾
}queue;
//the diagnosed patients are stored in the stack(诊断完的病人用栈储存)
typedef struct
{
node data;//patient information(病人的信息)
node* top;//stack top(栈顶)
}stack;
class Treatment
{
private:
queue q;
stack s;
int no;//medical record number(病历号)
public:
Treatment()//initialization(初始化)
{
//no header node(没头结点)
q.rear = q.front=NULL;
no = 0;
s.top = NULL;
}
void LineUp()//排队
{
while (1)//realize continuous input of information(实现连续输入信息)
{
node* temp;
temp = new node;
temp->next = NULL;
cout << "姓名" << endl;
cin >> temp->name;
cout << "性别" << endl;
cout << "1,男;2,女" << endl;
do
{
cin >> temp->sex;
if (temp->sex != 1 &&temp->sex != 2)
cout << "输入有误,请重新输入" << endl;
else break;
} while (1);
temp->no = ++no;
if (q.front == NULL)
{
q.front = temp;
q.rear = q.front;
}
q.rear->next = temp;
q.rear = temp;
cout << "输入完毕,是否继续输入下一个排队病人的信息" << endl;
cout << "Y:继续;N:不了" << endl;
char choice;
while (1)
{
cin >> choice;
if (choice != 'Y' && choice != 'N')
cout << "选择有误" << endl;
else break;
}
if (choice == 'N')
{
q.rear->next = NULL;
return;
}
}
}
void diagnosis()//诊断
{
if (q.front == NULL)
{
cout << "暂时没有看病的病人" << endl;
return;
}
node* temp=q.front;
while (temp)
{
cout << temp->no << " 号病人" << "开始就诊" << endl;
cout << "姓名 " << temp->name << endl;
cout << "性别" << (temp->sex == 1 ? "男" : "女") << endl;
cout << "输入OK诊治完毕" << endl;
string flag;
while (1)
{
cin >> flag;
if (flag == "OK")
{
cout << temp->no << " 号病人" << "诊治完毕" << endl;
cout << "姓名 " << temp->name << endl;
cout << "性别" << (temp->sex == 1 ? "男" : "女") << endl;
//enter the stack(进栈)
if (s.top == NULL)
{
s.top = temp;
s.top->next = NULL;
}
else
{
temp->next = s.top->next;
s.top->next = temp;
}
//to delete the information of the patient in the queue
//诊治完要删除该病人在队列里的信息
node* t = temp->next;
q.front = t;
temp = t;
break;
}
else cout << "指令有误,请重新输入" << endl;
}
cout << "是否继续诊断下一位病人" << endl;
char choice;
cout << "Y:是;N:否" << endl;
while (1)
{
cin >> choice;
if (choice == 'N')
{
return;
}
else if (choice == 'Y')
{
temp = temp->next;
break;
}
else cout << "输入指令有误请重新输入" << endl;
}
}
}
void QueueSituation()//view queuing(查看排队情况)
{
node* temp = q.front;
if (temp == NULL)
{
cout << "队列为空" << endl;
return;
}
for (; temp; temp = temp->next)
{
cout<<temp->no<<'\t' << temp->name << '\t' << (temp->sex == 1 ? "男" : "女") << endl;
}
}
void StackSituation()//view queuing(查看诊治完的病人)
{
node* temp = s.top;
if (temp == NULL)
{
cout << "还没诊治过病人" << endl;
return;
}
for (; temp; temp = temp->next)
cout << temp->no << " " << temp->name << " "<< (temp->sex == 1?"男":"女") << endl;
}
void SaveInformation()//save diagnosis and treatment(保存诊治情况)
{
node* temp = s.top;
if (temp == NULL)
{
cout << "暂无诊治情况" << endl;
return;
}
ofstream ofs;
ofs.open("诊治表", ios::out);
if (!ofs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
for (; temp; temp = temp->next)
ofs << temp->no << '\t' << temp->name << '\t' << (temp->sex == 1 ? "男" : "女") << endl;
cout << "保存完毕" << endl;
ofs.close();
}
void ViewInformation()
{
ifstream ifs;//读文件流
ifs.open("诊治表",ios::in);//为读文件而打开文件
string buf;
if (!ifs.is_open())
{
cout << "打开文件失败,该功能退出" << endl;
return;
}
if (!getline(ifs, buf))cout << "文件为空" << endl;
cout << buf << endl;
while (getline(ifs, buf))
{
cout << buf << endl;
}
ifs.close();
}
};
int main()
{
Treatment treatment;
do
{
cout << "输入想实现的功能" << endl;
cout << "0,下班" << endl
<< "1,排队看病" << endl
<< "2,开始诊治" << endl
<< "3,查看队列情况" << endl
<< "4,查看诊治完成情况" << endl
<< "5,保存诊治信息" << endl
<< "6,查看文件诊治情况" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 0:return 0;
case 1:treatment.LineUp(); break;
case 2:treatment.diagnosis(); break;
case 3:treatment.QueueSituation(); break;
case 4:treatment.StackSituation(); break;
case 5:treatment.SaveInformation(); break;
case 6:treatment.ViewInformation(); break;
default :cout << "输入有误" << endl;
}
system("pause");
system("cls");
} while (1);
}
|