Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>
#include "genlib.h"
#include <strlib.h>
#include "random.h"
#include "queue.h"

#define SimulationTime    720
#define ArrivalProbability   0.1
#define MinServiceTime       1
#define MaxServiceTime      4

typedef struct 
{

   int customerNumber;
   int arrivalTime;
   int serviceTime;

} *customerT;

typedef struct {

   queueADT queue;
   customerT activeCustomer;
   int time;
   int numCustomers;
   int numServed;
   long totalWaitTime;
   long totalLineLength;

} simDataT;

static bool traceFlag = FALSE;
static void InitializeSimulation(simDataT *sdp);
static void RunSimulation(simDataT *sdp);
static void EnqueueCustomer(simDataT *sdp);
static void ProcessQueue(simDataT *sdp);
static void ServeCustomer(simDataT *sdp);
static void DismissCustomer(simDataT *sdp);
static void ReportResults(simDataT *sdp);

main()

{

   simDataT simData;

   Randomize();

   InitializeSimulation(&simData);

   RunSimulation(&simData);

   ReportResults(&simData);

}

static void InitializeSimulation(simDataT *sdp)

{

   sdp->queue = NewQueue();

   sdp->activeCustomer = NULL;

   sdp->numServed = 0;

   sdp->totalWaitTime = 0;

   sdp->totalLineLength = 0;

}

static void RunSimulation(simDataT *sdp)

{

   for (sdp->time = 0; sdp->time < SimulationTime; sdp->time++) {

       if (RandomChance(ArrivalProbability)) {

           EnqueueCustomer(sdp);

       }

       ProcessQueue(sdp);

   }

}

static void EnqueueCustomer(simDataT *sdp)

{

   customerT c;

   sdp->numCustomers++;

   c = New(customerT);

   c->customerNumber = sdp->numCustomers;

   c->arrivalTime = sdp->time;

   c->serviceTime = RandomInteger(MinServiceTime, MaxServiceTime);

   Enqueue(sdp->queue, c);

   if (traceFlag) {

       printf("%4d: Customer %d arrives and gets in line\n",

              sdp->time, sdp->numCustomers);

   }

}

static void ProcessQueue(simDataT *sdp)

{

   if (sdp->activeCustomer == NULL) {

       if (!QueueIsEmpty(sdp->queue)) {

           ServeCustomer(sdp);

       }

   } else {

       if (sdp->activeCustomer->serviceTime == 0) {

           DismissCustomer(sdp);

       } else {

           sdp->activeCustomer->serviceTime--;

       }

   }

   sdp->totalLineLength += QueueLength(sdp->queue);

}

static void ServeCustomer(simDataT *sdp)

{

   customerT c;

   c = Dequeue(sdp->queue);

   sdp->activeCustomer = c;

   sdp->numServed++;

   sdp->totalWaitTime += (sdp->time - c->arrivalTime);

   if (traceFlag) {

       printf("%4d: Customer %d reaches cashier\n",

              sdp->time, c->customerNumber);

   }

}

static void DismissCustomer(simDataT *sdp)

{

   if (traceFlag) {

       printf("%4d: Customer %d leaves cashier\n",

              sdp->time, sdp->activeCustomer->customerNumber);

   }

   FreeBlock(sdp->activeCustomer);

   sdp->activeCustomer = NULL;

}

static void ReportResults(simDataT *sdp)

{

   printf("Simulation results given the following parameters:\n");

   printf(" SimulationTime:    %4d\n", (int) SimulationTime);

   printf(" ArrivalProbability: %7.2f\n",

                                  (double) ArrivalProbability);

   printf(" MinServiceTime:    %4d\n", (int) MinServiceTime);

   printf(" MaxServiceTime:    %4d\n", (int) MaxServiceTime);

   printf("\n");

   printf("Customers served:    %4d\n", sdp->numServed);

   printf("Average waiting time: %7.2f\n",

          (double) sdp->totalWaitTime / sdp->numServed);

   printf("Average line length: %7.2f\n",

          (double) sdp->totalLineLength / SimulationTime);

}


What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include "genlib.h"
#include <strlib.h>
#include "random.h"
#include "queue.h"

#define SimulationTime    720
#define ArrivalProbability   0.1
#define MinServiceTime       1
#define MaxServiceTime      4

 struct QNode
{

   int customerNumber;
   int arrivalTime;
   int serviceTime;

} *customerT;

struct queue
{

   queueADT queue;
   customerT activeCustomer;
   int time;
   int numCustomers;
   int numServed;
   long totalWaitTime;
   long totalLineLength;

} simDataT;

static bool traceFlag = FALSE;

void InitializeSimulation(struct QNode  *sdp);
void RunSimulation(struct QNode  *sdp);
void EnqueueCustomer(struct QNode  *sdp);
void ProcessQueue(struct QNode  *sdp);
void ServeCustomer(struct QNode  *sdp);
void DismissCustomer(struct QNode  *sdp);
void ReportResults(struct QNode  *sdp);

int main()
{
    
   simDataT simData;
   Randomize();
   InitializeSimulation(&simData);
   RunSimulation(&simData);
   ReportResults(&simData);

}
void InitializeSimulation(struct QNode *sdp)
{
    sdp->queue = NewQueue();
    sdp->activeCustomer=NULL;
    sdp->numServed=
}
Posted
Updated 21-Nov-19 1:13am
v2

That is already C code.
 
Share this answer
 
Comments
Lyke Tanera 21-Nov-19 6:50am    
Where"s your answer i cannot view?
CPallini 21-Nov-19 6:56am    
The code you posted is already C. It is not C++ code.
Lyke Tanera 21-Nov-19 7:10am    
okay,,
but can you help me to convert these into queue linked list in c language?.
CPallini 21-Nov-19 7:16am    
*** IT IS ALREADY C CODE ***
(and queue.h, queue.c are missing, in the code you posted).
Maciej Los 27-Nov-19 9:10am    
Well... Sometimes obvious things are not so obvious...
5ed!
C++ is a superset of C: everything that works in C will work in C++.
The converse is not true: it can be extremely difficult to transfer well written C++ into even bad C, and to "good" C is next to impossible because C++ is designed to be object oriented, and C isn't.

Fortunately for you - as CPallini as already told you - the code you show is C code already, and contains no C++ specific elements. Thus, it needs no conversion and should run fiune (depending on your compiler and options, of course).
 
Share this answer
 
v2
Comments
CPallini 21-Nov-19 7:19am    
+5 for mentioning one of the fathers of computer science. :-D :-D :-D
Maciej Los 27-Nov-19 9:11am    
Agree.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900