-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcong_tac_den.c
309 lines (295 loc) · 11.1 KB
/
cong_tac_den.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
*Den trong phong hoc thuc hanh C nhu 1 ma tran 5*3
*Den sang: 1
*Den toi: 0
*Cong suat tieu thu cua cac den:
- VT hang le cot le: 10W
- VT hang chan cot chan: 20W
- con lai: 15W
*Viet chtrinh menu:
-1. ng dung bat tat den theo hang
-2. ng dung bat tat den theo cot
-3. ng dung bat tat den theo VT
-4. Xem cong suat tieu thu cua den theo trang thai hien thoi
*/
#include <stdio.h>
#define MAX 100
int luachon, i, j, condition;
int s[MAX][MAX], dien[MAX][MAX];
void menu(); // function propotype
void row(); // function propotype
void column(); // function propotype
void vitri(); // function propotype
void congsuat(); // function propotype
void error();
int main ( void )
{
do {
menu();
switch ( luachon ){
case 1 : row();break;
case 2 : column();break;
case 3 : vitri();break;
case 4 : congsuat();break;
case 5 : break;
default : error();break;
}
} while ( luachon != 5);
}
void menu()
{
printf ("_______________________________________\n");
printf ("| MENU |\n");
printf ("|--------------------------------------|\n");
printf ("| 1 . Bat tat den theo hang |\n");
printf ("| 2 . Bat tat den theo cot |\n");
printf ("| 3 . Bat tat den theo vi tri |\n");
printf ("| 4 . Cong suat tieu thu cua dan den |\n");
printf ("| 5 . Thoat |\n");
printf ("|______________________________________|\n");
printf ("Lua chon cua ban : "); fflush(stdin); scanf("%d", &luachon);
}
void row()
{
int a, b;
do {
printf("________________________\n");
printf("| BANG LUA CHON |\n");
printf("|-----------------------|\n");
printf("| 1 . Bat den |\n");
printf("| 2 . Tat den |\n");
printf("| 3 . Thoat option |\n");
printf("|_______________________|\n");
printf("Lua chon cua ban: "); fflush(stdin); scanf("%d", &condition);
switch ( condition){
case 1:
printf("Hang ban muon bat den: "); fflush(stdin); scanf("%d", &a);
if ( a > 5 || a < 1) {
do {
printf ("Ban chi co the chon hang tu 1 den 5\n");
printf("VUI LONG NHAP LAI\n");
printf("Hang ban muon bat den: "); fflush(stdin); scanf("%d", &a);
} while (a > 5 || a < 1);
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
if ( i == a-1 ) s[i][j] = 1;
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", s[i][j]);
}
printf("\n\n");
}
break;
case 2:
printf("Hang ban muon tat den: "); fflush(stdin); scanf("%d", &b);
if ( b > 5 || b < 1) {
do {
printf ("Ban chi co the chon hang tu 1 den 5\n");
printf("VUI LONG NHAP LAI\n");
printf("Hang ban muon tat den: "); fflush(stdin); scanf("%d", &b);
} while (b > 5 || b < 1);
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
;
}
}
if ( s[0][j] == 0 || s[1][j] == 0 || s[2][j] == 0 || s[3][j] == 0 || s[4][j] == 0){
printf("Hang nay khong co bong nao bat\n");
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
if ( i == b-1 ) s[i][j] = 0;
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", s[i][j]);
}
printf("\n\n");
}
break;
case 3: break;
default :
printf(" ******* ERROR ********* \n");
printf("WARNING: Lua chon cua ban la %d khong nam trong menu\n", condition);
printf("HINT: Ban chi co the chon tu 1 den 3\n");
break;
}
} while ( condition != 3);
}
void column()
{
int a, b;
do {
printf("________________________\n");
printf("| BANG LUA CHON |\n");
printf("|-----------------------|\n");
printf("| 1 . Bat den |\n");
printf("| 2 . Tat den |\n");
printf("| 3 . Thoat option |\n");
printf("|_______________________|\n");
printf("Lua chon cua ban: "); fflush(stdin); scanf("%d", &condition);
switch ( condition){
case 1:
printf("Cot ban muon bat den: "); fflush(stdin); scanf("%d", &a);
if ( a > 3 || a < 1) {
do {
printf ("Ban chi co the chon cot tu 1 den 3\n");
printf("VUI LONG NHAP LAI\n");
printf("Cot ban muon bat den: "); fflush(stdin); scanf("%d", &a);
} while (a > 3 || a < 1);
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
if ( j == a-1 ) s[i][j] = 1;
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", s[i][j]);
}
printf("\n\n");
}
break;
case 2:
printf("Cot ban muon tat den: "); fflush(stdin); scanf("%d", &b);
if ( b > 3 || b < 1) {
do {
printf ("Ban chi co the chon cot tu 1 den 3\n");
printf("VUI LONG NHAP LAI\n");
printf("Cot ban muon tat den: "); fflush(stdin); scanf("%d", &b);
} while (b > 3 || b < 1);
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
;
}
}
if ( s[i][0] == 0 || s[i][1] == 0 || s[i][2] == 0){
printf("Cot nay khong co bong nao bat\n");
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
if ( j == b-1 ) s[i][j] = 0;
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", s[i][j]);
}
printf("\n\n");
}
break;
case 3: break;
default :
printf(" ******* ERROR ********* \n");
printf("WARNING: Lua chon cua ban la %d khong nam trong option\n", condition);
printf("HINT: Ban chi co the chon tu 1 den 3\n");
break;
}
} while ( condition != 3);
}
void vitri()
{
int a, b, c, d;
do {
printf("________________________\n");
printf("| BANG LUA CHON |\n");
printf("|-----------------------|\n");
printf("| 1 . Bat den |\n");
printf("| 2 . Tat den |\n");
printf("| 3 . Thoat option |\n");
printf("|_______________________|\n");
printf("Lua chon cua ban: "); fflush(stdin); scanf("%d", &condition);
switch ( condition){
case 1:
printf("Vi tri ban muon bat den ( row, column ): "); fflush(stdin); scanf("%d, %d", &a, &b);
if ( (a > 5 || a < 1) && (b < 1 || b > 3) ) {
do {
printf ("Ban chi co the chon hang tu 1 den 5. chon cot tu 1 den 3\n");
printf("VUI LONG NHAP LAI\n");
printf("Vi tri ban muon bat den ( row, column ): "); fflush(stdin); scanf("%d, %d", &a, &b);
} while ((a > 5 || a < 1) && (b < 1 || b > 3) );
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
if ((i == a-1) && (j == b-1)) s[i][j] = 1;
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", s[i][j]);
}
printf("\n\n");
}
break;
case 2:
printf("Vi tri ban muon tat den ( row, column ): "); fflush(stdin); scanf("%d, %d", &c, &d);
if ( c > 5 || c < 1 || d < 1 || d > 3 ) {
do {
printf ("Ban chi co the chon hang tu 1 den 5. chon cot tu 1 den 3\n");
printf("VUI LONG NHAP LAI\n");
printf("Vi tri ban muon tat den ( row, column ): "); fflush(stdin); scanf("%d, %d", &c, &d);
} while (c > 5 || c < 1 || d < 1 || d > 3 );
}
for ( i = 0; i < 5; i++){
for( j = 0; j < 3; j++){
if ((i == c-1) && (j == d-1)) s[i][j] = 0;
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", s[i][j]);
}
printf("\n\n");
}
break;
case 3: break;
default :
printf(" ******* ERROR ********* \n");
printf("WARNING: Lua chon cua ban la %d khong nam trong option\n", condition);
printf("HINT: Ban chi co the chon tu 1 den 3\n");
break;
}
} while ( condition != 3);
}
void congsuat()
{
int temp, temp1, sum=0;
for (i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
if (s[i][j] == 1) {
if (((i+1)%2==1)&&((j+1)%2==1))
dien[i][j] = 10;
if (((i+1)%2==0)&&((j+1)%2==0))
dien[i][j] = 20;
if ((((i+1)%2==1)&&((j+1)%2==0)) || (((i+1)%2==0)&&((j+1)%2==1)))
dien[i][j] = 15;
}
}
}
printf("\n");
for ( i = 0; i < 5; i++){
for ( j = 0; j < 3; j++){
printf("%5d", dien[i][j]);
sum += dien[i][j];
}
printf("\n\n");
}
printf ("Tong cong suat tieu thu cua dan den hien tai : %d\n", sum);
}
void error(){
printf(" ******* ERROR ********* \n");
printf("WARNING: Lua chon cua ban la %d khong nam trong menu\n", luachon);
printf("HINT: Ban chi co the chon option tu 1 den 5\n");
}