患者到医院看病的顺序是:先排队等候,再看病治疗。要求设计一个算法,模拟病人等候就诊的过程。 其中:“病人到达”用命令“A”(或“a”)表示,“护士让下一位就诊”用“N”(或“n”)表示,“不再接收病人排队”用“S”(或“s”)表示。输入:
A(或a) 病历号
N(或n)
S(或s)
x(其它字符)
病历号为x的病人就诊
若队列中有数据,输出队首元素,否则输出“无病人就诊”
不再接收病人排队,并输出当前队列中所有病历号
输入命令不合法!
A
123
n
n
a
124
a
125
x
s
病历号为123的病人就诊
无病人就诊
输入命令不合法!
今天不再接收病人排队,下列排队的病人依次就诊:124 125
基本思路:这是一个简单的队列的运用,我是自己写的队列。也可用
#includeusing namespace std; class Queue { public: string num; int maxSize; string* data; int front; int rear; Queue(int sz = 1000) { front = rear = 0; maxSize = sz; data = new string[maxSize]; } bool IsEmpty() { if (rear == front) return true; else return false; } bool EnStack(string str) { data[rear] = str; rear++; return true; } string getTop() { if (front == rear) return "!"; else return data[front]; } bool DeStack() { if (IsEmpty()) return false; front++; return true; } }; int main() { Queue s; char cho; while (true) { cin >> cho; if (cho == 'a' || cho == 'A') { string n; cin >> n; s.EnStack(n); } else if (cho == 'n' || cho == 'N') { string n = s.getTop(); if (n == "!") cout << "无病人就诊" << endl; else { cout << "病历号为" << n << "的病人就诊" << endl; s.DeStack(); } } else if (cho == 's' || cho == 'S') { cout << "今天不再接收病人排队,下列排队的病人依次就诊:" ; for (int i = s.front; i < s.rear; i++) cout << s.data[i] << " "; cout << endl; return 0; } else { cout << "输入命令不合法!" << endl; } } return 0; }
欢迎交流
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)