佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1191|回复: 7

关于C++的 queue operation

[复制链接]
发表于 24-12-2008 08:23 AM | 显示全部楼层
你的IDE版本跟你的参考来源一样吗?
visual studio的queue.h是没有enqueue的
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 22-12-2008 10:04 PM | 显示全部楼层 |阅读模式
小弟在 web site 找到有关queue application 的c++ source code但是compile后有error.
希望路过的高手能帮我解答.
以下是完整的program:

//File:Airport.cpp
//This program simulates the small airport described above using C++ classes,
//and the queue implementation given before.
//Include libraries of needed functions.

#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<queue>
using namespace std;

enum{MAXQUEUE= 5};                           //Queue maximum allowable size
enum action {arrive,depart};                 //What is the plane wishing to do?

class Plane                                  //Declaration of the Plane class
{
public:
        Plane(){id=0;tm=0;}                     //Plane constuctor function
        void set_id(int identity){id=identity;} //id modification member function
        void set_time(int time){tm=time;}       //time modification member function
        int get_id()const {return id;}           //Const member function for id
        int get_time()const {return tm;}         //Const member function for time
private:
        int id;                                  //Plane identification number
        int tm;                                  //Time of its arrival in queue
};
queue< Plane>landing;       //Create the landing queue
queue< Plane>takeoff;       //Create the takeoff queue
int curtime;               //Current time; one unit = time for takeoff or landing
int endtime;               //Total number of time units to run
int idletime=0;            //Number of units when the runway is idle
int nplanes=0;             //Number of planes processed so far
int nland=0;               //Number of planes landed
int ntakeoff=0;            //Number of planes taken off
int nrefuse=0;             //Number of planes refused use of the airport
int landwait=0;            //Total waiting time for planes landed
int takeoffwait=0;         //Total waiting time for planes taking off
double expectarrive;       //Expected no. planes arriving to land in one time unit
double expectdepart;       //Expected no. planes newly ready to take off

void start (int& endtime, double& expectarrive, double& expectdepart);
//Prompts the user for the required input values, and initializes the program

Plane newplane (int& nplanes, int curtime, action kind);
//Creates a new plane object and undates nplanes

void refuse (const Plane& p, action kind, int& nrefuse);
//Processes a plane waiting to use the runway, but the queue is full

void land (const Plane& p, int curtime, int& nland, int& landwait);
//Processes a plane,p, that is actually landing

void fly (const Plane& p, int curtime, int& ntakeoff, int& takeoffwait);
//Processes a plane, p, that is actually taking off

void idle (int curtime, int& idletime);
//Updates variables for one time unit when the runway is idle

void conclude ();
//Writes out all the required statistics and conclude the simulation

int randomnumber (double expectedvalue);
//Generates a random non-negative integer according to a Poisson distribution

void reset();      //Resets all variables to their initial values, and clears the queues


int main()         //The main program
{
        Plane p;
        bool finish;
        char response;
        int j;

        do{
                start (endtime, expectarrive, expectdepart);
                for (curtime = 1; curtime<=endtime; curtime++)
                {
                        j=randomnumber(expectarrive);
                        for (int i=1; i<=j; i++)
                        {
                                p=newplane(nplanes, curtime, arrive);
                                if(landing.size() == MAXQUEUE)
                                
                                        refuse (p,arrive, nrefuse);
                                
                                else
                                
                                        landing.enqueue(p);
                                
                        }
                        j = randomnumber(expectdepart);
                        for (int x = 1; x<=j; x++)
                        {
                                p=newplane (nplanes, curtime, depart);
                                if (takeoff.size() ==MAXQUEUE)
                                        refuse(p,depart, nrefuse);
                                else
                                        takeoff.enqueue(p);
                        }
                        if(!landing.is_empty())
                        {
                                p = landing.serve();
                                land(p,curtime, nland, landwait);
                        }
                        else if(!takeoff.is_empty())
                        {
                                p = takeoff.serve();
                                fly (p, curtime, ntakeoff, takeoffwait);
                        }
                        else
                                idle (curtime, idletime);
                }
                conclude ();
                reset ();
                cout<<"Do you want to run another simulation?";
                cin>>response;
                finish = ((response =='n')||(response =='N'));
        }while (!finish);
        return 0;
}
回复

使用道具 举报

 楼主| 发表于 22-12-2008 10:05 PM | 显示全部楼层
void start (int& endtime, double& expectarrive, double& expectdepart)
{
        bool ok;
        char response;
        cout<<"This program simulates an airport with only one runway. "<<endl;
        cout<<"One plane can land or depart in each unit of time"<<endl;
        cout<<"Up to "<<MAXQUEUE<<"plane can"<<endl;
        cout<<"be waiting to land ir take off at any time."<<endl;
        cout<<"How many units of time will the simulation run?";
        cin>>endtime;
        srand(time(0));

        do{
                cout<<"Expected number of arrivals per unit time(real number)?";
                cin>>expectarrive;
                cout<<"Expected number of departures per unit time(real number)?";
                cin>>expectdepart;
                if((expectarrive < 0.0)||(expectdepart < 0.0))
                {
                        cout<<"These numbers must be non-negative"<<endl;
                        ok = false;
                }
                else if ((expectarrive + expectdepart)>1.0)
                {
                        cout<<"The airport will become saturated. Read new numbers?";
                        cin>>response;
                        ok = (response =='n')||(response =='N');
                }
                else ok = true;
        }while (!ok);
}

Plane newplane (int& nplanes, int curtime, action kind)
{
        Plane p;

        nplanes++;
        p.set_id (nplanes);
        p.set_time (curtime);
        switch (kind)
        {
        case depart : cout<<"    Plane"<<nplanes;
                        cout<<"ready to take off."<<endl;
                        break;
        case arrive : cout<<"    Plane"<<nplanes;
                        cout<<"ready to land."<<endl;
                        break;
        }
        return p;
}

void refuse (const Plane& p, action kind, int& nrefuse)
{
        switch (kind)
        {
        case depart : cout<<"        Plane"<<p.get_id();
                cout<<" told to try later."<<endl;
                break;
        case arrive : cout<<"        Plane"<<p.get_time();
                cout<<"directed to another airport."<<endl;
                break;
        }
        nrefuse++;
}

void land (const Plane&p, int curtime, int&nland, int& landwait)
{
        int wait;

        wait = curtime - p.get_time();
        cout<<setw(4)<<curtime<<": Plane"<<p.get_id()<<"landed;";
        cout<<"In queue"<<wait<<"units."<<endl;
        nland++;
        landwait = landwait + wait;
}

void fly (const Plane& p, int curtime, int ntakeoff, int& takeoffwait)
{
        int wait;

        wait = curtime - p.get_time();
        cout<<setw(4)<<curtime<<": Plane"<<p.get_id()<<" took off;";
        cout<<" In queue"<<wait<<" units."<<endl;
        ntakeoff++;
        takeoffwait = takeoffwait + wait;
}

void idle (int curtime, int& idletime)
{
        cout<<setw(4) <<curtime<<": Runway is ilde."<<endl;
        idletime++;
}

void conclude()
{
        cout<<endl<<endl;
        cout<<"Simulation has concluded after"<<endtime<<"units."<<endl;
        cout<<endl;        
        cout<<"Expectes number of arrivals was: "<<expectarrive<<endl;
        cout<<"Expectes number of departures was:"<<expectdepart<<endl;
        cout<<endl;
        cout<<"Total number of planes processed:"<<setw(4)<<nplanes<<endl;
        cout<<" Number of planes landed:   "<<setw(4)<<nland<<endl;
        cout<<" Number of planes taken off: "<<setw(4)<<ntakeoff<<endl;
        cout<<" Number of planes refused use:"<<setw(4)<<nrefuse<<endl;
        cout<<" Number left ready to land: "<<setw(4)<<landing.size();
        cout<<endl;
        cout<<" Number left ready to take off:"<<setw(4)<<takeoff.size();
        cout<<endl;
        cout<<endl;

        if(endtime > 0)
        {
                cout<<"Percentage of time runwaay idle:";
                cout<<setw(6)<<setprecision(2)<<(idletime/(double)endtime)*100.0;
                cout<<endl;
        }
        if(nland > 0)
        {
                cout<<"Average wait time to land:   ";
                cout<<setw(6)<<setprecision(2)<<(landwait/(double)nland);
                cout<<endl;
        }
        if(ntakeoff > 0)
        {
                cout<<"Average wait time to take off: ";
                cout<<setw(6)<<setprecision(2)<<(takeoffwait/(double)ntakeoff);
                cout<<endl;
                cout<<endl<<endl;
        }
}


int randomnumber (double expectedvalue)
{
        double em,x;
        int n;
        em = exp(-expectedvalue);
        x = rand()/(double)RAND_MAX;
        n= 0;
        while (x > em)
        {
                n++;
                x =x * rand()/(double)RAND_MAX;
        }
        return n;
}

void reset ()
{
        idletime=0;
        nplanes=0;
        nland=0;
        ntakeoff=0;
        nrefuse=0;
        landwait=0;
        takeoffwait=0;
        landing.clear();
        takeoff.clear();
}
回复

使用道具 举报

 楼主| 发表于 23-12-2008 09:59 AM | 显示全部楼层
没人懂吗?
error C2039: 'enqueue' : is not a member of 'queue<class Plane,class std::deque<class Plane,class std::allocator<class Plane> > >'
请问这指的是什么?
还有serve()和clear()是queue operation吗?
回复

使用道具 举报

发表于 23-12-2008 02:32 PM | 显示全部楼层
你的queue class没有enqueue的function
回复

使用道具 举报

 楼主| 发表于 5-1-2009 12:11 AM | 显示全部楼层

回复 5# yeenfei 的帖子

不好意思, 我不是主修编程的, 不是很明白你的意思.
IDE 是什么东东来? 我用的是visual c++ 6.0.
以上的code 是有被我改过.原本有#include"queue.h"的, 可是我没这header file
所以改成#include<queue>.不理会其它, 只要能compile到!
queue.h是没有enqueue的?为什么不能declared enqueue的function 在queue.h 吗?
还有就是为什么很多参考书和网上的program都有用classes, 这个学校没教. 在外面工作会有用到吗?
回复

使用道具 举报

Follow Us
发表于 8-1-2009 10:59 PM | 显示全部楼层
原帖由 小白_86 于 5-1-2009 12:11 AM 发表
不好意思, 我不是主修编程的, 不是很明白你的意思.
IDE 是什么东东来? 我用的是visual c++ 6.0.
以上的code 是有被我改过.原本有#include"queue.h"的, 可是我没这header file
所以改成#include.不理会其它, 只要 ...


我觉得你还是买本书从头学起好一点,你缺少了很多基础知识,导致你目前学习困难
回复

使用道具 举报

发表于 11-2-2009 06:56 PM | 显示全部楼层

回复 6# 小白_86 的帖子

我没有 #include <queue> (不懂有没有在C++)
不过我用过#include<list>

如果你要学习Data Structure, 最好不要include build-in 的library

尝试自己写queue.h, queue.cpp
更容易integrate你要的concept.
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 18-12-2025 01:46 AM , Processed in 0.109438 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表