Глава 04
listing 1
// Demonstrate the % operator.
using System;
class ModDemo
{
public static void Main()
{
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0;
Console.WriteLine("Result and remainder of 10 / 3: " +
iresult + " " + irem);
Console.WriteLine("Result and remainder of 10.0 / 3.0: " +
dresult + " " + drem);
}
}
Result and remainder of 10 / 3: 3 1
Result and remainder of 10.0 / 3.0: 3,33333333333333 1
Анализ:
Вверх
x = 1;
Series generated using y = x + x++;
Series generated using y = x + ++x;
Анализ:
Вверх
i = 10;
i < j
Анализ:
Вверх
for(i = 0; i < 2; i++)
{
Console.WriteLine("p is " + p + ", q is " + q);
p is True, q is True
p is True, q is False
p is False, q is True
p is False, q is False
Анализ:
Вверх
n = 10;
2 is a factor of 10
Анализ:
Пришлось заремировать инструкцию
if(d != 0 & (n % d) == 0)
так как компилятор выдал исключение о делении на нуль.
Вверх
i = 0;
/* Here, i is still incremented even though
if statement executed: 1
Анализ:
Вверх
for(i = 1; i <= 10; i++)
{
Console.WriteLine("num: " + num);
num: 1
num: 2
num: 2
num: 3
num: 4
num: 5
num: 6
num: 7
num: 8
num: 9
num: 10
Анализ:
Вверх
num = 10;
if((num & 1) == 1)
11 is odd.
Анализ:
Вверх
val = 123;
0 1 1 1 1 0 1 1
Анализ:
Вверх
for(i = 1; i <= 10; i++)
{
Console.WriteLine("num: " + num);
num: 1
num: 2
num: 3
num: 4
num: 5
num: 6
num: 7
num: 8
num: 9
num: 10
Анализ:
Вверх
int key = 88;
Console.WriteLine("Original message: " +
Original message: Hi!
Анализ:
Вверх
for(t=128; t > 0; t = t/2)
{
1 1 0 1 1 1 1 0
Анализ:
Вверх
for(i = 0; i < 8; i++)
{
0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
Анализ:
Вверх
n = 10;
Console.WriteLine("Value of n: " + n);
Value of n: 10
Value of n: 10
Анализ:
Вверх
for(i = -5; i < 6; i++)
{
100 / -5 is -20
Анализ:
Вверх
for(i = -5; i < 6; i++)
100 / -5 is -20
Анализ:
listing 2
/*
Demonstrate the difference between prefix
postfix forms of ++.
*/
using System;
class PrePostDemo
{
int i;
Console.WriteLine("Series generated using y = x + x++;");
for(i = 0; i < 10; i++)
{
Console.WriteLine(y + " ");
Console.WriteLine();
x = 1;
Console.WriteLine("Series generated using y = x + ++x;");
for(i = 0; i < 10; i++)
{
Console.WriteLine(y + " ");
Console.WriteLine();
Результат:
2
4
6
8
10
12
14
16
18
20
3
5
7
9
11
13
15
17
19
21
listing 3
// Demonstrate the relational and logical operators.
using System;
class RelLogOps
{
bool b1, b2;
j = 11;
if(i < j) Console.WriteLine("i < j");
if(i <= j) Console.WriteLine("i <= j");
if(i != j) Console.WriteLine("i != j");
if(i == j) Console.WriteLine("this won't execute");
if(i >= j) Console.WriteLine("this won't execute");
if(i > j) Console.WriteLine("this won't execute");
b1 = true;
b2 = false;
if(b1 & b2) Console.WriteLine("this won't execute");
if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) is true");
if(b1 | b2) Console.WriteLine("b1 | b2 is true");
if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true");
Результат:
i <= j
i != j
!(b1 & b2) is true
b1 | b2 is true
b1 ^ b2 is true
listing 4
// Create an implication operator in C#.
using System;
class Implication
{
int i, j;
if(i==1) p = false;
if(j==0) q = true;
if(j==1) q = false;
if(!p | q) Console.WriteLine(p + " implies " + q +
" is " + true);
Console.WriteLine();
Результат:
True implies True is True
False implies True is True
False implies False is True
listing 5
// Demonstrate the short-circuit operators.
using System;
class SCops
{
d = 2;
if(d != 0 && (n % d) == 0)
Console.WriteLine(d + " is a factor of " + n);
d = 0; // now, set d to zero
// Since d is zero, the second operand is not evaluated.
if(d != 0 && (n % d) == 0)
Console.WriteLine(d + " is a factor of " + n);
/* Now, try the same thing without short-circuit operator.
This will cause a divide-by-zero error. */
if(d != 0 & (n % d) == 0)
Console.WriteLine(d + " is a factor of " + n);
Результат:
0 is a factor of 10
listing 6
// Side-effects can be important.
using System;
class SideEffects
{
the if statement fails. */
if(false & (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // displays 1
/* In this case, i is not incremented because
the short-circuit operator skips the increment. */
if(false && (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // still 1 !!
Результат:
if statement executed: 1
listing 7
// Use bitwise AND to make a number even.
using System;
class MakeEven
{
ushort i;
num = (ushort) (num & 0xFFFE); // num & 1111 1110
Console.WriteLine("num after turning off bit zero: "
+ num + "\n");
Результат:
num after turning off bit zero: 0
num after turning off bit zero: 0
num after turning off bit zero: 2
num after turning off bit zero: 2
num after turning off bit zero: 4
num after turning off bit zero: 4
num after turning off bit zero: 6
num after turning off bit zero: 6
num after turning off bit zero: 8
num after turning off bit zero: 8
num after turning off bit zero: 10
listing 8
// Use bitwise AND to determine if a number is odd.
using System;
class IsOdd
{
Console.WriteLine("This won't display.");
num = 11;
if((num & 1) == 1)
Console.WriteLine(num + " is odd.");
Результат:
listing 9
// Display the bits within a byte.
using System;
class ShowBits
{
byte val;
for(t=128; t > 0; t = t/2)
{
if((val & t) == 0) Console.Write("0 ");
Результат:
listing 10
// Use bitwise OR to make a number odd.
using System;
class MakeOdd
{
ushort i;
num = (ushort) (num | 1); // num | 0000 0001
Console.WriteLine("num after turning on bit zero: "
+ num + "\n");
Результат:
num after turning on bit zero: 1
num after turning on bit zero: 3
num after turning on bit zero: 3
num after turning on bit zero: 5
num after turning on bit zero: 5
num after turning on bit zero: 7
num after turning on bit zero: 7
num after turning on bit zero: 9
num after turning on bit zero: 9
num after turning on bit zero: 11
listing 11
// Use XOR to encode and decode a message.
using System;
class Encode
{
char ch2 = 'i';
char ch3 = '!';
ch1 + ch2 + ch3);
// encode the message
ch1 = (char) (ch1 ^ key);
ch2 = (char) (ch2 ^ key);
ch3 = (char) (ch3 ^ key);
Console.WriteLine("Encoded message: " +
ch1 + ch2 + ch3);
// decode the message
ch1 = (char) (ch1 ^ key);
ch2 = (char) (ch2 ^ key);
ch3 = (char) (ch3 ^ key);
Console.WriteLine("Decoded message: " +
ch1 + ch2 + ch3);
Результат:
Encoded message: >1y
Decoded message: Hi!
listing 12
// Demonstrate the bitwise NOT.
using System;
class NotDemo
{
int t;
if((b & t) == 0) Console.Write("0 ");
Console.WriteLine();
// reverse all bits
b = (sbyte) ~b;
for(t=128; t > 0; t = t/2)
{
if((b & t) == 0) Console.Write("0 ");
Результат:
0 0 1 0 0 0 0 1
listing 13
// Demonstrate the shift << and >> operators.
using System;
class ShiftDemo
{
int t;
int i;
if((val & t) == 0) Console.Write("0 ");
Console.WriteLine();
val = val << 1; // left shift
Console.WriteLine();
val = 128;
for(i = 0; i < 8; i++)
{
if((val & t) == 0) Console.Write("0 ");
Console.WriteLine();
val = val >> 1; // right shift
Результат:
0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
listing 14
// Use the shift operators to multiply and divide by 2.
using System;
class MultDiv
{
// multiply by 2
n = n << 1;
Console.WriteLine("Value of n after n = n * 2: " + n);
// multiply by 4
n = n << 2;
Console.WriteLine("Value of n after n = n * 4: " + n);
// divide by 2
n = n >> 1;
Console.WriteLine("Value of n after n = n / 2: " + n);
// divide by 4
n = n >> 2;
Console.WriteLine("Value of n after n = n / 4: " + n);
Console.WriteLine();
// reset n
n = 10;
Console.WriteLine("Value of n: " + n);
// multiply by 2, 30 times
n = n << 30; // data is lost
Console.WriteLine("Value of n after left-shifting 30 places: " + n);
Результат:
Value of n after n = n * 2: 20
Value of n after n = n * 4: 80
Value of n after n = n / 2: 40
Value of n after n = n / 4: 10
Value of n after left-shifting 30 places: -2147483648
listing 15
// Prevent a division by zero using the ?.
using System;
class NoZeroDiv
{
int i;
if(i != 0)
Console.WriteLine("100 / " + i + " is " + result);
Результат:
100 / -4 is -25
100 / -3 is -33
100 / -2 is -50
100 / -1 is -100
100 / 1 is 100
100 / 2 is 50
100 / 3 is 33
100 / 4 is 25
100 / 5 is 20
listing 16
// Prevent a division by zero using the ?.
using System;
class NoZeroDiv2
{
if(i != 0 ? true : false)
Console.WriteLine("100 / " + i +
" is " + 100 / i);
Результат:
100 / -4 is -25
100 / -3 is -33
100 / -2 is -50
100 / -1 is -100
100 / 1 is 100
100 / 2 is 50
100 / 3 is 33
100 / 4 is 25
100 / 5 is 20