在写时间片轮转算法时出错,求解答
#include<iostream>#include<string>
#include<iomanip>
using namespace std;
//==========================
struct PCB
{
string name;
int ArrivalTime;
int ServiceTime;
int FinishTime;
int WholeTime;
int needTime;
double WeightWholeTime;
double AverageWT,AverageWWT;
//bool Finished;
};
struct Queue
{
int size; //数组的大小
int head, tail; //队列的头和尾下标
PCB *item; //数组头指针
};
//Queue 类型别名
void input(PCB *&p,int &n)
{
cout<<"请输入各个进程的名字,到达时间和服务时间"<<endl;
for(int i=0;i<n;i++)
{
cin>>p[i].name>>p[i].ArrivalTime>>p[i].ServiceTime;
p[i].needTime=p[i].ServiceTime;
}
}
//循环队列的操作
void InitQueue(Queue *&q,int &n)
{
q->size =n ;
q->item = new PCB[n]; //分配内存
//unhandled exception in 1.exe:0xC0000005:Access Violation
q->tail = 0;
q->head = 0;
}
//==============================================
void EnQueue(Queue *&q, const PCB &key)//入队
{
int tail = (q->tail+1) % q->size; //取余保证,当quil=queuesize-1时,再转回0
if (tail == q->head) //此时队列没有空间
{
cout<<"the queue has been filled full!"<<endl;
}
else
{
q->item[tail] = key;
q->tail = tail;
}
}
//====================================================
PCB DeQueue(Queue *&q) //出队
{
PCB tmp;
if(q->tail == q->head) //判断队列不为空
{
cout<<"the queue is NULL"<<endl;
}
else
{
tmp = q->item[q->head];
q->head = (q->head+1) % q->size;
}
return tmp;
}
//====================================
bool IsQueueEmpty(Queue *q)
{
if(q->head == q->tail)
{
return true;
}
else
{
return false;
}
}
//===================================
bool IsQueueFull(Queue *q)
{
if((q->tail+1)% q->size == q->head)
{
return true;
}
else
{
return false;
}
}//==================================
void insert_sort(PCB *&p,int n)
{
int i,j;
PCB temp;
for(i=1;i<n;i++)
{
temp=p[i];//赋值
j=i-1;
while(j>=0&&temp.ArrivalTime<p[j].ArrivalTime)
{
p[j+1]=p[j];
j--;
}
p[j+1]=temp;
}
cout<<"输出排序后的各个进程的名字,到达时间和服务时间"<<endl;
for(i=0;i<n;i++)
{
cout<<p[i].name<<" "<<p[i].ArrivalTime<<" "<<p[i].ServiceTime<<endl;
}
}
//==============================================
void print(PCB &p)
{
cout<<setiosflags(ios::left)<<"进程:"<<" "<<setw(10)<<p.name<<"到达时间为:"<<" "<<setw(10)<<p.ArrivalTime<<
"服务时间为:"<<" "<<setw(10)<<p.ServiceTime<<"完成时间为:"<<" "<<setw(10)<<p.FinishTime<<
"周转时间为:"<<" "<<setw(10)<<p.WholeTime<<"带权周转时间为:"<<" "<<setw(10)<<p.WeightWholeTime<<endl;
}
//==========================================
//时间片轮转算法
void RR(Queue *&q,int &PServiceTime)
{
PCB temp;
int time=0;
double sumWT=0,sumWWT=0;
while(!IsQueueEmpty(q))
{
temp=DeQueue(q);
if(temp.needTime>PServiceTime)
{
temp.needTime=temp.needTime-PServiceTime;
time+=PServiceTime;
EnQueue(q,temp);//再次入队
}
else
{
temp.FinishTime=time+temp.needTime;
temp.WholeTime=temp.FinishTime-temp.ArrivalTime;
temp.WeightWholeTime=temp.WholeTime/temp.ServiceTime;
sumWT+=temp.WholeTime;
sumWWT+=temp.WeightWholeTime;
time+=temp.needTime;
print(temp);
}
}
}
//=================================
int main()
{
int n,i,PServiceTime;
cout<<"请输入进程的个数"<<endl;
cin>>n;
if(n<=0)
{
cout<<"进程个数不合法,请重新输入"<<endl;
exit(1);
}
PCB *p=new PCB[n];//进程
input(p,n);
cout<<"请输入时间片的大小"<<endl;
cin>>PServiceTime;
insert_sort(p,n);
Queue *q;//队列
InitQueue(q,n);
for(i=0;i<n;i++)
EnQueue(q, p[i]); //入队
RR(q,PServiceTime);
return 1;
}