-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (158 loc) · 4.2 KB
/
main.cpp
File metadata and controls
175 lines (158 loc) · 4.2 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include<graphics.h>
#define n 6
using namespace std;
int count = 0,i;
class stick
{
public:
int used;
}chop[n];
class philospher
{
public:
int left;
int right;
}P[n];
void func(int name)
{
if(P[name].left==-1 && P[name].right==-1)
cout<<"Philosopher "<<name+1<<" completed his Lunch\n";
else if(P[name].left==1 && P[name].right==1)
{
cout<<"Philosopher "<<name+1<<" completed his Lunch\n";
P[name].left = P[name].right = -1;
int fork = name-1;
if(fork== -1)
fork=(n-1);
chop[name].used = chop[fork].used = 0;
cout<<"Philosopher "<<name+1<<" released chopstick "<<name+1<<" and chopstick "<<fork+1<<"\n";
count++;
}
else if(P[name].left==1 && P[name].right==0)
{
if(name==(n-1))
{
if(chop[name].used==0)
{
chop[name].used = P[name].right = 1;
cout<<"Chopstick "<<name+1<<" taken by philosopher "<<name+1<<"\n";
}
else
{
cout<<"Philosopher "<<name+1<<" is waiting for chopstick "<<name+1<<"\n";
}
}
else
{
int vr = name;
name-=1;
if(name== -1)
name=(n-1);
if(chop[name].used == 0)
{
chop[name].used = P[vr].right = 1;
cout<<"Chopstick "<<name+1<<" taken by Philosopher "<<vr+1<<"\n";
}
else
{
cout<<"Philosopher "<<vr+1<<" is waiting for Chopstick "<<name+1<<"\n";
}
}
}
else if(P[name].left==0)
{
if(name==(n-1))
{
if(chop[name-1].used==0)
{
chop[name-1].used = P[name].left = 1;
cout<<"Chopstick "<<name<<" taken by philosopher "<<name+1<<"\n";
}
else
{
cout<<"Philosopher "<<name+1<<" is waiting for chopstick "<<name<<"\n";
}
}
else
{
if(chop[name].used == 0)
{
chop[name].used = P[name].left = 1;
cout<<"Chopstick "<<name+1<<" taken by Philosopher "<<name+1<<"\n";
}
else
{
cout<<"Philosopher "<<name+1<<" is waiting for Chopstick "<<name+1<<"\n";
}
}
}
}
int main()
{
int gd=DETECT,gm,num,j,k;
initwindow(800,700);
// GUI
setfillstyle(XHATCH_FILL,BLUE);
floodfill(550,400,WHITE);
circle(400,350,200);
circle(400,225,35);
circle(400,475,35);
circle(275,350,35);
circle(525,350,35);
line(435,305,505,235);
line(365,305,295,235);
line(365,395,295,465);
line(435,395,505,465);
rectangle(380,80,420,120);
outtextxy(393,93,"P1");
setfillstyle(SLASH_FILL,RED);
floodfill(390,90,WHITE);
rectangle(380,580,420,620);
outtextxy(393,593,"P3");
setfillstyle(SLASH_FILL,RED);
floodfill(390,590,WHITE);
rectangle(130,330,170,370);
outtextxy(143,343,"P2");
setfillstyle(SLASH_FILL,RED);
floodfill(140,340,WHITE);
rectangle(630,330,670,370);
outtextxy(643,343,"P4");
setfillstyle(SLASH_FILL,RED);
floodfill(640,340,WHITE);
// CODE
cout<<"Enter the number of Philosophers : ";
cin>>num;
for(j=0;j<num;j++)
chop[j].used=P[j].left=P[j].right=0;
while(count<num)
{
for(j=0;j<num;j++)
func(j);
cout<<"\n Number of Philosophers who completed their Lunch : "<<count<<"\n\n";
if(count==1)
{
setfillstyle(SOLID_FILL,GREEN);
floodfill(410,230,WHITE);
}
if(count==2)
{
setfillstyle(SOLID_FILL,YELLOW);
floodfill(280,360,WHITE);
}
if(count==3)
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
floodfill(410,480,WHITE);
}
if(count==4)
{
setfillstyle(SOLID_FILL,WHITE);
floodfill(530,360,WHITE);
}
delay(2000);
}
getch();
closegraph();
return 0;
}