餐厅队列模拟功能中的标记错误

时间:2014-10-01 22:55:01

标签: c++ data-structures queue simulation

我正在使用队列为我的数据结构类创建一个餐厅模拟,并且知道如何执行此操作,但我的程序不断抛出错误并标记我的函数,增加了"等待时间"所有"客户的变量"在队列中。

有时我的程序会一直运行并为平均等待时间生成一个值,有时它会在生成第一个或第二个客户后中断。我认为我使用incrementWaitTime函数的条件会阻止它从NULL内存位置读取,但显然它们没有。有人可以浏览一下我的源代码并帮我弄清楚它是什么坏了吗?

编辑:这是我的程序投掷的确切错误:

  

" Homework 2 Restaurant Queue.exe中0x010419aa处的未处理异常:0xC0000005:访问冲突读取位置0x00000018。"

#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

class Customer{
public:
    int WaitTime, ServiceTime, OrderCounter, ServiceCounter, OrderTime, test;
    Customer *next;

    Customer(){
        test = 0;
        OrderCounter = 0;
        WaitTime = 0;
        ServiceTime = 0;
        OrderTime = 0;
        next = NULL;
    }
};

class Queue{
public:
    Customer *top, *bottom;
    int totalWaitTime, totalCustomers; 
    Queue(){
        bottom = NULL;
        top = NULL;
        totalWaitTime = 0;
        totalCustomers = 0;
    }

void incrementWaitTime(){
    Customer *temp;
    temp = top;
    if(top == NULL){
        return;
    }
    else{
        if(temp->next == bottom){
            temp->WaitTime++;
        }
        else{
            while(temp->next != bottom){
            temp->WaitTime = temp->WaitTime + 1;
            temp = temp->next;
            }
        }
    }
}

void displayContents(){
    Customer *temp;
    temp = top;
    while(temp!= NULL){
        cout << temp->test << "---->"; 
        temp = temp->next;
    }
    cout << endl;
}

void newCustomer(int x){
    Customer *temp = new Customer;
    temp->OrderTime = x;
    if(top == NULL){ //No customers in line
        top = bottom = temp;
        totalCustomers++;
        cout << "There is a new customer." << endl;
    }
    else{
        temp->next = top;
        top = temp;
        totalCustomers++;
        cout << "There is a new customer." << endl;
    }
}

void removeCustomer(){
    Customer *chase, *follow;
    chase = follow = top;
    if(top == NULL){
        //No customers in queue.
        cout << "No customers are in line.. there's nothing to remove." << endl;
        return;
    }
    else{
        if(top->next == NULL){
            //Only one customer
            delete top;
            top = NULL;
            bottom = NULL;
            return;
        }
        while(chase->next != NULL){
            follow = chase;
            chase = chase->next;
        } 
        delete chase;
        bottom = follow;
        follow->next = NULL;
    }
}

void checkStatus(){
    if(top == NULL){
        bottom = NULL;
        return;
    }
    else if(bottom->OrderCounter != bottom->OrderTime){
        bottom->OrderCounter++;
        bottom->WaitTime++;
    }
    else{
        totalWaitTime = totalWaitTime + bottom->WaitTime;
        removeCustomer();
    }
}
};



int main(){
Queue Restaurant;
int Clock = 1, totalCustomers = 0;
float probArrival = 0;
srand(time(NULL)); //seeds random number generator
int number, orderTime, TotalWaitTime, TotalServiceTime;

while(Clock < 1140){
    while(Clock < 120){
        number = rand();
        number = number%10+1; //Generates a number between 1 and 10
        if(number >=1 && number <= 3){
            orderTime = rand();
            orderTime = orderTime%6 + 1; //Creates orderTime between 1 and 6
            Restaurant.newCustomer(orderTime);
            cout << "The time is: " << Clock << " minutes." << endl;
            Clock++;
        }
        else{
            Restaurant.checkStatus();
            Restaurant.incrementWaitTime();
            cout << "The time is: " << Clock << " minutes." << endl;
            Clock++;
        }
    }
    Clock = 1140;
}
cout << "There were " << Restaurant.totalCustomers << " customers. " << endl;
cout << "Average wait time was: " << Restaurant.totalWaitTime / Restaurant.totalCustomers << " minutes per customer." << endl;


system("pause");
return 1;
}

0 个答案:

没有答案