simulation
"simulationlinkedqueue.h"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #ifndef _SIMULATION_LINKED_QUEUE_ #define _SIMULATION_LINKED_QUEUE_ #include "simulationdefault.h" struct Queue_Node_Type { Simulation_Customer data; struct Queue_Node_Type* Pointer_Next; }; struct Linked_Queue_Type { int Current_Element_Count; struct Queue_Node_Type* Pointer_Front_Node; struct Queue_Node_Type* Pointer_Rear_Node; }; typedef struct Queue_Node_Type Queue_Node; typedef struct Linked_Queue_Type Linked_Queue; Linked_Queue* Create_Linked_Queue(void); int Enqueue_Linked_Queue(Linked_Queue* Pointer_Queue, Queue_Node element); Queue_Node* Dequeue_Linked_Queue(Linked_Queue* Pointer_Queue); Queue_Node* Peek_Linked_Queue(Linked_Queue* Pointer_Queue); void Delete_Linked_Queue(Linked_Queue* Pointer_Queue); int Is_Linked_Queue_Full(Linked_Queue* Pointer_Queue); int Is_Linked_Queue_Empty(Linked_Queue* Pointer_Queue); #endif // !_SIMULATION_LINKED_QUEUE_ #ifndef _COMMON_QUEUE_DEFAULT_ #define _COMMON_QUEUE_DEFAULT_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_QUEUE_DEFAULT_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #ifndef _SIMULATION_DEFAULT_ #define _SIMULATION_DEFAULT_ enum Simulation_Status_Type { arrival, start, end }; struct Simulation_Customer_Type { enum Simulation_Status_Type status; int Arrival_Time; int Service_Time; int Start_Time; int End_Time; }; typedef enum Simulation_Status_Type Simulation_Status; typedef struct Simulation_Customer_Type Simulation_Customer; #endif // !_SIMULATION_DEFAULT_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #ifndef _SIMULATION_UTIL_ #define _SIMULATION_UTIL_ #include "simulationlinkedqueue.h" void Insert_Customer(int Arrival_Time, int Process_Time, Linked_Queue* Pointer_Queue); void Process_Arrival(int Current_Time, Linked_Queue* Pointer_Arrival_Queue, Linked_Queue* Pointer_Wait_Queue); Queue_Node* Process_Service_Node_Start(int Current_Time, Linked_Queue* Pointer_Wait_Queue); Queue_Node* Process_Service_Node_End(int Current_Time, Queue_Node* Pointer_Service_Node, int* Pointer_Service_User_Count, int* Pointer_Total_Wait_Time); void Print_Simulation_Customer(int Current_Time, Simulation_Customer customer); void Print_Wait_Queue_Status(int Current_Time, Linked_Queue* Pointer_Wiat_Queue); void Print_Report(Linked_Queue* Pointer_Wait_Queue, int Service_User_Count, int Total_Wait_Time); #endif // !_SIMULATION_UTIL_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "simulationdefault.h" #include "simulationlinkedqueue.h" Linked_Queue* Create_Linked_Queue(void) { Linked_Queue* Pointer_Return = (Linked_Queue*)malloc(1 * sizeof(Linked_Queue)); if ((Linked_Queue*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation. Create_Linked_Queue()\n"); return NULL; } (void*)memset((Linked_Queue*)Pointer_Return, 0, 1 * sizeof(Linked_Queue)); return (Linked_Queue*)Pointer_Return; } int Enqueue_Linked_Queue(Linked_Queue* Pointer_Queue, Queue_Node element) { if ((Linked_Queue*)Pointer_Queue == NULL) { printf("Error! The linked queue is null. Enqueue_Linked_Queue()\n"); return FALSE; } Queue_Node* Pointer_Node = (Queue_Node*)malloc(1 * sizeof(Queue_Node)); if ((Queue_Node*)Pointer_Node == NULL) { printf("Error! Dynamic memory allocation. Enqueue_Linked_Queue()\n"); return FALSE; } *(Queue_Node*)Pointer_Node = *(Queue_Node*)&element; (struct Queue_Node_Type*)Pointer_Node->Pointer_Next = NULL; if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) { (struct Queue_Node_Type*)Pointer_Queue->Pointer_Front_Node = (struct Queue_Node_Type*)Pointer_Node; (struct Queue_Node_Type*)Pointer_Queue->Pointer_Rear_Node = (struct Queue_Node_Type*)Pointer_Node; } else { (struct Queue_Node_Type*)Pointer_Queue->Pointer_Rear_Node->Pointer_Next = (struct Queue_Node_Type*)Pointer_Node; (struct Queue_Node_Type*)Pointer_Queue->Pointer_Rear_Node = (struct Queue_Node_Type*)Pointer_Node; } Pointer_Queue->Current_Element_Count++; return TRUE; } Queue_Node* Dequeue_Linked_Queue(Linked_Queue* Pointer_Queue) { if ((Linked_Queue*)Pointer_Queue == NULL) { printf("Error! The linked queue is null. Dequeue_Linked_Queue()\n"); return NULL; } else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) return NULL; Queue_Node* Pointer_Return = (Queue_Node*)Pointer_Queue->Pointer_Front_Node; (struct Queue_Node_Type*)Pointer_Queue->Pointer_Front_Node = (struct Queue_Node_Type*)Pointer_Queue->Pointer_Front_Node->Pointer_Next; (struct Queue_Node_Type*)Pointer_Return->Pointer_Next = NULL; Pointer_Queue->Current_Element_Count--; if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) { (struct Queue_Node_Type*)Pointer_Queue->Pointer_Rear_Node = NULL; } return (Queue_Node*)Pointer_Return; } Queue_Node* Peek_Linked_Queue(Linked_Queue* Pointer_Queue) { if ((Linked_Queue*)Pointer_Queue == NULL) return NULL; else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) return NULL; return (Queue_Node*) Pointer_Queue->Pointer_Front_Node; } void Delete_Linked_Queue(Linked_Queue* Pointer_Queue) { if ((Linked_Queue*)Pointer_Queue == NULL) return; Queue_Node* Pointer_Node = (Queue_Node*)Pointer_Queue->Pointer_Front_Node; while ((Queue_Node*)Pointer_Node != NULL) { Queue_Node* Pointer_Delete_Node = (Queue_Node*)Pointer_Node; (Queue_Node*)Pointer_Node = (Queue_Node*)Pointer_Node->Pointer_Next; free((Queue_Node*)Pointer_Delete_Node); } free((Queue_Node*)Pointer_Queue); return; } int Is_Linked_Queue_Full(Linked_Queue* Pointer_Queue) { return FALSE; } int Is_Linked_Queue_Empty(Linked_Queue* Pointer_Queue) { if ((Linked_Queue*)Pointer_Queue == NULL) return FALSE; else if (Pointer_Queue->Current_Element_Count == 0) return TRUE; return FALSE; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <stdio.h> #include <stdlib.h> #include "simulationutil.h" #include "simulationdefault.h" #include "simulationlinkedqueue.h" void Insert_Customer(int Arrival_Time, int Service_Time, Linked_Queue* Pointer_Queue) { if ((Linked_Queue*)Pointer_Queue == NULL) return; Queue_Node node = { 0 , }; (enum Simulation_Status_Type)node.data.status = arrival; node.data.Arrival_Time = Arrival_Time; node.data.Service_Time = Service_Time; node.data.Start_Time = 0; node.data.End_Time = 0; Enqueue_Linked_Queue((Linked_Queue*)Pointer_Queue, node); return; } void Process_Arrival(int Current_Time, Linked_Queue* Pointer_Arrival_Queue, Linked_Queue* Pointer_Wait_Queue) { while (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Arrival_Queue) == FALSE) { Queue_Node* Pointer_Arrival_Node = (Queue_Node*)Peek_Linked_Queue((Linked_Queue*)Pointer_Arrival_Queue); if (Pointer_Arrival_Node->data.Arrival_Time == Current_Time) { Enqueue_Linked_Queue((Linked_Queue*)Pointer_Wait_Queue, *(Queue_Node*)Pointer_Arrival_Node); Print_Simulation_Customer(Current_Time, *(Simulation_Customer*)&Pointer_Arrival_Node->data); free(Dequeue_Linked_Queue((Linked_Queue*)Pointer_Arrival_Queue)); } else break; } return; } Queue_Node* Process_Service_Node_End(int Current_Time, Queue_Node* Pointer_Service_Node, int* Pointer_Service_User_Count, int* Pointer_Total_Wait_Time) { if ((Queue_Node*)Pointer_Service_Node == NULL || Pointer_Service_User_Count == NULL || Pointer_Total_Wait_Time == NULL) return NULL; int End_Time = Pointer_Service_Node->data.Start_Time + Pointer_Service_Node->data.Service_Time; if (!(End_Time <= Current_Time)) return (Queue_Node*)Pointer_Service_Node; int Wait_Time = 0; Wait_Time = Pointer_Service_Node->data.Start_Time - Pointer_Service_Node->data.Arrival_Time; (*Pointer_Service_User_Count)++; (*Pointer_Total_Wait_Time) += Wait_Time; (enum Simulation_Status_Type)Pointer_Service_Node->data.status = end; Pointer_Service_Node->data.End_Time = Current_Time; Print_Simulation_Customer(Current_Time, *(Simulation_Customer*)&Pointer_Service_Node->data); free((Queue_Node*)Pointer_Service_Node); return NULL; } Queue_Node* Process_Service_Node_Start(int Current_Time, Linked_Queue* Pointer_Wait_Queue) { if ((Linked_Queue*)Pointer_Wait_Queue == NULL) return NULL; else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Wait_Queue)) return NULL; Queue_Node* Pointer_Service_Node = Dequeue_Linked_Queue((Linked_Queue*)Pointer_Wait_Queue); if (Pointer_Service_Node == NULL) return NULL; (enum Simulation_Status_Type)Pointer_Service_Node->data.status = start; Pointer_Service_Node->data.Start_Time = Current_Time; Print_Simulation_Customer(Current_Time, *(Simulation_Customer*)&Pointer_Service_Node->data); return (Queue_Node*)Pointer_Service_Node; } void Print_Simulation_Customer(int Current_Time, Simulation_Customer Customer) { printf("[%d], ", Current_Time); switch (Customer.status) { case arrival: printf("Customer arrival\n"); break; case start: printf("Start service"); printf(", arrival-[%d], waiting time-[%d]\n", Customer.Arrival_Time, (Customer.Start_Time - Customer.Arrival_Time)); break; case end: printf("End service"); printf(", arrival-[%d], start time-[%d], waiting time-[%d], service time-[%d]\n", Customer.Arrival_Time, Customer.Start_Time, Customer.Start_Time - Customer.Arrival_Time, Customer.End_Time - Customer.Start_Time); break; default: printf("Error!\n"); break; } return; } void Print_Wait_Queue_Status(int Current_Time, Linked_Queue* Pointer_Wait_Queue) { printf("[%d], Current waiting customer count : %d\n", Current_Time, Pointer_Wait_Queue->Current_Element_Count); return; } void Print_Report(Linked_Queue* Pointer_Wait_Queue, int Service_User_Count, int total_Wait_Time) { printf("REPORT\n"); printf("Number of service customers : %d\n", Service_User_Count); if (Service_User_Count > 0) printf("Average wait time : %f\n", ((float)total_Wait_Time / (float)Service_User_Count)); printf("Number of customers in the current queue : %d\n", Pointer_Wait_Queue->Current_Element_Count); return; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> #include <stdlib.h> #include "simulationdefault.h" #include "simulationlinkedqueue.h" #include "simulationutil.h" int main(int argc, char** argv) { Linked_Queue* Pointer_Arrival_Queue = Create_Linked_Queue(); Linked_Queue* Pointer_Wait_Queue = Create_Linked_Queue(); if ((Linked_Queue*)Pointer_Arrival_Queue == NULL && (Linked_Queue*)Pointer_Wait_Queue == NULL) return FALSE; Insert_Customer(0, 3, (Linked_Queue*)Pointer_Arrival_Queue); Insert_Customer(2, 2, (Linked_Queue*)Pointer_Arrival_Queue); Insert_Customer(4, 1, (Linked_Queue*)Pointer_Arrival_Queue); Insert_Customer(6, 1, (Linked_Queue*)Pointer_Arrival_Queue); Insert_Customer(8, 3, (Linked_Queue*)Pointer_Arrival_Queue); Queue_Node* Pointer_Service_Node = NULL; int Service_User_Count = 0, Total_Wait_Time = 0; int Current_Time = 0, End_Time = 10; for (Current_Time = 0; Current_Time < End_Time; Current_Time++) { Process_Arrival(Current_Time, (Linked_Queue*)Pointer_Arrival_Queue, (Linked_Queue*)Pointer_Wait_Queue); // service 'end' processing if (Pointer_Service_Node != NULL) { (Queue_Node*)Pointer_Service_Node = (Queue_Node*)Process_Service_Node_End(Current_Time, (Queue_Node*)Pointer_Service_Node, &Service_User_Count, &Total_Wait_Time); } // service 'start' processing if (Pointer_Service_Node == NULL) { (Queue_Node*)Pointer_Service_Node = (Queue_Node*)Process_Service_Node_Start(Current_Time, (Linked_Queue*)Pointer_Wait_Queue); } Print_Wait_Queue_Status(Current_Time, (Linked_Queue*)Pointer_Wait_Queue); } Print_Report((Linked_Queue*)Pointer_Wait_Queue, Service_User_Count, Total_Wait_Time); // free memory if ((Queue_Node*)Pointer_Arrival_Queue != NULL) free((Queue_Node*)Pointer_Service_Node); Delete_Linked_Queue((Linked_Queue*)Pointer_Arrival_Queue); Delete_Linked_Queue((Linked_Queue*)Pointer_Wait_Queue); return 0; } | cs |
0 개의 댓글:
댓글 쓰기