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;
}