1. 程式人生 > >作業系統課設移動臂排程演算法

作業系統課設移動臂排程演算法

main

#include "pcb.h"



int main()
{

	work();

	getchar();
	getchar();
}
work.cpp
#include "pcb.h"


void work()
{
	Pcb pcb[200];
	int a[200] = {};
	int iChoice;
	int j = 0;

	for (int i = 0; i < 200; ++i)
	{
		pcb[i].setPcb(i, 0);
	}
	pcb[125].setuserPcb();
	a[0] = 125;
	while (1)
	{
		iChoice = chooseInMenu();
		switch (iChoice)
		{
		case 1: FIFO(pcb,a);break;
		case 2:break;
		case 3: mobileDevices(pcb,a);break;
		case 4: onewayScan(pcb, a);break;
		case 5:inputFloor(pcb,a);break;
		case 6:break;
		case 0:j = 1;break;
		default:
			break;
		}
		if (j == 1)
			break;
	}
}


void inputFloor(Pcb *p,int *q)
{
	int i;
	int j = 0;
	cout << "請輸入要去的樓層:";
	cin >> i;
	while (i >= 200)
	{
		cout << "輸入範圍錯誤,請重新輸入要去的樓層:";
		cin >> i;
	}

	(p + i)->setuserPcb();

	while (*(q + j) != '\0')
		j++;
	*(q + j) = i;
	*(q + j + 1) = '\0';
}


int chooseInMenu()
{
	int i;
	while (1)
	{
		cout << endl;
		cout << "+-----------------------------------------+" << endl;
		cout << "+      1.先來先服務        2.最短時間優先   +" << endl;
		cout << "+      3.電梯排程          4.單向掃描       +" << endl;
		cout << "+      5.輸入樓層                          +" << endl;
		cout << "+-----------------------------------------+" << endl;
		cout << "+                       0.退出            +" << endl;
		cout << "+-----------------------------------------+" << endl;
		cout << "請輸入操作指令:";
		cin >> i;
		if (i >= 0 && i <= 9)
			break;
		else
			cout << "請重新輸入" << endl;

	}
	return i;
}

void FIFO(Pcb *p, int *q)
{
	int i = 0;
	int j;
	while (*(q + i) != '\0')
	{
		j = *(q + i);
		cout << "Floor:"<< j << endl;
		(p + j)->setUnuserPcb();
		i++;
	}
}

void shortTime(Pcb *p, int *q)
{
	int i = 0;
	int j;
	
	while (*(q + i) != '\0')
	{
		j = *(q + i);
		cout << "Floor:" << j << endl;
		(p + j)->setUnuserPcb();
		i++;
	}
}

void mobileDevices(Pcb *p, int *q)
{
	int i = 0;
	int j;
	int len = sort(q);
	
	while (*(q + len) != '\0')
	{
		j = *(q + len);
		cout << "Floor:" << j << endl;
		(p + j)->setUnuserPcb();
		len++;
	}
	for (int k = sort(q) - 1; k >= 0; k--)
	{
		j = *(q + k);
		cout << "Floor:" << j << endl;
		(p + j)->setUnuserPcb();
	}
}


void onewayScan(Pcb *p, int *q)
{
	int i = 0;
	int j;
	int len = sort(q);

	while (*(q + len) != '\0')
	{
		j = *(q + len);
		cout << "Floor:" << j << endl;
		(p + j)->setUnuserPcb();
		len++;
	}
	for (int k = 0; k < sort(q); k++)
	{
		j = *(q + k);
		cout << "Floor:" << j << endl;
		(p + j)->setUnuserPcb();
	}
}


int sort(int *p)
{
	int len = 0;
	int temp;

	while (*(p + len) != '\0')
	{
		len++;
	}
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = i + 1; j < len; j++)
		{
			if (*(p + j) < *(p + i))
			{
				temp = *(p + i);
				*(p + i) = *(p + j);
				*(p + j) = temp;
			}
		}
	}
	for (int i = 0; i < len; i++)
	{
		if (*(p + i) == 125)
			return i;
	}
}

pcb
#pragma once
#include<iostream>
using namespace std;

class Pcb
{
public:
	Pcb():nPosition(0),bVisited(0){}
	~Pcb() {}
	Pcb(int np, bool bv);
	void setPcb(int np, bool bv);
	void showPcb();
	void setuserPcb();
	void setUnuserPcb();

private:
	int nPosition;
	bool bVisited;//1表示有人按電梯
};

int chooseInMenu();
void work();
void FIFO(Pcb *p, int *q);
void shortTime(Pcb *p, int *q);
void mobileDevices(Pcb *p, int *q);
void onewayScan(Pcb *p, int *q);
void run();
void inputFloor(Pcb *p, int *q);
int sort(int *p);
#include "pcb.h"

Pcb::Pcb(int np, bool bv)
{
	this->nPosition = np;
	this->bVisited = bv;
}

void Pcb::setPcb(int np, bool bv)
{
	this->nPosition = np;
	this->bVisited = bv;
}

void Pcb::showPcb()
{
	cout << nPosition << "  " << bVisited << endl;
}

void Pcb::setuserPcb()
{
	this->bVisited = 1;
}

void Pcb::setUnuserPcb()
{
	this->bVisited = 0;
}