#include<iostream>
#include<stdlib.h>
using namespace std;
//优先级队列数组元素的个数
const int MaxPQSize = 50;
template<class T>
class PQueue{
private:
int count;
T pqlist[MaxPQSize];
public:
PQueue(void);
void PQInsert(const T& item);
T PQDelete(void);
void ClearPQ(void);
//检测优先级队列状态操作
int PQEmpty(void)const;
int PQFull(void)const;
int PQLength(void)const;
};
template <class T>
PQueue<T>::PQueue(){
count = 0;
}
template<class T>
int PQueue<T>::PQEmpty() const{
return count==0;
}
template<class T>
void PQueue<T>::PQInsert(const T& item){
if(count == MaxPQSize){
cerr<<"队列已经溢出了!"<<endl;
}
//将元素置于队尾并使count+1
pqlist[count] = item;
count++;
}
//将尾元素放进最大优先级元素处 count--
template<class T>
T PQueue<T>::PQDelete(){
T min;
int i,minindex = 0;
if(count > 0){
//在pqlist中找到最小值极其下标
min = pqlist[0];//假设第一个元素为最小值
//顺序访问元素 修改最小值 及下标
for(i = 1; i < count; i++)
if(pqlist[i] < min){
min = pqlist[i];
minindex = i;
}
//将尾元素移入最小元素处并将count减一
pqlist[minindex] = pqlist[count-1];
count--;
}else{
cerr<<"deleting from an empty pqueue"<<endl;
}
return min;//返回最小值
}
//应用
#include <iostream>
#include<iomanip>
#include<fstream>
#include"PQueue.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//定义请求单的记录
enum Staff{Manager,Supervisor,Worker
};//员工类型 Manager 优先级最高
struct JobRequest
{
Staff staffPerson;
int jobid;
int jobTime;
};//工作状态
int operator<(const JobRequest& a,const JobRequest& b){
return a.staffPerson < b.staffPerson;
}
void PrintJobInfo(JobRequest PR){
switch(PR.staffPerson){
case Manager:
cout <<"Manager ";
break;
case Supervisor:
cout<<"Supervisor ";
break;
case Worker:
cout<<"Worker ";
break;
}
cout<<PR.jobid <<" "<<PR.jobTime<<endl;
}
void PrintJobSummary(int jobServicesUse[]){
cout<<"\nTotal Support Usage\n";
cout<<" Maager "<<setw(3)
<<jobServicesUse[0]<<setw(3)<<endl;
cout<<" Supervisor "<<setw(3)<<jobServicesUse[1]<<endl;
cout<<" Worker "<<setw(3)<<jobServicesUse[2]<<endl;
}
int main(int argc, char** argv) {
//处理不超过50个服务请求单
PQueue<JobRequest> jobpool;
//从fin中读入服务请求单
ifstream fin;
//为每类员工服务的时间
int jobServicesUse[3]={0,0,0};
JobRequest PR;
char ch;
//打开输入文件 job.dat 若失败则退出程序
fin.open("job.dat",ios::in);
if(!fin){
cerr<<"cannot open file job.dat"<<endl;
}
//从文件读入每个请求并插入到优先队列jobpool中
//每行开始为表明员工类别的字符
while(fin>>ch){
switch(ch){
case‘M‘: PR.staffPerson = Manager;
break;
case‘S‘: PR.staffPerson = Supervisor;
break;
case‘W‘: PR.staffPerson = Worker;
break;
default: break;
}
//读入PR的jobid 和jobtime
fin>>PR.jobid;
fin>>PR.jobTime;
//将PR插入到优先级队列
cout<<PR.jobid<<"****"<<endl;
jobpool.PQInsert(PR);
}
//从优先级队列中删除服务并输出该服务的信息
cout<<"Category Job ID Job Time"<<endl;
while(!jobpool.PQEmpty()){
PR = jobpool.PQDelete();
PrintJobInfo(PR);
//对每类员工累计服务时间
jobServicesUse[int(PR.staffPerson)]+=PR.jobTime;
}
PrintJobSummary(jobServicesUse);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。