📄 pex13_5.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "pqueue.h"
#include "strclass.h"
#include "random.h"
struct ProcessRequestRecord
{
int priority;
String name;
};
// compare two process records by priority. needed
// by the heap implementation of the priority queue
int operator<= (const ProcessRequestRecord& x,
const ProcessRequestRecord& y)
{
return x.priority <= y.priority;
}
// output a process record
ostream& operator<< (ostream& ostr, const ProcessRequestRecord& prec)
{
ostr << prec.name << " " << prec.priority;
return ostr;
}
void main(void)
{
RandomNumber rnd;
int i;
ProcessRequestRecord procrec;
// priority queue of process records
PQueue<ProcessRequestRecord> pq(10);
cout << "Processes prior to insertion into the priority queue:" << endl << endl;
// generate 10 process records, print them and insert each
// into the priority queue
procrec.name = "Process X";
for(i=0;i < 10;i++)
{
// priority 0..39. 0 is the highest, 39 the lowest
procrec.priority = rnd.Random(40);
// process names are "Process A" ... "Process J"
procrec.name[8] = 'A'+i;
cout << procrec << endl;
pq.PQInsert(procrec);
}
cout << endl << endl;
// delete the records from the priority queue and print each
cout << "Processes upon deletion from the priority queue:" << endl << endl;
while(!pq.PQEmpty())
cout << pq.PQDelete() << endl;
}
/*
<Run>
Processes prior to insertion into the priority queue:
Process A 32
Process B 25
Process C 26
Process D 38
Process E 29
Process F 13
Process G 27
Process H 19
Process I 38
Process J 27
Processes upon deletion from the priority queue:
Process F 13
Process H 19
Process B 25
Process C 26
Process G 27
Process J 27
Process E 29
Process A 32
Process D 38
Process I 38
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -