我要解决的问题:
如何在SJF排序算法中建立条件,我想先进行排序直到中值,然后再进行fifo。
include_iostream
include_iomanip
using namespace std;
class sjf_alg
{
int exe[10],id[10];
int n;
void sort(int *,int*);
public:
void getdata();
void display();
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of "<<i+1<<" process : ";
cin>>exe[i];
id[i]=i+1;
}
}
void sjf_alg::display()
{
cout<<endl<<"Process ID\tExecution time"<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(15)<<exe[i]<<setw(15)<<endl;
}
void sjf_alg::sort(int *f,int *l)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=l[z];
l[z]=l[z+1];
l[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int wt=0,tnt=0,numbers[250000];;
float avg=0,avtnt=0;
sort(exe,id);
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
for(int i=0;i<n;i++)
{
tnt+=exe[i];
avtnt+=tnt;
avg+=wt;
cout<<setw(5)<<id[i]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
wt+=exe[i];
numbers[i] = avg;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
cout << "Memory used: " << sizeof(numbers)/1024 << "kb\n";
cin.get();
}
int main()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
return 0;
}
输出:
进程ID
5
4
3
2
1
我想让条件一直进行到中位数为止 所以输出将是:
进程ID
5 ----- | SJF
4 ----- | SJF
3 ----- | SJF x ----->这是中位数
1 --- |先进先出
2 --- |先进先出