64,643
社区成员
发帖
与我相关
我的任务
分享
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node {
int data; // data is the time how long the customer stay in the queue
Node *next;
};
void print_info(); //display name,id and assignment number
void insert_n(Node *head, int num); //insert coutomers to the queue
void travel_n(Node *head); //time counter plus one when each customer leave the checkout
int dele_n(Node *head); // remove a customer from the queue
void insert_n(Node *head, int num){
Node * temp, * tempe;
int i;
temp=head;
for(i=1;i <=num;++i)
{
while(temp->next)//这里要改
{
temp=temp->next;
}
tempe=new Node;
tempe->data=0;
temp->next=tempe;//这里是要做什么
travel_n(head);
}
}
void travel_n(Node *head){
Node * temp;
temp=head;
while(temp)
{
temp->data=temp->data+1;
temp=temp->next;
}
}
int dele_n(Node *head){
int wait_time;
Node * temp;
temp=head;
wait_time=temp->data;
temp=temp->next; //这里有内存泄漏,先把temp->next 转移到一个临时变量,然有释放temp
head=temp;
travel_n(head);
return wait_time;
}
int main()
{
Node *head=NULL;
Node temp,end;
int ser_num=0,cus_num=0,i;
float time,averaget,longestt=0,tempt; //time 定义成浮点形,下面循环又用整形来判断???
cout < <"Start of the simulation program" < <endl;
cout < <"Enter the length of time to run the simulator : ";
cin>>time;
cout < <endl;
for(i=0;i <time;++i)
{
switch(rand()%4)
{
case 1 :
if(head==NULL)
{
insert_n(head, 1);
cus_num=cus_num+1;
}else
{
tempt=dele_n(head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
insert_n(head,1);
cus_num=cus_num+1;
}
case 2 :
if(head==NULL)
{
insert_n(head, 2);
cus_num=cus_num+1;
}else
{
tempt=dele_n(head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
insert_n(head,2);
cus_num=cus_num+2;
}
case 0 :
if(head)
tempt=dele_n(head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
case 3 :
if(head)
tempt=dele_n(head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
}
}
averaget=time/ser_num;
cout < <"Customers served : " < <ser_num < <endl;
cout < <"Average wait : " < <averaget < <endl;
cout < <"Longest wait : " < <longestt < <endl;
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node {
int data; // data is the time how long the customer stay in the queue
Node *next;
};
void print_info(); //display name,id and assignment number
void insert_n(Node **head, int num); //insert coutomers to the queue
void travel_n(Node *head); //time counter plus one when each customer leave the checkout
int dele_n(Node **head); // remove a customer from the queue
void insert_n(Node **head, int num){
Node * temp, *tempe;
int i;
temp = *head;
for(i = 1;i <= num; ++i)
{
while(temp)
{
temp=temp->next;
}
tempe=new Node;
tempe->data=0;
tempe->next=NULL;
if ( NULL == temp )
{
*head = temp = tempe;
}
travel_n(*head);
}
}
void travel_n(Node *head){
Node * temp;
temp = head;
while(temp)
{
temp->data = temp->data + 1;
temp = temp->next;
}
}
int dele_n(Node **head){
int wait_time;
Node * temp;
temp = *head;
wait_time=temp->data;
temp = temp->next;
*head = temp;
travel_n(*head);
return wait_time;
}
int main()
{
Node *head=NULL;
//Node temp,end;
int ser_num=0,cus_num=0,i;
float time,averaget,longestt=0,tempt;
cout <<"Start of the simulation program" <<endl;
cout <<"Enter the length of time to run the simulator : ";
cin>>time;
cout <<endl;
for(i=0;i <time;++i)
{
switch(rand()%4)
{
case 1 :
if(head==NULL)
{
insert_n(&head, 1);
cus_num=cus_num+1;
}else
{
tempt=dele_n(&head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
insert_n(&head,1);
cus_num=cus_num+1;
}
break; // case need break
case 2 :
if(head==NULL)
{
insert_n(&head, 2);
cus_num=cus_num+1;
}else
{
tempt=dele_n(&head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
insert_n(&head,2);
cus_num=cus_num+2;
}
break;
case 0 :
if(head)
tempt=dele_n(&head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
break;
case 3 :
if(head)
tempt=dele_n(&head);
if(longestt <tempt)
longestt=tempt;
time=time+tempt;
ser_num++;
break;
}
}
averaget = time/ser_num;
cout <<"Customers served : " <<ser_num <<endl;
cout <<"Average wait : " <<averaget <<endl;
cout <<"Longest wait : " <<longestt <<endl;
return 0;
}