Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #define ROW 5
- #define COL 3
- #define O_O 10
- #define E_E 20
- #define O_E 15
- #define E_O 15
- int isEven (int n) {
- return !(n & 1);
- }
- int GetPower (int state, int row, int col) {
- if (state) {
- if (isEven(row) && isEven(col))
- return E_E;
- else if (!isEven(row) && !isEven(col))
- return O_O;
- else return O_E;
- } else return 0;
- }
- int PrintPower (int state[ROW][COL]) {
- int i, j;
- printf("> Cong suat tieu thu hien thoi cua cac den la:\n");
- for (i = 0; i < ROW; ++i) {
- for (j = 0; j < COL; ++j) {
- printf("%d ", GetPower(state[i][j], i, j));
- }
- printf("\n");
- }
- return 0;
- }
- int Input(int min, int max) {
- int tmp, isInvalid, pos;
- do {
- tmp = scanf("%d", &pos);
- while (getchar() != '\n');
- if (tmp != 1 || (pos < min || pos > max)) {
- printf("!!! Vui long nhap dung so tu %d den %d !!!\n", min, max);
- isInvalid = 1;
- } else isInvalid = 0;
- } while (isInvalid);
- return pos;
- }
- int main () {
- int isOut = 0, isInvalid, choice, tmp;
- int state[ROW][COL] = {0};
- int posRow, posCol, selectState, i, j;
- do {
- printf("\n----------------------------------------------\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 hien thoi\n");
- printf("0. Thoat\n");
- printf("----------------------------------------------\n");
- printf("> Nhap lua chon: ");
- do {
- tmp = scanf("%d", &choice);
- while (getchar() != '\n');
- if (tmp != 1) {
- printf("!!! Vui long nhap dung lua chon !!!\n");
- isInvalid = 1;
- } else isInvalid = 0;
- } while (isInvalid);
- switch (choice) {
- case 1:
- printf("\n----------------------------------------------\n");
- printf("> Ban muon tat hay bat (0 = Tat, 1 = Bat): ");
- selectState = Input(0, 1);
- printf("> Nhap hang can %s: ", selectState ? "Bat" : "Tat");
- posRow = Input(1, ROW);
- for (j = 0; j < COL; ++j)
- state[posRow-1][j] = selectState;
- printf(">>> Da %s hang %d thanh cong !\n", selectState ? "Bat" : "Tat", posRow);
- printf("----------------------------------------------\n");
- break;
- case 2:
- printf("\n--------------------------------------------\n");
- printf("> Ban muon tat hay bat (0 = Tat, 1 = Bat): ");
- selectState = Input(0, 1);
- printf("> Nhap cot can %s: ", selectState ? "Bat" : "Tat");
- posCol = Input(1, COL);
- for (i = 0; i < ROW; ++i)
- state[i][posCol-1] = selectState;
- printf(">>> Da %s cot %d thanh cong !\n", selectState ? "Bat" : "Tat", posCol);
- printf("----------------------------------------------\n");
- break;
- case 3:
- printf("\n----------------------------------------------\n");
- printf("> Ban muon tat hay bat (0 = Tat, 1 = Bat): ");
- selectState = Input(0, 1);
- printf("> Nhap toa do hang can %s: ", selectState ? "Bat" : "Tat");
- posRow = Input(1, ROW);
- printf("> Nhap toa do cot can %s: ", selectState ? "Bat" : "Tat");
- posCol = Input(1, COL);
- state[posCol-1][posCol-1] = selectState;
- printf(">>> Da %s den (%d, %d) thanh cong !\n", selectState ? "Bat" : "Tat", posRow, posCol);
- printf("----------------------------------------------\n");
- break;
- case 4:
- printf("\n----------------------------------------------\n");
- PrintPower(state);
- printf("----------------------------------------------\n");
- break;
- case 0:
- isOut = 1;
- default:
- printf("!!! Vui long nhap lua chon tu 0 den 4 !!!\n");
- }
- } while (!isOut);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement