Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2019년 4월 8일 월요일

simulation test (used queue)


"linkedqueue.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
#ifndef _LINKED_QUEUE_
#define _LINKED_QUEUE_
 
#include "simulation.h"
 
struct Queue_Node_Type {
 
    struct Simulation_Students_Type Students_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;
};
 
struct Linked_Queue_Type* Create_Linked_Queue(void);
void Delete_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue);
 
int Enqueue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue, struct Queue_Node_Type element);
struct Queue_Node_Type* Dequeue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue);
struct Queue_Node_Type* Peek_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue);
 
int Is_Linked_Queue_Empty(struct Linked_Queue_Type* Pointer_Queue);
 
#endif // !_LINKED_QUEUE_
 
#ifndef _COMMON_QUEUE_DEFAULT_
#define _COMMON_QUEUE_DEFAULT_
 
#define TRUE    1
#define FALSE    0
 
#endif // !_COMMON_QUEUE_DEFAULT_
 
cs
"simulationProcessing.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef _SIMULATION_PROCESSING_
#define _SIMULATION_PROCESSING_
 
void Insert_Students(struct Linked_Queue_Type* Pointer_Queue, int Arrival_Time, char* name, enum Gender_Type gender);
int Distribution_By_Students_Gender(struct Linked_Queue_Type* Pointer_Queue, struct Linked_Queue_Type* Male_Students_Room, struct Linked_Queue_Type* Female_Students_Room);
void Make_Couple(struct Linked_Queue_Type* Male_Students_Room, struct Linked_Queue_Type* Female_Students_Room, int limit);
void Student_Processing(struct Linked_Queue_Type* Waiting_Room, struct Linked_Queue_Type* Students_Room, enum Gender_Type gender, int time);
void Display_Couple_Succeed(struct Queue_Node_Type* student, struct Queue_Node_Type* partner, int time);
 
#endif // !_SIMULATION_PROCESSING_
 
#ifndef _COMMON_SIMULATION_DEFAULT_
#define _COMMON_SIMULATION_DEFAULT_
 
#define ERROR    -99999
 
#endif // !_COMMON_QUEUE_DEFAULT_
cs
"simulation.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _SIMULATION_
#define _SIMULATION_
 
enum Gender_Type {
 
    Male,
    Female
};
 
struct Simulation_Students_Type {
 
    int Arrival_Time;
    int Delay_Time;
    
    char* name;
    enum Gender_Type gender;
};
 
#endif // !_SIMULATION_
cs
"linkedqueue.c"
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedqueue.h"
 
struct Linked_Queue_Type* Create_Linked_Queue(void) {
 
    struct Linked_Queue_Type* Pointer_Return = (struct Linked_Queue_Type*malloc(1 * sizeof(struct Linked_Queue_Type));
    if ((struct Linked_Queue_Type*)Pointer_Return == NULL) {
 
        printf("Error! Dynamic memory allocation failed()\n");
        return NULL;
    }
    (void*)memset((struct Linked_Queue_Type*)Pointer_Return, 01 * sizeof(struct Linked_Queue_Type));
    
    return (struct Linked_Queue_Type*)Pointer_Return;
}
void Delete_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) {
 
    struct Queue_Node_Type* Pointer_Node = Pointer_Queue->Pointer_Front_Node;
    while ((struct Queue_Node_Type*) Pointer_Node) {
 
        struct Queue_Node_Type* Pointer_Delete_Node = Pointer_Node;
        Pointer_Node = (struct Queue_Node_Type*)Pointer_Node->Pointer_Next;
        free((struct Queue_Node_Type*)Pointer_Delete_Node);
    }
    if ((struct Linked_Queue_Type*) Pointer_Queue != NULLfree((struct Linked_Queue_Type*) Pointer_Queue);
 
    return;
}
 
int Enqueue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue, struct Queue_Node_Type element) {
 
    if ((struct Linked_Queue_Type*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Enqueue_Linked_Queue()\n");
        return FALSE;
    }
 
    struct Queue_Node_Type* Pointer_Node = (struct Queue_Node_Type*malloc(1 * sizeof(struct Queue_Node_Type));
    if ((struct Queue_Node_Type*)Pointer_Node == NULL) {
 
        printf("Error! Dynamic memory allocation failed. Enqueue_Linked_Queue()\n");
        return FALSE;
    }
    (void*)memset((struct Queue_Node_Type*)Pointer_Node, 01 * sizeof(struct Queue_Node_Type));
 
    *Pointer_Node = *(struct Queue_Node_Type*)&element;
    Pointer_Node->Pointer_Next = NULL;
 
    if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Pointer_Queue)) {
 
        Pointer_Queue->Pointer_Front_Node = (struct Queue_Node_Type*)Pointer_Node;
        Pointer_Queue->Pointer_Rear_Node = (struct Queue_Node_Type*)Pointer_Node;
    }
    else {
 
        Pointer_Queue->Pointer_Rear_Node->Pointer_Next = (struct Queue_Node_Type*)Pointer_Node;
        Pointer_Queue->Pointer_Rear_Node = (struct Queue_Node_Type*)Pointer_Node;
    }
    Pointer_Queue->Current_Element_Count++;
 
    return TRUE;
}
struct Queue_Node_Type* Dequeue_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) {
 
    if ((struct Linked_Queue_Type*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Dequeue_Linked_Queue()\n");
        return    NULL;
    }
    else if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Pointer_Queue)) return NULL;
 
    struct Queue_Node_Type* Pointer_Return = (struct Queue_Node_Type*) Pointer_Queue->Pointer_Front_Node;
    Pointer_Queue->Pointer_Front_Node = (struct Queue_Node_Type*)Pointer_Queue->Pointer_Front_Node->Pointer_Next;
    Pointer_Return->Pointer_Next = NULL;
 
    Pointer_Queue->Current_Element_Count--;
    if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Pointer_Queue)) {
 
        Pointer_Queue->Pointer_Rear_Node = NULL;
    }
 
    return (struct Queue_Node_Type*)Pointer_Return;
}
 
struct Queue_Node_Type* Peek_Linked_Queue(struct Linked_Queue_Type* Pointer_Queue) {
 
    if ((struct Linked_Queue_Type*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Peek_Linked_Queue()\n");
        return    NULL;
    }
    else if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Pointer_Queue)) return NULL;
 
    return (struct Queue_Node_Type*)Pointer_Queue->Pointer_Front_Node;
}
 
int Is_Linked_Queue_Empty(struct Linked_Queue_Type* Pointer_Queue) {
 
    if ((struct Linked_Queue_Type*)Pointer_Queue == NULLreturn FALSE;
    else if (!Pointer_Queue->Current_Element_Count) return TRUE;
    else return FALSE;
}
cs
"sinulationProcessing.c"
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include "linkedqueue.h"
#include "simulationProcessing.h"
 
void Insert_Students(struct Linked_Queue_Type* Pointer_Queue, int Arrival_Time, char* name, enum Gender_Type gender) {
 
    if ((struct Linked_Queue_Type*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Insert_Students()\n");
        return;
    }
 
    struct Queue_Node_Type node = { 0, };
    node.Students_Data.Arrival_Time = Arrival_Time;
    node.Students_Data.name = name;
    node.Students_Data.gender = gender;
 
    Enqueue_Linked_Queue((struct Linked_Queue_Type*)Pointer_Queue, node);
 
    return;
}
 
int Distribution_By_Students_Gender(struct Linked_Queue_Type* Pointer_Queue, struct Linked_Queue_Type* Male_Students_Room, struct Linked_Queue_Type* Female_Students_Room) {
 
    if ((struct Linked_Queue_Type*)Pointer_Queue == NULL) {
 
        printf("Error! The linked queue is null. Distribution_By_Students_Gender()\n");
        return ERROR;
    }
 
    int Count_Male = 0, Count_Female = 0;
    while (!Is_Linked_Queue_Empty ((struct Linked_Queue_Type*)Pointer_Queue)) {
 
        struct Queue_Node_Type* Pointer_Students_Data = (struct Queue_Node_Type*)Dequeue_Linked_Queue((struct Linked_Queue_Type*)Pointer_Queue);
        if (Pointer_Students_Data->Students_Data.gender == Male) {
 
            Enqueue_Linked_Queue((struct Linked_Queue_Type*)Male_Students_Room, *(struct Queue_Node_Type*)Pointer_Students_Data);
            Count_Male++;
        }
        else if (Pointer_Students_Data->Students_Data.gender == Female) {
 
            Enqueue_Linked_Queue((struct Linked_Queue_Type*)Female_Students_Room, *(struct Queue_Node_Type*)Pointer_Students_Data);
            Count_Female++;
        }
        else {
 
            printf("Error! Can not understand gender. Distribution_By_Students_Gender()\n");
            free((struct Queue_Node_Type*)Pointer_Students_Data);
 
            return ERROR;
        }
        free((struct Queue_Node_Type*)Pointer_Students_Data);
    }
 
    return Count_Male - Count_Female;
}
 
void Make_Couple(struct Linked_Queue_Type* Male_Students_Room, struct Linked_Queue_Type* Female_Students_Room, int limit) {
 
    if ((struct Linked_Queue_Type*)Male_Students_Room == NULL) {
 
        printf("Error! The linked queue is null(1). Make_Couple()\n");
        return;
    }
    else if ((struct Linked_Queue_Type*)Female_Students_Room == NULL) {
 
        printf("Error! The linked queue is null(2). Make_Couple()\n");
        return;
    }
 
    struct Linked_Queue_Type* Waiting_Room = (struct Linked_Queue_Type*) Create_Linked_Queue();
 
    int Count_Time = 0, Count_Male_Students = 0, Count_Female_Students = 0, check = 0;
    while (!Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Male_Students_Room) || !Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Female_Students_Room)) {
        if (limit < Count_Time) break;
 
        struct Queue_Node_Type* Peek_Student = (struct Queue_Node_Type*)Peek_Linked_Queue((struct Linked_Queue_Type*)Male_Students_Room);
        if ((struct Queue_Node_Type*)Peek_Student != NULL) {
 
            if (Peek_Student->Students_Data.Arrival_Time < Count_Time) Count_Time--;
            if (Peek_Student->Students_Data.Arrival_Time == Count_Time) {
 
                Student_Processing((struct Linked_Queue_Type*) Waiting_Room, (struct Linked_Queue_Type*) Male_Students_Room, Male, Count_Time);
                Count_Male_Students++, check = 1;
            }
        }
 
        Peek_Student = (struct Queue_Node_Type*)Peek_Linked_Queue((struct Linked_Queue_Type*)Female_Students_Room);
        if ((struct Queue_Node_Type*)Peek_Student != NULL) {
 
            if (Peek_Student->Students_Data.Arrival_Time < Count_Time) Count_Time--;
            if (Peek_Student->Students_Data.Arrival_Time == Count_Time) {
 
                Student_Processing((struct Linked_Queue_Type*) Waiting_Room, (struct Linked_Queue_Type*) Female_Students_Room, Female, Count_Time);
                Count_Female_Students++, check = 1;
            }
        }
 
        if (!Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Waiting_Room) && !check) {
 
            printf("[%2d] 대기 인원 수 : %d\n", Count_Time, Waiting_Room->Current_Element_Count);
        }
        else if (Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Waiting_Room) && !check) {
 
            printf("[%2d] == Empty ==\n", Count_Time);
        }
        Count_Time++, check = 0;
    }
 
    Delete_Linked_Queue((struct Linked_Queue_Type*)Waiting_Room);
 
    return;
}
 
