代码:
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<fstream.h>
const int MAXJOB=50; //定义最大作业
//定义数据结构体
typedef struct node{
int number;
int reach_time;
int reach_hour;
int reach_minite;
int need_time;
int privilege;
float excellent;
int start_time;
int wait_time;
int visited;
}job;
job jobs[MAXJOB];
int quantity;
//初始化函数
void initial()
{
int i;
for(i=0;i<MAXJOB;i++){
jobs[i].number=0;
jobs[i].reach_time=0;
jobs[i].reach_hour=0;
jobs[i].reach_minite=0;
jobs[i].privilege=0;
jobs[i].excellent=0;
jobs[i].start_time=0;
jobs[i].wait_time=0;
jobs[i].visited=0;
}
quantity=0;
}
//重置作业数据函数
void reset()
{ int i;
for(i=0;i<MAXJOB;i++){
jobs[i].start_time=0;
jobs[i].wait_time=0;
jobs[i].visited=0;
}
}
//读入作业数据函数
void readData()
{
FILE *fp;
char fname[20];
int i;
cout<<"请输入作业数据文件名:";
strcpy(fname,"8job.txt");
cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
cout<<"错误,文件打不开,请检查文件名:)"<<endl;
}
else{
while(!feof(fp)){
fscanf(fp,"%d %d %d %d",&jobs[quantity].number,&jobs[quantity].reach_time,&jobs[quantity].need_time,&jobs[quantity].privilege);
jobs[quantity].reach_hour=jobs[quantity].reach_time/100;
jobs[quantity].reach_minite=jobs[quantity].reach_time%100;
quantity++;
}
//输出初始作业数据
cout<<"输出初始作业数据"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout.setf(2);
cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(14)<<"所需时间(分)"<<setw(14)<<"优先级(0>1)"<<endl;
for(i=0;i<quantity;i++){
cout<<setw(10)<<jobs[i].number<<setw(12)<<jobs[i].reach_time<<setw(14)<<jobs[i].need_time<<setw(14)<<jobs[i].privilege<<endl;
}
}
}
void FIFO()
{
int i;
int current_hour;
int current_minute;
int total_time=0;
cout<<endl;
cout<<endl<<"FIFO算法作业流"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout.setf(2);
cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(12)<<"开始时间"<<setw(14)<<"周转时间(分)"<<endl;
current_hour=jobs[0].reach_hour;
current_minute=jobs[0].reach_minite;
for(i=0;i<quantity;i++){
jobs[i].start_time=current_hour*100+current_minute;
jobs[i].wait_time=(current_hour-jobs[i].reach_hour)*60+(current_minute-jobs[i].reach_minite)+jobs[i
].need_time;
cout<<setw(10)<<jobs[i].number<<setw(12)<<jobs[i].reach_time<<setw(12)<<jobs[i].start_time<<setw(14)<<jobs[i].wait_time<<endl;
current_hour=current_hour+(jobs[i].need_time+current_minute)/60;
current_minute=(jobs[i].need_time+current_minute)%60;
total_time+=jobs[i].wait_time;
}
cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl;
}
void shorter()
{
int i,j,p;
int current_hour;
int current_minute;
int current_need_time;
int total_time=0;
cout<<endl;
cout<<endl<<"时间短作业优先算法作业流(开始调度时刻为最后一个作业到达系统的时间)"<<endl;
cout<<"------------------------------------------------------------------------"<<endl;
cout.setf(2);
cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(14)<<"所需时间(分)"<<setw(12)<<"开始时间"<<setw(14)<<"周转时间(分)"<<endl;
current_hour=jobs[quantity-1].reach_hour;
current_minute=jobs[quantity-1].reach_minite;
for(i=0;i<quantity;i++){
current_need_time=30000;
for(j=0;j<quantity;j++){
if((jobs[j].visited==0)&&(jobs[j].need_time<current_need_time)){
p=j;
current_need_time=jobs[j].need_time;
}
}
jobs[p].start_time=current_hour*100+current_minute;
jobs[p].wait_time=(current_hour-jobs[p].reach_hour)*60+(current_minute-jobs[p].reach_minite)+jobs[p
].need_time;
cout<<setw(10)<<jobs[p].number<<setw(12)<<jobs[p].reach_time<<setw(14)<<jobs[p].need_time<<setw(12)<<jobs[p].start_time<<setw(14)<<jobs[p].wait_time<<endl;
current_hour=current_hour+(jobs[p].need_time+current_minute)/60;
current_minute=(jobs[p].need_time+current_minute)%60;
jobs[p].visited=1;
total_time+=jobs[p].wait_time;
}
cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl;
}
void excellent()
{
int i,j,p;
int current_hour;
int current_minute;
float current_excellent;
int total_time=0;
//输出作业流
cout<<endl;
cout<<endl<<"响应比高者优先调度算法作业流(开始调度时刻为最后一个作业到达系统的时间)"<<endl;
cout<<"------------------------------------------------------------------------"<<endl;
cout.setf(2);
cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(12)<<"开始时间"<<setw(14)<<"周转时间(分)"<<endl;
current_hour=jobs[quantity-1].reach_hour;
current_minute=jobs[quantity-1].reach_minite;
for(i=0;i<quantity;i++){
current_excellent=-1;
for(j=0;j<quantity;j++){
if(jobs[j].visited==0){
jobs[j].wait_time=(current_hour-jobs[j].reach_hour)*60+(current_minute-jobs[j].reach_minite);
jobs[j].excellent=(float)(jobs[j].wait_time/jobs[j].need_time);
}
}
for(j=0;j<quantity;j++){
if((jobs[j].visited==0)&&(jobs[j].excellent>current_excellent)){
p=j;
current_excellent=jobs[j].excellent;
}
}
jobs[p].start_time=current_hour*100+current_minute;
jobs[p].wait_time=(current_hour-jobs[p].reach_hour)*60+(current_minute-jobs[p].reach_minite)+jobs[p
].need_time;
cout<<setw(10)<<jobs[p].number<<setw(12)<<jobs[p].reach_time<<setw(12)<<jobs[p].start_time<<setw(14)<<jobs[p].wait_time<<endl;
current_hour=current_hour+(jobs[p].need_time+current_minute)/60;
current_minute=(jobs[p].need_time+current_minute)%60;
jobs[p].visited=1;
total_time+=jobs[p].wait_time;
}
cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl;
}
void main()
{
initial();
//readData();
FIFO();
reset();
//shorter();
//excellent();
}
6 个解决方案
#1
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<fstream.h>
.h都去掉
#2
你这个想法是不对的!!很不专业!!谢谢
#3
想求助别人首先要懂得谦虚的态度。
#4
干~ 专业的楼主请自己调试去! 写写!
#5
quantity的值初始化为0 因为你的文件没有数据输入 所以quantity的值为0
#6
void FIFO()
{
int i;
int current_hour;
int current_minute;
int total_time=0;
cout<<endl;
cout<<endl<<"FIFO算法作业流"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout.setf(2);
cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(12)<<"开始时间"<<setw(14)<<"周转时间(分)"<<endl;
current_hour=jobs[0].reach_hour;
current_minute=jobs[0].reach_minite;
for(i=0;i<quantity;i++)
{
jobs[i].start_time=current_hour*100+current_minute;
jobs[i].wait_time=(current_hour-jobs[i].reach_hour)*60+(current_minute-jobs[i].reach_minite)+jobs[i
].need_time;
cout<<setw(10)<<jobs[i].number<<setw(12)<<jobs[i].reach_time<<setw(12)<<jobs[i].start_time<<setw(14)<<jobs[i].wait_time<<endl;
current_hour=current_hour+(jobs[i].need_time+current_minute)/60;
current_minute=(jobs[i].need_time+current_minute)%60;
total_time+=jobs[i].wait_time;
}
if( quantity != 0 )//-----------error
cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl;
}
#1
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<fstream.h>
.h都去掉
#2
你这个想法是不对的!!很不专业!!谢谢
#3
想求助别人首先要懂得谦虚的态度。
#4
干~ 专业的楼主请自己调试去! 写写!
#5
quantity的值初始化为0 因为你的文件没有数据输入 所以quantity的值为0
#6
void FIFO()
{
int i;
int current_hour;
int current_minute;
int total_time=0;
cout<<endl;
cout<<endl<<"FIFO算法作业流"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout.setf(2);
cout<<setw(10)<<"作业号"<<setw(12)<<"到达时间"<<setw(12)<<"开始时间"<<setw(14)<<"周转时间(分)"<<endl;
current_hour=jobs[0].reach_hour;
current_minute=jobs[0].reach_minite;
for(i=0;i<quantity;i++)
{
jobs[i].start_time=current_hour*100+current_minute;
jobs[i].wait_time=(current_hour-jobs[i].reach_hour)*60+(current_minute-jobs[i].reach_minite)+jobs[i
].need_time;
cout<<setw(10)<<jobs[i].number<<setw(12)<<jobs[i].reach_time<<setw(12)<<jobs[i].start_time<<setw(14)<<jobs[i].wait_time<<endl;
current_hour=current_hour+(jobs[i].need_time+current_minute)/60;
current_minute=(jobs[i].need_time+current_minute)%60;
total_time+=jobs[i].wait_time;
}
if( quantity != 0 )//-----------error
cout<<endl<<"总周转时间:"<<total_time<<" 平均周转时间:"<<total_time/quantity<<endl;
}