Date: May 7th, 2013
Time: 14:10-16:00
Open book; turn off computer & mobile phone
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Static Data Member
#include <iostream>
using std::cout;
using std::endl;
class CNode
{
public:
int value;
CNode* next;
static CNode* pFirstNode;
static CNode* pLastNode;
CNode(int v) :
value(v), next(NULL)
{
if (!pFirstNode)
pFirstNode = this;
if (pLastNode)
pLastNode->next = this;
pLastNode = this;
}
int Count() // Calculate this carefully
{
int n = 0;
CNode* p;
for (p = pFirstNode; p != pLastNode; p = p->next)
n++;
return n;
}
};
CNode* CNode::pFirstNode = NULL;
CNode* CNode::pLastNode = NULL;
int main()
{
CNode a(5), b(0), c(7);
CNode* p;
cout << a.Count() << endl;
for (p=CNode::pFirstNode; p!=NULL; p=p->next)
cout << p->value;
cout << endl;
return 0;
}
(10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after you run the program? Please specify the coordinates of endpoints of each segment, or the coordinates of centers and boundaries.
#include <cmath>
#define PI 3.1415926535
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
const int R = 100;
const int N = 4;
const double theta = 2 * PI / N;
CPoint center(2*R, 2*R);
CPoint a[N];
int i;
pDC->Ellipse(center.x - R, center.y - R, center.x + R, center.y + R);
for (i=0; i<N; i++)
{
a[i].x = center.x + R * cos(i * theta);
a[i].y = center.y + R * sin(i * theta);
}
pDC->MoveTo( a[0] );
for (i=1; i<=N; i++)
pDC->LineTo( a[ i % N ] );
}
(10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after you run the program? Please specify the coordinates of endpoints of each segment, or the coordinates of centers and boundaries.
#include <cmath>
#define PI 3.1415926535
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
const int R = 100;
const int N = 8;
const double theta = 2 * PI / N;
CPoint center(2*R, 2*R);
CPoint a[N];
int i;
pDC->Ellipse(center.x - R, center.y - R, center.x + R, center.y + R);
for (i=0; i<N; i++)
{
a[i].x = center.x + R * cos(i * theta);
a[i].y = center.y + R * sin(i * theta);
}
pDC->MoveTo( a[0] );
for (i=1; i<=N; i++)
pDC->LineTo( a[ 3*i % N ] );
}