1. 程式人生 > >先來先服務排程演算法c++版

先來先服務排程演算法c++版

作業名                 到達時間                     服務時間

a                         0                                   6

b                         2                                  50

c                        5                                   20

d                        5                                    10

e                         12                                 40

f                        15                                   8




#include "stdafx.h"
#include<iostream>

#include<iomanip>
#include<queue>
#include<string>

using namespace std;

class JOB//類
{
public:
 string  name; //程序名
 float  arrivetime;//到達時間
 float servicestime;//服務時間
 float starttime;  //開始時間
 float finishtime;//完成時間
 float zztime; //週轉時間
 float dqzztime;  //帶權週轉時間
 void Show()//類方法
    {
        cout.width(9);   
  cout<<name;
        cout.width(9);   
  cout<<arrivetime;
        cout.width(11);  
  cout<<servicestime;
        cout.width(13);  
  cout<<starttime;
        cout.width(10);  
  cout<<finishtime;
        cout.width(10);  
  cout<<zztime;
        cout.width(9);   
  cout<<dqzztime<<endl;
    }

 };
int input(int num)
{
    //cout<<"(FCFS)請輸入作業數目:";
    //cin>>num;
 queue<JOB>  jobQue;
 JOB job1;
 cout<<"\n請輸入作業名,到達時間,執行時間:";
 cin>>job1.name>>job1.arrivetime>>job1.servicestime;
 job1.starttime=job1.arrivetime;
 job1.finishtime=job1.starttime+job1.servicestime;
 job1.zztime=job1.finishtime-job1.arrivetime;
 job1.dqzztime=job1.zztime/job1.servicestime;
 jobQue.push(job1);
 JOB job2;
 for (int i=2; i<=num; i++)
 {
  job2=jobQue.back();
  cout<<"\n請輸入作業名,到達時間,執行時間:";
  cin>>job1.name>>job1.arrivetime>>job1.servicestime;
  job1.starttime=job2.finishtime;
  job1.finishtime=job1.starttime+job1.servicestime;
  job1.zztime=job1.finishtime-job1.arrivetime;
  job1.dqzztime=job1.zztime/job1.servicestime;
  jobQue.push(job1);
 }
 //先來先服務
 cout<<"\n先來先服務演算法(FCFS):"<<endl;
 cout<<"\n作業名  到達時間  服務時間  開始執行時間  完成時間  週轉時間  帶權週轉時間"<<endl;
    double sum_dq=0;
 double sum_pj=0;
    cout.setf(ios::left);
    while(!jobQue.empty())
    {
        job1=jobQue.front();
        job1.Show();
        //sum+=(double)job1.zztime/job1.arrivetime;
  sum_dq+=(double)job1.dqzztime;
  sum_pj+=(double)job1.zztime;
        jobQue.pop();
    }
    cout<<"\n先來先服務演算法(FCFS)平均帶權週轉時間:"<<sum_dq/num<<endl;
 cout<<"\n先來先服務演算法(FCFS)平均週轉時間:"<<sum_pj/num<<endl;
 return 0;

};

int main(int argc, char* argv[])
{
 int job_count;
 cout<<"請輸入一組作業的數:";
 cin>>job_count;
 input(job_count);
 return 0;
}