Date: February 23rd, 2011
Time: 14:10-16:00
Open book; turn off computer & mobile phone
#include <iostream>
using std::cout;
using std::endl;
void test(char p[])
{ cout << ++p << endl;
}
int main()
{
char *name = "ABCD";
test(name++);
cout << ++name << endl;
}
// Pass-by-reference
#include <iostream>
using std::cout;
using std::endl;
void print(int i, int j)
{
cout << "i = " << i << "\t j = " << j << endl;
}
void swap1(int a, int b)
{ int temp;
temp = a; a = b; b = temp;
}
void swap2(int &a, int &b)
{ int temp;
temp = a; a = b; b = temp;
}
void swap3(int &a, int b)
{ int temp;
temp = a; a = b; b = temp;
}
int main()
{
int i=20, j=11;
swap1(i,j); print(i,j);
swap2(i,j); print(i,j);
swap3(i,j); print(i,j);
}
#include <iostream>
using std::cout;
using std::endl;
struct Node
{
char value;
Node* left;
Node* right;
};
void print(Node* r)
{
if (r->left != NULL) print(r->left);
cout << r->value;
if (r->right != NULL) print(r->right);
return;
}
int main(void)
{
Node a = { 'A', 0, 0 };
Node b = { 'B', 0, 0 };
Node h = { 'H', &a, &b };
Node c = { 'C', 0, 0 };
Node d = { 'D', 0, 0 };
Node g = { 'G', &c, &d };
Node i = { 'I', &h, &g };
Node f = { 'F', 0, 0 };
Node e = { 'E', &i, &f };
print(&e);
cout << endl;
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
struct Node
{
char value;
Node* left;
Node* right;
};
void print(Node* r)
{
if (r->left != NULL) print(r->left);
if (r->right != NULL) print(r->right);
cout << r->value;
return;
}
int main(void)
{
Node a = { 'A', 0, 0 };
Node b = { 'B', 0, 0 };
Node h = { 'H', &a, &b };
Node c = { 'C', 0, 0 };
Node d = { 'D', 0, 0 };
Node g = { 'G', &c, &d };
Node i = { 'I', &h, &g };
Node f = { 'F', 0, 0 };
Node e = { 'E', &i, &f };
print(&i);
cout << endl;
return 0;
}
// Importance of the "break" statement in the "switch" statement
#include <iostream>
using std::cout;
using std::endl;
int main(void)
{
char str[] = "TAIWAN";
char *p = str;
int vowel = 0;
int consonant = 0;
for ( ; *p != '\0'; p++)
switch (*p)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowel++;
default:
consonant++;
}
cout << vowel << '\t' << consonant << endl;
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char str[10] = "February";
char* p = str;
cout << str << endl;
cout << *(++p) << endl;
for ( ; *p != '\0'; p++)
*p ^= 0x20;
cout << sizeof(*p) << endl;
cout << str << endl;
}
// Manipulator hex
#include <iostream>
using std::cout;
using std::endl;
using std::hex;
int main(void)
{
int c1 = 'A', c2 = 'a';
c1 ^= c2; cout << hex << c1 << c2 << endl;
c2 ^= c1; cout << hex << c1 << c2 << endl;
c1 ^= c2; cout << hex << c1 << c2 << endl;
return 0;
}
// Linked-List
#include <iostream>
using std::cout;
using std::endl;
struct Node
{
int value;
Node* next;
};
Node* insert(Node* head, int n)
{
Node* p = new Node;
p->value = n;
p->next = head;
return p;
}
void print_list(Node* p)
{
int n = 0;
while (p)
{
cout << "Line " << ++n << ":" << p->value << endl;
p = p->next;
}
return;
}
int main(void)
{
int A[] = { 1, 3, 5, 7, 2, 4, 6 };
int n = sizeof(A) / sizeof(A[0]);
Node* head = NULL;
for (int i=0; i<n; i++)
head = insert(head, A[i]);
print_list(head);
return 0;
}