1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
using namespace std;
// 0 1 2 3 4 5
// 012345678901234567890123456789012345678901234567890123
char txt[] = "ENDELIG-BLE-DET-EDINBURGH-I-VAAR-PIEMONTE-NESTE-I-HOST";
int
main()
{
int i = 5;
int j = i * i, k = 19 % 12 + i * 8; // j=25. k=47
int n = j, m = k; // n=25. m=47
// Starting at 25 & 47, txt matches through "-I-"
// So the loop executes 3 times.
while (txt[j] == txt[k]) {
j++;
++k;
}
// Now j=28, k=50
cout << txt[j] << ' ' << txt[k] << '\n'; // prints "V H"
j = n; // j=25
k = m; // k=47
// Note that this is an empty loop ( selicolon at end of line)
// As before, the loop executes 3 times, but THE CONDITION IS
// EVALUATED 4 TIMES. The 4th time is when it fails. So
// when the loop ends, j=29 and k=51
while (txt[j++] == txt[k++]) ;
// The ++'s below are post increment, so it outputs
// txt[j] and txt[k], then increments j and k.
cout << txt[j++] << ' ' << txt[k++] << '\n'; // print "A O"
// Now j=30 and k=52
cout << txt[j] << ' ' << txt[k] << '\n'; // prints "A S"
i = 5 * 6 / 10; // i = (5*6)/10 = 3;
j = n / i; // j = 25/3 = 8
// Starting at 3 and 8, txt is "EL" and "BL"
// The loop executes once, so i and j are incremented
// once. Again, this uses post increment, so it prints
// txt[3] and txt[8] then increments i and j
while (txt[i] != txt[j])
cout << txt[i++] << ' ' << txt[j++] << '\n'; // prints "E B"
// i=4, j=9, n=25
// i+n=29
// Note that ++j is PREincrement, so
// ++j + n % 7 is 10 + (25 % 7) = 14
// So this prints txt[29] and txt[14]
cout << txt[i + n] << ' ' << txt[++j + n % 7] << '\n'; // prints "A T"
return 0;
}
|