C++ 餐厅排队模拟函数中的标记错误
我正在使用队列为我的data structures类创建一个餐厅模拟,我对如何实现这一点有很好的想法,但我的程序不断抛出错误并标记我的函数,该函数增加队列中所有客户的等待时间变量 有时,我的程序会一直运行,并生成平均等待时间的值,而其他时候,它会在生成第一个或第二个客户后中断。我认为我使用incrementWaitTime函数的条件可以防止它从空内存位置读取,但显然没有。有没有人能浏览一下我的源代码,帮我找出哪些代码被破坏了 编辑:下面是我的程序抛出的确切错误: 作业2餐厅队列中0x010419aa处的未处理异常。exe:0xC0000005:访问冲突读取位置0x00000018C++ 餐厅排队模拟函数中的标记错误,c++,data-structures,queue,simulation,C++,Data Structures,Queue,Simulation,我正在使用队列为我的data structures类创建一个餐厅模拟,我对如何实现这一点有很好的想法,但我的程序不断抛出错误并标记我的函数,该函数增加队列中所有客户的等待时间变量 有时,我的程序会一直运行,并生成平均等待时间的值,而其他时候,它会在生成第一个或第二个客户后中断。我认为我使用incrementWaitTime函数的条件可以防止它从空内存位置读取,但显然没有。有没有人能浏览一下我的源代码,帮我找出哪些代码被破坏了 编辑:下面是我的程序抛出的确切错误: 作业2餐厅队列中0x010419
理想情况下,客户不应该知道下一个客户是谁。这是将对象绑定到特定的数据结构。如果您的客户有一天同时进入两个队列,该怎么办?对于一般数据结构,增加等待时间是对队列的使用,而不是队列的固有属性。如果你想排队买食物怎么办?你会用重复的逻辑写一个新的队列吗?通常最好包含错误消息的确切文本…不要让我们猜!你应该在调试器下运行这个程序,这将允许你在变量崩溃时检查变量值,或者通过它来查看它是否做了你期望它做的事情。考虑当你在队列中有一个客户时的情况,这样Top= =底部和Top-> Next=NULL。你的递增等待时间发生了什么?@NeilKirk我不确定我是否理解你关于递增等待时间的说法。。你是说我应该在主函数中将总等待时间设置为变量,而不是将其设置为队列的属性吗?incrementWaitTime不应该是队列中的函数。相反,它应该是队列外部的一个函数,该函数接受队列实例,并沿着它进行迭代,以执行需要执行的任何操作。
#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;
}