SJF算法思想:
第一步:通过bubbleSort1()函数按到达时间小者优先进行排序,确定本调度算法的第一个作业。
第二步:通过getCount()函数获得后续作业的到达时间小于等于第一个作业的完成时间的数量count。
第三步:判断getCount()函数的返回值
若count等于0,则后续作业的开始时间等于该作业的到达时间
若count大于0,将第一个作业后的count个作业通过bubbleSort()按运行时间短者优先进行排序,确定第二个作业
重复第二、三步确定后续作业的排序。
HRN算法思想:
HRN算法与SJF算法相似,多了两个属性:nowTime(记录当前时间),ratio(记录作业响应比),另外第三步按ratio高者优先进行排序。
SJF,HRN输入数据:
SJF输出数据:
HRN输出数据:
注:在SJF算法中,我没有判断两个作业运行时间相同时的优先级,但是由于第一次排序是按提交时间进行的,所以我用着没问题,可能有些情况特殊,需要使用者手动添加一个判断语句,程序才能正确运行。
另外,我使用的是数组,你们也可以改写一下,改成指针的形式。
如有什么错误的地方,望指出。
SJF算法代码:
#include <iostream> using namespace std; class JCB { public: void input(); void output(); void bubbleSort(int start, int len); void bubbleSort1(int start, int len); int getCount(int x); void swap(int x, int y); void makeJCB(int x); void makeTime(int x); // 一些数据的处理 void init(); //初始化函数,貌似不需要 void printCir(); private: int length; double arriveTime[10]; double startTime[10]; double workTime[10]; double finishTime[10]; double cirTime[10]; double dqzzTime[10]; string name[10]; }; void JCB::input() { cout << "请输入作业个数(不大于10个): "; cin >> length; for(int i=0; i<length; i++) { cout << "作业NO." << i+1 << ": " << endl; cout << "请输入作业名字:"; cin >> name[i]; cout << "请输入作业到达时间:"; cin >> arriveTime[i]; cout << "请输入作业运行时间:"; cin >> workTime[i]; } cout << "输入完成!" << endl; } void JCB::output() { cout << endl; for(int i=0; i<length; i++) { makeJCB(i); cout << "NO." << i+1 << endl; cout << "name\t" << "arrive\t" << "work\t" << "start\t"<< "finish\t" << "cir\t" << "dqzz\t" << endl; cout << name[i] << "\t" << arriveTime[i] << "\t" << workTime[i] << "\t" << startTime[i] << "\t" << finishTime[i] << "\t" << cirTime[i] << "\t" << dqzzTime[i] << "\t" << endl; cout << endl; } } void JCB::bubbleSort(int start, int len) { for(int i=start; i<start+len-1; i++) for(int j=start; j<start+len-1; j++) if(workTime[j] > workTime[j+1]) swap(j,j+1); } void JCB::bubbleSort1(int start, int len) { for(int i=start; i<start+len-1; i++) for(int j=start; j<start+len-1; j++) if(arriveTime[j] > arriveTime[j+1]) swap(j,j+1); } int JCB::getCount(int x) { int count = 0; int y = x-1; for(int i=x; i<length; i++) if(arriveTime[i] <= finishTime[y]) count++; return count; } void JCB::swap(int x, int y) { double temp; string temp1; temp = arriveTime[x]; arriveTime[x] = arriveTime[y]; arriveTime[y] = temp; temp = workTime[x]; workTime[x] = workTime[y]; workTime[y] = temp; temp1 = name[x]; name[x] = name[y]; name[y] = temp1; } void JCB::makeTime(int x) { finishTime[x] = startTime[x] + workTime[x]; cirTime[x] = finishTime[x] - arriveTime[x]; dqzzTime[x] = cirTime[x] / workTime[x]; } void JCB::printCir() { double Tcir = 0; double Tdqzz = 0; for(int i=0; i<length; i++) { Tcir = Tcir + cirTime[i]; Tdqzz = Tdqzz + dqzzTime[i]; } cout << "平均周转时间:" << Tcir/length << endl; cout << "平均带权周转时间:" << Tdqzz/length << endl; } void JCB::makeJCB(int x) { if(x == 0) { bubbleSort1(x,length); startTime[x] = arriveTime[x]; makeTime(x); } else { int count = getCount(x); if(count == 0) { startTime[x] = arriveTime[x]; makeTime(x); } else { bubbleSort(x,count); startTime[x] = finishTime[x-1]; makeTime(x); } } } void JCB::init() { for(int i=0; i<10; i++) { arriveTime[i] = 0; startTime[i] = 0; workTime[i] = 0; finishTime[i] = 0; cirTime[i] = 0; dqzzTime[i] = 0; } } int main() { JCB A; A.init(); A.input(); A.output(); A.printCir(); }
HRN算法代码:
#include <iostream> using namespace std; class JCB { public: void input(); void output(); void bubbleSort(int start, int len); void bubbleSort1(int start, int len); int getCount(int x); void swap(int x, int y); void makeJCB(int x); void makeTime(int x); void init(); //貌似不需要这函数 void printCir(); private: int length; double nowTime; double arriveTime[10]; double startTime[10]; double workTime[10]; double finishTime[10]; double cirTime[10]; double dqzzTime[10]; double ratio[10]; string name[10]; }; void JCB::input() { cout << "请输入作业个数(不大于10个)"; cin >> length; for(int i=0; i<length; i++) { cout << "作业NO." << i+1 << ": " << endl; cout << "请输入作业名字:"; cin >> name[i]; cout << "请输入作业到达时间:"; cin >> arriveTime[i]; cout << "请输入作业运行时间:"; cin >> workTime[i]; } cout << "输入完成!" << endl; } void JCB::output() { cout << endl; for(int i=0; i<length; i++) { makeJCB(i); cout << "NO." << i+1 << endl; cout << "name\t" << "arrive\t" << "work\t" << "start\t"<< "finish\t" << "cir\t" << "dqzz\t" << endl; cout << name[i] << "\t" << arriveTime[i] << "\t" << workTime[i] << "\t" << startTime[i] << "\t" << finishTime[i] << "\t" << cirTime[i] << "\t" << dqzzTime[i] << "\t" << endl; cout << endl; } } void JCB::bubbleSort(int start, int len) { for(int i=start; i<start+len; i++) { ratio[i] = (workTime[i] + nowTime - arriveTime[i])/workTime[i]; } for(int i=start; i<start+len-1; i++) for(int j=start; j<start+len-1; j++) if(ratio[j] < ratio[j+1]) swap(j,j+1); } void JCB::bubbleSort1(int start, int len) { for(int i=start; i<start+len-1; i++) for(int j=start; j<start+len-1; j++) if(arriveTime[j] > arriveTime[j+1]) swap(j,j+1); } int JCB::getCount(int x) { int count = 0; int y = x-1; for(int i=x; i<length; i++) if(arriveTime[i] <= finishTime[y]) count++; return count; } void JCB::swap(int x, int y) { double temp; string temp1; temp = arriveTime[x]; arriveTime[x] = arriveTime[y]; arriveTime[y] = temp; temp = workTime[x]; workTime[x] = workTime[y]; workTime[y] = temp; temp = ratio[x]; ratio[x] = ratio[y]; ratio[y] = temp; temp1 = name[x]; name[x] = name[y]; name[y] = temp1; } void JCB::makeTime(int x) { finishTime[x] = startTime[x] + workTime[x]; cirTime[x] = finishTime[x] - arriveTime[x]; dqzzTime[x] = cirTime[x] / workTime[x]; } void JCB::printCir() { double Tcir = 0; double Tdqzz = 0; for(int i=0; i<length; i++) { Tcir = Tcir + cirTime[i]; Tdqzz = Tdqzz + dqzzTime[i]; } cout << "平均周转时间:" << Tcir/length << endl; cout << "平均带权周转时间:" << Tdqzz/length << endl; } void JCB::makeJCB(int x) { if(x == 0) { bubbleSort1(x,length); nowTime = arriveTime[x]; startTime[x] = arriveTime[x]; makeTime(x); } else { int count = getCount(x); if(count == 0) { nowTime = arriveTime[x]; startTime[x] = arriveTime[x]; makeTime(x); } else { nowTime = finishTime[x-1]; bubbleSort(x,count); startTime[x] = finishTime[x-1]; makeTime(x); } } } void JCB::init() { for(int i=0; i<10; i++) { arriveTime[i] = 0; startTime[i] = 0; workTime[i] = 0; finishTime[i] = 0; cirTime[i] = 0; dqzzTime[i] = 0; ratio[i] = 0; } } int main() { JCB A; A.init(); A.input(); A.output(); A.printCir(); }