科目:
程式設計
|
學號: |
考試日期 |
2025.6.6 |
期末考 |
姓名:
|
考試時間 |
14:10-17:00 |
Open book; turn off computer &
mobile phone
- (10%) Which of the following is a valid template declaration?
- template<T> void func(T
x){}
- template<class
T> T func(x){}
- template<typename
T> func(T x){}
- template<typename
T> void func(T x){}
- (10%) In Visual C++, when I am designing an MFC application, if I
want to create an event handler OnLButtonDown()
to handle the event
when the mouse left button is down, which tab in the Properties Windows
should I navigate to find the corresponding Windows Message to create
the event handler?

- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the line(s) with syntax mistake(s).
// Callback Function and Overloading
Operator<<
#include <iostream>
#include <cstdlib>
using std::ostream;
class Rational {
public:
int numerator;
int denominator;
Rational(int n=0, int d=1): numerator(n),
denominator(d) {}
};
int comp(const void* p1, const void* p2) {
const Rational a1 = *(static_cast<const
Rational*>(p1));
const Rational a2 = *(static_cast<const
Rational*>(p2));
return a2.numerator*a1.denominator -
a1.numerator*a2.denominator;
}
ostream& operator<<(ostream& o, const Rational& a) {
o << a.numerator;
if (a.denominator != 1)
o << '/' <<
a.denominator;
return o;
}
int main() {
Rational A[] = { Rational(1, 1), Rational(1, 2),
Rational(1, 3),
Rational(2), Rational(3) };
qsort(A, sizeof(A)/sizeof(A[0]), sizeof(A[0]), comp);
for (size_t i=0; i<sizeof(A)/sizeof(A[0]); ++i)
std::cout << A[i]
<< ' ';
return 0;
}
- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the line(s) with syntax mistake(s).
// Binary Tree
#include <iostream>
class CNode {
public:
int value;
CNode* left;
CNode* right;
CNode(int v): value(v), left(nullptr),
right(nullptr) {}
void Print() {
if (right)
right->Print();
std::cout << value;
if (left)
left->Print();
}
void Insert(int v) {
if (v < value) {
if (left)
left->Insert(v);
else
left = new CNode(v);
} else {
if
(right)
right->Insert(v);
else
right = new CNode(v);
}
}
};
class CTree {
public:
CNode* root;
CTree() { root = nullptr; }
void Print() { root->Print(); }
CTree& Insert(int v) {
if (root)
root->Insert(v);
else
root
= new CNode(v);
return *this;
}
};
int main() {
CTree t;
t.Insert(3).Insert(1).Insert(2).Insert(5).Insert(4);
t.Print();
std::cout << std::endl;
return 0;
}
- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the line(s) with syntax mistake(s).
// Destructor
#include <iostream>
class CInt {
public:
int value;
CInt(int v): value(v) { std::cout <<
"Constructor for " << value <<
std::end
l;}
~CInt() { std::cout << "Destructor for "
<< value << std::endl; }
};
class CRational {
CInt numerator;
CInt denominator;
public:
CRational(int n=0, int d=1): numerator(CInt(n)),
denominator(CInt(d)) {
std::cout <<
"Constructor for " << n << '/' << d <<
std::endl;
}
~CRational() {
std::cout <<
"Destructor for " << numerator.value << '/'
<< denominator.value << std::endl;
}
};
int main() {
CRational a(1, 2);
CRational b(3, 4);
return 0;
}
- (10%) Consider
the following program. Please note that the line number on the
left is only shown for your convenience. They are not part of the
code.
1
#include <iostream>
2
3 int main()
4 {
5 int total = 0;
6 int i = 1;
7 while (i <= 100) {
8 total += i;
9 ++i;
10 }
11 std::cout << total <<
std::endl;
12 return 0;
13 }
If
we compile this program with "g++ -g q6.cpp -o q6.exe", and run "gdb
q6.exe" with the following commands, what value of i shall we obtain?
break 9 if total >= 3003
run
print i
- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the line(s) with syntax mistake(s).
// Vector
#include <iostream>
#include <vector>
int main()
{
std::vector<int> b(2, 5);
b.resize(4, 9);
b.insert(b.begin()+1, 3);
b.erase(b.begin()+3);
for (size_t i=0; i<b.size(); ++i)
std::cout << b.at(i)
<< ' ';
std::cout << std::endl;
return 0;
}
- (10%) Suppose
you successfully compile the following program by "g++ -I ~solomon/CPP
q8.cpp". Predict the output when you run this program.
// JSON
#include <iostream>
#include <json.hpp>
using nlohmann::json;
int main()
{
json ex8 = json::parse("[ [1,2,3,4], [5,6,7,8] ]");
std::cout << ex8.size() << std::endl;
std::cout << ex8[1].size() << std::endl;
return 0;
}
- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the line(s) with syntax mistake(s).
// Overloading the Decrement Operator
#include <iostream>
using std::ostream;
class CRational {
friend ostream& operator<<(ostream& o, const
CRational& b);
public:
CRational(int n = 0, int d = 1): m_numerator(n),
m_denominator(d) {}
CRational& operator--() { m_numerator--; return
*this; }
CRational operator--(int n) { CRational t = *this;
m_denominator--; return t; }
private:
int m_numerator;
int m_denominator;
};
ostream& operator<<(ostream& o, const CRational& b) {
o << b.m_numerator << '/' <<
b.m_denominator;
return o;
}
int main() {
CRational a(2, 3);
CRational b(3, 5);
std::cout << --a << ' ' << b--
<< std::endl;
return 0;
}
- (10%) Consider the
following OnDraw() function,
what result will be shown on the screen after you run the
program? Please specify the coordinates of endpoints of each line
segment.
// Polygon
void CQ10View::OnDraw(CDC* pDC)
{
int x0 = 100;
int y0 = 100;
for (int i = 1; i <= 3; ++i) {
CPoint p[3] = { CPoint(x0, y0 -
10 * i), CPoint(x0 - 10 * i, y0 + 10 * i), CPoint(x0 + 10 * i, y0 + 10
* i) };
pDC->Polygon(p, 3);
}
}