48cd912194555ebe8aab6ebaa48a2bcb267a9b82
[sdk] / installer / coursework / Chapter 3 - Operators / Lab3 / lab3.ec
1 bool IsGreaterThan10(int value)
2 {
3    PrintLn("IsGreaterThan10!");
4    return value > 10;
5 }
6
7 class OperatorsApp : Application
8 {
9    void Main()
10    {
11       PrintLn(" *** Question 4 *** ");
12       {
13          int a = 12, b = 7, c;
14          c = b - a + 23;
15          PrintLn(c);
16       }
17       PrintLn(" *** Question 6 *** ");
18       {
19          int result = 5 + 3 * 4;
20          PrintLn(result);
21       }
22       PrintLn(" *** Question 7 *** ");
23       {
24          int result = (5 + 3) * 4;
25          PrintLn(result);
26       }
27       PrintLn(" *** Question 8 *** ");
28       {
29          int result = 17 / 4;
30          PrintLn(result);
31       }
32       PrintLn(" *** Question 9 *** ");
33       {
34          int result = 17 % 4;
35          PrintLn(result);
36       }
37       PrintLn(" *** Question 10 *** ");
38       {
39          float result = 17 / 4.0f;
40          PrintLn(result);
41       }
42
43       PrintLn(" *** Question 13 *** ");
44       {
45          int a = 7, b = 24;
46          int result = a++ * 4 - --b / 6;
47          PrintLn(result);
48       }
49       PrintLn(" *** Question 19 *** ");
50       {
51          int a = 10;
52          PrintLn(a = 5);
53          PrintLn(a);
54       }
55       PrintLn(" *** Question 20 *** ");
56       {
57          int a = 10;
58          PrintLn(a == 5);
59          PrintLn(a);
60       }
61       PrintLn(" *** Question 21 *** ");
62       {
63          int a = 7, b = 7;
64          a = (b == 7) + 7;
65          PrintLn("a = ", a, ", b = ", b);
66       }
67       PrintLn(" *** Question 25 *** ");
68       {
69          int a = 7, b = 9, c = 0;
70          PrintLn("a) ", a > 2 && a < 8);
71          PrintLn("b) ", a < 6 || b > 8);
72          PrintLn("c) ", a >= 7 && b < 10);
73          PrintLn("d) ", a && b && c);
74          PrintLn("e) ", a || c);
75          PrintLn("f) ", !a);
76          PrintLn("g) ", !b || !c);
77          PrintLn("h) ", !!c);
78          PrintLn("i) ", !(a > 5 && b == 9));
79          PrintLn("j) ", (c = 5) || (a = 0));
80          // We need to reset c because j) modified it
81          c = 0;
82          PrintLn("k) ", !(c > 7 || b <= 9) && !c);
83       }
84       PrintLn(" *** Question 26 *** ");
85       {
86          int a = 3, b = 8, c = 9;
87          PrintLn(a >= 3 || b < 8 && c != 9);
88       }
89       PrintLn(" *** Question 28 *** ");
90       {
91          int a = 7, b = 11, c = 17;
92          PrintLn("a) ", a > 6 && IsGreaterThan10(b), ", a = ", a);
93          PrintLn("b) ", IsGreaterThan10(a) && (a = 12), ", a = ", a);
94          a = 7;
95          PrintLn("c) ", (a = 12) && IsGreaterThan10(a), ", a = ", a);
96          a = 7;
97          PrintLn("d) ", (a = 0) && IsGreaterThan10(a), ", a = ", a);
98          a = 7;
99          PrintLn("e) ", (c == 12) || IsGreaterThan10(b), ", a = ", a);
100          PrintLn("f) ", (b > 8 && a < 4) || IsGreaterThan10(c), ", a = ", a);
101          PrintLn("g) ", (c < 16 || b % 2) && IsGreaterThan10(a), ", a = ", a);
102          PrintLn("h) ", c - b < a && (a = 12) && IsGreaterThan10(a), ", a = ", a);
103          a = 7;
104          PrintLn("i) ", (c > 15 && (b = 7)) || IsGreaterThan10(b) || (a = 5), ", a = ", a);
105          a = 7; b = 11;
106          PrintLn("j) ", b = (a = (c = 12) + (a = 12) + IsGreaterThan10(b) * 12), ", a = ", a);
107       }
108
109       PrintLn(" *** Question 29 *** ");
110       {
111          byte a = 0x5B, b = 0x83;
112
113          // Print/PrintLn are missing a nice way to display in hexadecimal, so we're using printf for that purpose
114          printf("a) 0x%02X\n", a & b);
115          printf("b) 0x%02X\n", a | b);
116          printf("c) 0x%02X\n", a ^ b);
117          {
118             // There is an eC compiler bug (mantis #108) that prevents a simple cast from working here:
119             // printf(" 0x%02X\n", (byte)~a);  // This works in C and should work in eC once the bug is fixed.
120             byte result = ~a;
121             printf("d) 0x%02X\n", result);
122          }
123          printf("e) 0x%02X\n", (a & b) ^ b);
124          printf("f) 0x%02X\n", a ^ a);
125          printf("g) 0x%02X\n", a << 1);
126          printf("h) 0x%02X\n", b >> 5);
127          printf("i) 0x%02X\n", (a & 0x10) >> 4);
128          PrintLn("j) ", (b & 0x0F) == 3);
129       }
130
131       PrintLn(" *** Question 31 *** ");
132       {
133          byte a = 0x05;
134          a |= 0x20;
135          printf(" 0x%02X\n", a);
136       }
137       PrintLn(" *** Question 32 *** ");
138       {
139          byte a = 0x25, b = 0x37;
140          a &= 0xF0;
141          b &= 0xF0;
142          printf("a = 0x%02X, b = 0x%02X\n", a, b);
143       }
144       PrintLn(" *** Question 33 *** ");
145       {
146          byte a = 0x25, b = 0x37;
147          a &= ~0x0F;
148          b &= ~0x0F;
149          printf("a = 0x%02X, b = 0x%02X\n", a, b);
150       }
151       PrintLn(" *** Question 34 *** ");
152       {
153          byte a = 0x20;
154          PrintLn(a & 0x20 == 0x20);
155       }
156       PrintLn(" *** Question 35 *** ");
157       {
158          byte a = 0x20;
159          PrintLn((a & 0x20) == 0x20);
160       }
161       
162       system("pause");
163    }
164 }