[QT]效率优先的电梯调度算法及命令输出

电梯调度算法有很多,这个呢是我自己写的一个简单的已效率优先的调度算法

简单说,就是电梯运行时间最短内把用户送到指定地点

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QStringListModel"
#include<QTcpServer>
#include<QTcpSocket>
#include<QMessageBox>
#include<QDataStream>
#include<QString>
#include<QByteArray>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->socket=0;
    this->server=new QTcpServer(this);
    this->server->listen(QHostAddress::Any,8080);
    QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(NewConnection()));
}
void MainWindow::NewConnection()
{
    this->socket=this->server->nextPendingConnection();
    QMessageBox::about(this,"提示","有新的连接!");
    connect(this->socket,SIGNAL(readyRead()),this,SLOT(ReceiveData()));
}
void MainWindow::ReceiveData()
{
   QByteArray arr=this->socket->readAll();
   QDataStream dst(arr);
   QString str1;
   dst>>str1;
   bool OK;
   int n=str1.toInt(&OK,10);
   if(n==0)
   {
       QByteArray arr;
       QDataStream dst1(&arr,QIODevice::ReadWrite);
       QString s=QString::number(q1.front(),10);
       dst1<<s;
       this->socket->write(arr);
       q1.pop();
   }
   else if(n==1)
   {
       QByteArray arr;
       QDataStream dst1(&arr,QIODevice::ReadWrite);
       QString s=QString::number(q2.front(),10);
       dst1<<s;
       this->socket->write(arr);
       q2.pop();
   }
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::Init()
{
    All.clear();
    Fir.clear();
    Sec.clear();
    srand((unsigned)time(NULL));
    int RandomCommandNumber = rand() % 10 + 1;
    while(RandomCommandNumber<1)
        RandomCommandNumber = rand() % 10 + 1;
    for (int i = 0; i < 2; i++)
    {
        Elevator temp;
        temp.number = i;
        temp.currentFloor= rand() % 10 + 1;
        AllElevator.push_back(temp);
        //cout << "电梯"<<i<<"初始楼层:" << temp.currentFloor << endl;
        QString t=QString::number(temp.currentFloor,10);
        if(i==0)
            ui->lineEdit->insert(t);
        else if(i==1)
            ui->lineEdit_2->insert(t);

    }
    //cout << "命令数:" << RandomCommandNumber << endl;
    for (int i = 0; i < RandomCommandNumber; i++)
    {
        RandomCommand(AllCommand1);
    }
    QStringListModel *model = new QStringListModel(All);
    ui->listView->setModel(model);
    ManyElevatorSort(AllCommand1, RandomCommandNumber, RandomCommandNumber);
    QStringListModel *model1 = new QStringListModel(Fir);
    ui->listView_2->setModel(model1);
    QStringListModel *model2 = new QStringListModel(Sec);
    ui->listView_3->setModel(model2);
}
void MainWindow::RandomCommand(vector<Command> &AllCommand)
{
    Command Temp;
    Temp.name = AllName[rand() % 20];
    while(CheckRandomName(AllCommand,Temp.name))
        Temp.name = AllName[rand() % 20];
    Temp.start = rand() % 10 + 1;
    Temp.end = rand() % 10 + 1;
    while (Temp.start == Temp.end)
        Temp.end = rand() % 10 + 1;
    Temp.flag = OUT;
    AllCommand.push_back(Temp);
    //cout << Temp.name << " From " << Temp.start << " To " << Temp.end << endl;
    QString t=QString::fromStdString(Temp.name);
    QString str1=QString::number(Temp.start,10);
    QString str2=QString::number(Temp.end,10);
    QString A=t+tr(" ")+str1+tr(" To ")+str2;
    All<<A;
}
bool MainWindow::CheckRandomName(vector<Command> &AllCommand,string name)
{
    for (int i = 0; i < AllCommand.size(); i++)
    {
        if (AllCommand[i].name == name)
            return true;
    }
    return false;
}
void MainWindow::ManyElevatorSort(vector<Command> AllCommand, int TaskNumber, int AllTaskNumber)
{
    int i;
    while (TaskNumber>0)
    {
        for (int elevatorIndex = 0; elevatorIndex < AllElevator.size(); elevatorIndex++)
        {
            for (; AllElevator[elevatorIndex].currentFloor <= 10; AllElevator[elevatorIndex].currentFloor++)            //电梯上升
            {
                for (i = 0; i<AllTaskNumber; i++)
                {
                    if (AllCommand[i].end>AllCommand[i].start&&AllCommand[i].start == AllElevator[elevatorIndex].currentFloor&&AllCommand[i].flag == 1)
                    {
                        //cout << "当前层数为" << AllElevator[elevatorIndex].currentFloor << "  " << AllCommand[i].name << "进" << AllElevator[elevatorIndex].number << "电梯" << endl;
                        AllCommand[i].flag = ENTER;
                        AllCommand[i].ElevatorNumber = elevatorIndex;
                        QString t=QString::fromStdString(AllCommand[i].name);
                        QString str1=QString::number(AllElevator[elevatorIndex].currentFloor,10);
                        QString str2=QString::number(AllElevator[elevatorIndex].number,10);
                        QString A=t+tr(" ENTER ")+tr("NO.")+str2+tr(" ON ")+str1;
                        if(elevatorIndex==0)
                        {
                            Fir<<A;
                            qEle1.push(AllCommand[i]);
                            q1.push(AllElevator[elevatorIndex].currentFloor);
                        }
                        else if(elevatorIndex==1)
                        {
                            Sec<<A;
                            qEle2.push(AllCommand[i]);
                            q2.push(AllElevator[elevatorIndex].currentFloor);
                        }


                    }
                    else if ((AllCommand[i].end>AllCommand[i].start&&AllCommand[i].end == AllElevator[elevatorIndex].currentFloor&&AllCommand[i].flag == 0)&&AllCommand[i].ElevatorNumber==elevatorIndex)
                    {
                        //cout << "当前层数为" << AllElevator[elevatorIndex].currentFloor << "  " << AllCommand[i].name << "出" << AllElevator[elevatorIndex].number << "电梯" << endl;
                        QString t=QString::fromStdString(AllCommand[i].name);
                        QString str1=QString::number(AllElevator[elevatorIndex].currentFloor,10);
                        QString str2=QString::number(AllElevator[elevatorIndex].number,10);
                        QString A=t+tr(" OUT ")+tr("NO.")+str2+tr(" ON ")+str1;
                        if(elevatorIndex==0)
                        {
                            Fir<<A;
                            qEle1.push(AllCommand[i]);
                            q1.push(AllElevator[elevatorIndex].currentFloor);
                        }
                        else if(elevatorIndex==1)
                        {
                            Sec<<A;
                            qEle2.push(AllCommand[i]);
                            q2.push(AllElevator[elevatorIndex].currentFloor);
                        }

                        TaskNumber--;
                        if (TaskNumber == 0) break;
                    }
                }
                if (TaskNumber == 0) break;
            }

        }

        for (int elevatorIndex = AllElevator.size()-1; elevatorIndex >=0; elevatorIndex--)
        {
            for (AllElevator[elevatorIndex].currentFloor = 10; AllElevator[elevatorIndex].currentFloor >= 1; AllElevator[elevatorIndex].currentFloor--)        //电梯下降
            {
                for (i = 0; i<AllTaskNumber; i++)
                {
                    if (AllCommand[i].end<AllCommand[i].start&&AllCommand[i].start == AllElevator[elevatorIndex].currentFloor&&AllCommand[i].flag == 1)
                    {
                        //cout << "当前层数为" << AllElevator[elevatorIndex].currentFloor << "  " << AllCommand[i].name << "进" << AllElevator[elevatorIndex].number << "电梯" << endl;
                        AllCommand[i].flag = ENTER;
                        AllCommand[i].ElevatorNumber = elevatorIndex;
                        QString t=QString::fromStdString(AllCommand[i].name);
                        QString str1=QString::number(AllElevator[elevatorIndex].currentFloor,10);
                        QString str2=QString::number(AllElevator[elevatorIndex].number,10);
                        QString A=t+tr(" ENTER ")+tr("NO.")+str2+tr(" ON ")+str1;
                        if(elevatorIndex==0)
                        {
                            Fir<<A;
                            qEle1.push(AllCommand[i]);
                            q1.push(AllElevator[elevatorIndex].currentFloor);
                        }
                        else if(elevatorIndex==1)
                        {
                            Sec<<A;
                            qEle2.push(AllCommand[i]);
                            q2.push(AllElevator[elevatorIndex].currentFloor);
                        }


                    }
                    else if ((AllCommand[i].end<AllCommand[i].start&&AllCommand[i].end == AllElevator[elevatorIndex].currentFloor&&AllCommand[i].flag == 0)&&AllCommand[i].ElevatorNumber==elevatorIndex)
                    {
                        //cout << "当前层数为" << AllElevator[elevatorIndex].currentFloor << "  " << AllCommand[i].name << "出" << AllElevator[elevatorIndex].number << "电梯" << endl;
                        QString t=QString::fromStdString(AllCommand[i].name);
                        QString str1=QString::number(AllElevator[elevatorIndex].currentFloor,10);
                        QString str2=QString::number(AllElevator[elevatorIndex].number,10);
                        QString A=t+tr(" OUT ")+tr("NO.")+str2+tr(" ON ")+str1;
                        if(elevatorIndex==0)
                        {
                            Fir<<A;
                            qEle1.push(AllCommand[i]);
                            q1.push(AllElevator[elevatorIndex].currentFloor);
                        }
                        else if(elevatorIndex==1)
                        {
                            Sec<<A;
                            qEle2.push(AllCommand[i]);
                            q2.push(AllElevator[elevatorIndex].currentFloor);
                        }

                        TaskNumber--;
                        if (TaskNumber == 0) break;
                    }
                }
                if (TaskNumber == 0) break;
            }
            AllElevator[elevatorIndex].currentFloor = 0;

        }

    }
}

void MainWindow::on_pushButton_clicked()
{
    Init();
}

void MainWindow::on_pushButton_2_clicked()
{
    int RandomCommandNumber = rand() % 5 + 1;
    while(RandomCommandNumber<1)
        RandomCommandNumber = rand() % 5 + 1;
    for (int i = 0; i < RandomCommandNumber; i++)
    {
        RandomCommand(AllCommand1);
    }
    QStringListModel *model = new QStringListModel(All);
    ui->listView->setModel(model);
    ManyElevatorSort(AllCommand1, RandomCommandNumber, RandomCommandNumber);
    QStringListModel *model1 = new QStringListModel(Fir);
    ui->listView_2->setModel(model1);
    QStringListModel *model2 = new QStringListModel(Sec);
    ui->listView_3->setModel(model2);
}
void MainWindow::Write()
{

}

 

posted @ 2018-03-13 20:51  leeli73  阅读(661)  评论(0编辑  收藏  举报