科目名稱:資訊系統 與網路導論 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2009.12.9 |
(考試時間: 14:10-14:30)
// Pass-by-value
#include <iostream>
using std::cout;
using std::endl;
void test(char p[])
{ cout << ++p << endl;
}
int main()
{
char *name = "BASIC";
test(++name);
cout << name++ << endl;
test(++name);
cout << name++ << endl;
}
// void function
#include <iostream>
using std::cout;
using std::endl;
void print_stars(int n);
int main()
{
int j;
for (j=1; j<6; j--)
print_stars(j);
return 0;
}
void print_stars(int n)
{
int i;
for (i=0; i<n; i++)
cout << "*";
cout << endl;
return 0;
}
// static variable
#include <iostream>
using std::cout;
using std::endl;
void check(int n)
{ static int s = 5;
int t = 5;
if (n>0)
{
s--;
t--;
check(n>>1);
}
else
{
cout << s << endl;
cout << t << endl;
}
}
int main()
{
check(12);
}
// 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=3, j=2;
swap1(i,j); print(i,j);
swap2(i,j); print(i,j);
swap3(i,j); print(i,j);
}