void Student_Processing(struct Linked_Queue_Type* Waiting_Room, struct Linked_Queue_Type* Students_Room, enum Gender_Type gender, int time) {
 
    if ((struct Linkd_Queue_Type*)Waiting_Room == NULLreturn;
    else if ((struct Linked_Queue_Type*)Students_Room == NULLreturn;
 
    struct Queue_Node_Type* student = (struct Queue_Node_Type*)Dequeue_Linked_Queue((struct Linked_Queue_Type*)Students_Room);
    if (!Is_Linked_Queue_Empty((struct Linked_Queue_Type*)Waiting_Room) && Waiting_Room->Pointer_Front_Node->Students_Data.gender != gender) {
 
        struct Queue_Node_Type* Partner = (struct Queue_Node_Type*) Dequeue_Linked_Queue((struct Linked_Queue_Type*)Waiting_Room);
        Partner->Students_Data.Delay_Time = Partner->Students_Data.Arrival_Time - student->Students_Data.Arrival_Time;
 
        Display_Couple_Succeed((struct Queue_Node_Type*) student, (struct Queue_Node_Type*) Partner, time);
        printf("[%2d] 대기 인원 수 : %d\n", time, Waiting_Room->Current_Element_Count);
 
        free((struct Queue_Node_Type*)Partner);
    }
    else {
 
        Insert_Students((struct Linked_Queue_Type*) Waiting_Room, student->Students_Data.Arrival_Time, student->Students_Data.name, student->Students_Data.gender);
        printf("[%2d] 대기열 추가 <%s>\n", time, Waiting_Room->Pointer_Rear_Node->Students_Data.name);
        printf("[%2d] 대기 인원 수 : %d\n", time, Waiting_Room->Current_Element_Count);
    }
    free((struct Queue_Node_Type*)student);
 
    return;
}
 
void Display_Couple_Succeed(struct Queue_Node_Type* student, struct Queue_Node_Type* partner, int time) {
 
    if ((struct Queue_Node_Type*)student == NULLreturn;
    else if ((struct Queue_Node_Type*) partner == NULLreturn;
 
    if ((*student).Students_Data.gender == Male) {
 
        printf("[%2d] 커플 탄생! (男)%s ♡ %s(女) COUPLE\n", time, student->Students_Data.name, partner->Students_Data.name);
        printf("[%2d] 남학생 대기시간 : %d,    ", time, student->Students_Data.Delay_Time);
        printf("여학생 대기시간 : %d\n", partner->Students_Data.Delay_Time);
    }
    else {
 
        printf("[%2d] 커플 탄생! (男)%s ♡ %s(女) COUPLE\n", time, partner->Students_Data.name, student->Students_Data.name);
        printf("[%2d] 남학생 대기시간 : %d,    ", time, partner->Students_Data.Delay_Time);
        printf("여학생 대기시간 : %d\n", student->Students_Data.Delay_Time);
    }
    return;
}
cs
"main.c"
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
#include <stdio.h>
#include "linkedqueue.h"
#include "simulationProcessing.h"
 
#define LIMIT    15
 
int main(int argc, char** argv) {
 
    typedef struct Linked_Queue_Type Linked_Queue;
    typedef struct Queue_Node_Type Queue_Node;
 
    Linked_Queue* Pointer_Queue = (Linked_Queue*)Create_Linked_Queue();
    if ((Linked_Queue*)Pointer_Queue == NULLreturn -1;
 
    Insert_Students((Linked_Queue*)Pointer_Queue, 0"광만", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 3"민아", Female);
    Insert_Students((Linked_Queue*)Pointer_Queue, 4"태수", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 4"광수", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 7"지영", Female);
    Insert_Students((Linked_Queue*)Pointer_Queue, 10"민수", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 11"아름", Female);
    Insert_Students((Linked_Queue*)Pointer_Queue, 11"민준", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 15"수영", Female);
    Insert_Students((Linked_Queue*)Pointer_Queue, 16"규현", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 18"민후", Male);
    Insert_Students((Linked_Queue*)Pointer_Queue, 21"지아", Female);
 
    Linked_Queue* Male_Students_Room = (Linked_Queue*)Create_Linked_Queue();
    Linked_Queue* Female_Students_Room = (Linked_Queue*)Create_Linked_Queue();
 
    Distribution_By_Students_Gender((Linked_Queue*)Pointer_Queue, (Linked_Queue*)Male_Students_Room, (Linked_Queue*)Female_Students_Room);
    Make_Couple((Linked_Queue*)Male_Students_Room, (Linked_Queue*)Female_Students_Room, LIMIT);
 
    Delete_Linked_Queue((Linked_Queue*)Male_Students_Room);
    Delete_Linked_Queue((Linked_Queue*)Female_Students_Room);
 
    return 0;
}
cs

0 개의 댓글:

댓글 쓰기