Advertisement
pdaogu

HW10.2

Nov 13th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define ROW 5
  5. #define COL 3
  6.  
  7. #define O_O 10
  8. #define E_E 20
  9. #define O_E 15
  10. #define E_O 15
  11.  
  12. int isEven (int n) {
  13.     return !(n & 1);
  14. }
  15.  
  16. int GetPower (int state, int row, int col) {
  17.     if (state) {
  18.         if (isEven(row) && isEven(col))
  19.             return E_E;
  20.         else if (!isEven(row) && !isEven(col))
  21.             return O_O;
  22.         else return O_E;
  23.     } else return 0;
  24. }
  25.  
  26. int PrintPower (int state[ROW][COL]) {
  27.     int i, j;
  28.     printf("> Cong suat tieu thu hien thoi cua cac den la:\n");
  29.     for (i = 0; i < ROW; ++i) {
  30.         for (j = 0; j < COL; ++j) {
  31.             printf("%d  ", GetPower(state[i][j], i, j));
  32.         }
  33.         printf("\n");
  34.     }
  35.     return 0;
  36. }
  37.  
  38. int Input(int min, int max) {
  39.     int tmp, isInvalid, pos;
  40.     do {
  41.         tmp = scanf("%d", &pos);
  42.         while (getchar() != '\n');
  43.         if (tmp != 1 || (pos < min || pos > max)) {
  44.             printf("!!! Vui long nhap dung so tu %d den %d !!!\n", min, max);
  45.             isInvalid = 1;
  46.         } else isInvalid = 0;
  47.     } while (isInvalid);
  48.     return pos;
  49. }
  50.  
  51. int main () {
  52.     int isOut = 0, isInvalid, choice, tmp;
  53.     int state[ROW][COL] = {0};
  54.     int posRow, posCol, selectState, i, j;
  55.  
  56.     do {
  57.         printf("\n----------------------------------------------\n");
  58.         printf("1. Bat/Tat den theo hang\n");
  59.         printf("2. Bat/Tat den theo cot\n");
  60.         printf("3. Bat/Tat den theo vi tri\n");
  61.         printf("4. Cong suat tieu thu hien thoi\n");
  62.         printf("0. Thoat\n");
  63.         printf("----------------------------------------------\n");
  64.         printf("> Nhap lua chon: ");
  65.         do {
  66.             tmp = scanf("%d", &choice);
  67.             while (getchar() != '\n');
  68.             if (tmp != 1) {
  69.                 printf("!!! Vui long nhap dung lua chon !!!\n");
  70.                 isInvalid = 1;
  71.             } else isInvalid = 0;
  72.         } while (isInvalid);
  73.         switch (choice) {
  74.             case 1:
  75.                 printf("\n----------------------------------------------\n");
  76.                 printf("> Ban muon tat hay bat (0 = Tat, 1 = Bat): ");
  77.                 selectState = Input(0, 1);
  78.                 printf("> Nhap hang can %s: ", selectState ? "Bat" : "Tat");
  79.                 posRow = Input(1, ROW);
  80.                 for (j = 0; j < COL; ++j)
  81.                     state[posRow-1][j] = selectState;
  82.                 printf(">>> Da %s hang %d thanh cong !\n", selectState ? "Bat" : "Tat", posRow);
  83.                 printf("----------------------------------------------\n");
  84.                 break;
  85.             case 2:
  86.                 printf("\n--------------------------------------------\n");
  87.                 printf("> Ban muon tat hay bat (0 = Tat, 1 = Bat): ");
  88.                 selectState = Input(0, 1);
  89.                 printf("> Nhap cot can %s: ", selectState ? "Bat" : "Tat");
  90.                 posCol = Input(1, COL);
  91.                 for (i = 0; i < ROW; ++i)
  92.                     state[i][posCol-1] = selectState;
  93.                 printf(">>> Da %s cot %d thanh cong !\n", selectState ? "Bat" : "Tat", posCol);
  94.                 printf("----------------------------------------------\n");
  95.                 break;
  96.             case 3:
  97.                 printf("\n----------------------------------------------\n");
  98.                 printf("> Ban muon tat hay bat (0 = Tat, 1 = Bat): ");
  99.                 selectState = Input(0, 1);
  100.                 printf("> Nhap toa do hang can %s: ", selectState ? "Bat" : "Tat");
  101.                 posRow = Input(1, ROW);
  102.                 printf("> Nhap toa do cot can %s: ", selectState ? "Bat" : "Tat");
  103.                 posCol = Input(1, COL);
  104.                 state[posCol-1][posCol-1] = selectState;
  105.                 printf(">>> Da %s den (%d, %d) thanh cong !\n", selectState ? "Bat" : "Tat", posRow, posCol);
  106.                 printf("----------------------------------------------\n");
  107.                 break;
  108.             case 4:
  109.                 printf("\n----------------------------------------------\n");
  110.                 PrintPower(state);
  111.                 printf("----------------------------------------------\n");
  112.                 break;
  113.             case 0:
  114.                 isOut = 1;
  115.             default:
  116.                 printf("!!! Vui long nhap lua chon tu 0 den 4 !!!\n");
  117.         }
  118.     } while (!isOut);
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement