Solutions to Homework Assignment #4

1. 使用真假值表以驗證下列邏輯等式的正確性:

(a) pÙq º Ø(ØpÚØq)

Answer:

p q pÙq Øp Øq ØpÚØq Ø(ØpÚØq)

0

0

1

1

0

1

0

1

0

0

0

1

1

1

0

0

1

0

1

0

1

1

1

0

0

0

0

1

(b) pÞq º ØpÚq

Answer:

p q pÞq Øp ØpÚq

0

0

1

1

0

1

0

1

1

1

0

1

1

1

0

0

1

1

0

1

(c) pÛq º Ø(pÚq)ÚØ(ØpÚØq)

Answer:

p q pÛq Øp Øq pÚq Ø(pÚq) ØpÚØq Ø(ØpÚØq) Ø(pÚq)ÚØ(ØpÚØq)

0

0

1

1

0

1

0

1

1

0

0

1

1

1

0

0

1

0

1

0

0

1

1

1

1

0

0

0

1

1

1

0

0

0

0

1

1

0

0

1

2. 對下列的每個英文句子,(i) 指出其中的基本句子 (primitive statement),並取一個命題名稱 (propositional identifier),(ii) 將每一個英文句子寫成邏輯的命題公式 (propositional formula) 。

(a) Finishing the writing of my homework assignment before dinner is necessary for my going to a movie tonight.

(b) Rainy day and Sunday are sufficient for me to watch TV at home.

(c) If John works on his programming assignment, then he will get a good grade if he studies hard enough.

Answer:

(a) (i) p: I finish the writing of my homework assignment before dinner,

          q: I go to a movie tonight.

     (ii) Formula: qÞp.

(b) (i) p: It is a rainy day.

          q: It is Sunday.

          r: I watch TV at home.

     (ii) Formula: (pÙq)Þr.

(c) (i) p: John works on his programming assignment.

         q: John gets a good grade.

         r: John studies hard enough.

     (ii) Formula: pÞ(rÞq).

3. 假設變數 u, v, w 都是 unsigned int 的資料型態,使用 C 語言的位元邏輯運算 (bitwise logic operations) 改寫下列的算術式或所描述的運算:(256=28, 4096=212)

(a) u * 256 + v % 256

(b) w / 4096 * 4096 + 78

(c) 將變數 u 的左邊十六個位元作補數 (complement),再乘以 32 ,忽略滿溢 (overflow) 現象。

Answer:

(a) (u << 8) | (v & 0X000000FF)

(b) (w & 0XFFFFF000) | 0X0000004E

(c) (u ^ 0XFFFF0000 ) << 5

4. 假設有兩個 32 位元的變數 xy,分別為 x31x30x1x0 y31y30…y1y0,使用 C 語言的位元邏輯運算 (bitwise logic operations) 寫出一個 C 語言的程式片段,以計算下列的結果:

(a) u = x150x140…x10x00

(b) v = y0y1y14y15x31x30x17x16

Answer:

(a) u = 0;
    for (i=15; i>=0; i--) u = u | ((x & (1 << i)) << (i+1));

(b) v = (~x) >> 16;
    for (i=0; i<16; i++) v = v | (((~y) & (1 << i)) << (31-2*i));

5. 假設在一個 C 語言的程式中三個整數變數 u, v, 和 w 的值分別被設定為 10, 15, 和 8. 如果下列的敘述句依序被執行,寫出在每一行執行結束後,變數  u, v, 和 w 的值是什麼?

1

2

3

4

5

if (u+5==v) {u = w * 2; v++;} else {u = --v;}
if (u==v && v==w*2) {v = u % 2;}
if (v==w) {w = u - 10; v = w + 2;} else {v = w + 2; w = u - 10;}
if (u>v++*4) {u = v * 4; w = v % 8;}
if (u=v/10) {v = u + 2; w = u - 2;} else {v = u - 2; w = v + 2;}

Answer:

After Line 1: u=16, v=16, w=8
After Line 2: u=16, v=0,  w=8
After Line 3: u=16, v=10, w=6
After Line 4: u=16, v=11, w=6
After Line 5: u=1,  v=3,  w=-1

6. 將下列的 switch-case 的敘述句 (statement) 改寫成 if-thenif-then-else 的敘述句:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

switch (kind) {
  case 'A': {
    switch (quantity) {
      case 1: {printf("One Coke: NT$ %d\n", quantity*18); break;}
      case 2: {printf("Two Coke: NT$ %d\n", quantity*18); break;}
      default: {printf("%d Coke: NT$ %d\n", quantity, quantity*18);}
    }
    break;
  }
  case 'B': {
    switch (quantity) {
      case 1: {printf("One Milk: NT$ %d\n", quantity*12); break;}
      case 2: {printf("Two Milk: NT$ %d\n", quantity*12); break;}
      default: {printf("%d Milk: NT$ %d\n", quantity, quantity*12);}
    }

    break;

  }
  default: printf("No such drinks available.\n");
}

Solution:

if (kind=='A') {

  if (quantity==1) {

    printf("One Coke: NT$ %d\n", quantity*18);

  }

  else if (quantity==2) {

    printf("Two Coke: NT$ %d\n", quantity*18);

  }

  else {

    printf("%d Coke: NT$ %d\n", quantity, quantity*18);

  }

}

else if (kind='B') {

  if (quantity==1) {

    printf("One Milk: NT$ %d\n", quantity*12);

  }

  else if (quantity==2) {

    printf("Two Milk: NT$ %d\n", quantity*12);

  }

  else {

    printf("%d Milk: NT$ %d\n", quantity, quantity*12);

  }

}

else {

  printf("No such drinks available.\n");

}